This site requires JavaScript, please enable it in your browser!
Greenfoot back
gyoshon
gyoshon wrote ...

2014/12/15

Stuck!

1
2
3
gyoshon gyoshon

2014/12/15

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.awt.Font;
import greenfoot.GreenfootSound;
import greenfoot.GreenfootImage;

public class PlayMusicButton extends Actor
{
    private static final int WIDTH = 100;
    private static final int HEIGHT = 40;
    private static final Color BACKGROUND = 
                              new Color(0, 0, 0);
    private static final float FONT_SIZE = 20.0F;
    
    public void act() 
    {
       checkMouseClick(); 
    }   
    
       private void makeIamge()
        {
        GreenfootImage image = new GreenfootImage(this.WIDTH, this.HEIGHT);
        
        
        image.fillRect(0, 0, this.WIDTH, this.HEIGHT);
        image.setColor(Color.BLACK);
        image.drawRect(0, 0,WIDTH, 1,HEIGHT,1);
        image.setColor(Color.BLACK);
        Font font = image.getFont();
        if (playing) {
            font = font.deriveFont(1);
        } else {
            font = font.deriveFont(0);
        }
        image.setFont(font);
        image.drawString(this.text, 5, 15);
        setImage(image);
    }
    
    public PlayMusicButton(String text, Color c)
    {
        this.text = text;
        this.color = c;
        
        this.WIDTH = 100;
        this.HEIGHT = 25; 
        makeImage(false); 
        this.sound = new GreenfootSound(text + ".mp3");
    }
    
    private void checkMouseClick()
    {
        if (Greenfoot.mouseClicked(this)) {
            if (this.sound.isPlaying())
            {
                this.sound.stop();
                makeIamge(false);
            }
            else
            {
                PlayerWorld player = (PlayerWorld)getWorld();
                player.stop();
                makeImage(true);
                this.sound.play();
            }
        }
    }
    
      public boolean stop()
      {
      if (this.sound.isPlaying())
      {
          this.sound.stop();
          makeIamge (false);
          return true;
       }
       return false;
    }
}
            
        
        
        
danpost danpost

2014/12/15

#
You are using several variables, 'this.c', 'this.text' and 'this.sound', that are not declared within the class. You do have variables 'text' and 'c' that are local to the PlayButtonMusic constructor (gained through parameters in the call to the constructor) and you declare a local variable for 'sound' on line 48; but, those local variables are lost when the constructor is done executing. When lines like those of 42 and 43 are executed, the value of the local variable is being set to an instance field that is already declared in the class so the values can be retained throughout the life of the object being created (the PlayMusicButton object, in this case). This page of the Java tutorials might be of some help in the matter.
danpost danpost

2014/12/15

#
A few things of note: (1) the GreenfootImage and GreenfootSound classes imported on lines 4 and 5 were already made accessible through the greenfoot package imported on line 1 -- so lines 4 and 5 can be removed; (2) static fields do not belong to the objects created within the class; using 'this', which refers to an object created from the class, to access static fields is not recommended; in this case, if you are to use anything in lieu of 'this', use 'PlayMusicButton; in general, use 'this' for instance fields and the name of the class for static fields. (3) I do not see where the class constant Color field 'BACKGROUND' is being used; you can probably remove line 11, as well;
gyoshon gyoshon

2014/12/15

#
thanks, im a noob at programming and im having lots of trouble, im suppose to make a MusicPlayer. for example i should have 5 music buttons to play each different music, and have a button called stop. so if a music is being played i can either stop or click to this next button to play next song .....
danpost danpost

2014/12/15

#
Two more things: (1) it appears you are not using the class constant FONT_SIZE field declared on line 13 anywhere, either; you can probably remove that line; and (2) line 27 calls 'drawRect' on a GreenfootImage object; however, it contains six int parameters when the method requires only four.
gyoshon gyoshon

2014/12/15

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.awt.Font;
public class PlayMusicButton extends Actor
{
    private int w;
    private int h;
    private String text;
    private Color color;
    private GreenfootSound sound;
    public void act() 
    {
        
    }
        
    public PlayMusicButton(String text, Color c)
    {
       this.text = text;
       this.color = c;
       this.w = 100;
       this.h = 25;
            
       makeImage(false);
       this.sound = new GreenfootSound(text + ".mp3");
    }    
    
    private void checkMouseClick()
    {
       if(Greenfoot.mouseClicked(this)) {
         if (this.sound.isPlaying())
         {
             this.sound.stop();
             makeImage(false);
            }
            else
            {
                PlayerWorld player = (PlayerWorld)getWorld();
                player.stop();
                makeImage(true);
                this.sound.play();
            }
        }
    }
                
    private void makeImage(boolean playing)
    {
     GreenfootImage image = new GreenfootImage(this.w, this.h);
     image.setColor(color);
     image.fillRect(0, 0, w, h);
     image.setColor(Color.BLACK);
     image.drawRect(0, 0, w -1, h -1);
     Font font = image.getFont();
     if (playing) {
         font = font.deriveFont(1);
        } else {
            font = font.deriveFont(0);
        }
     image.setFont(font);
     image.drawString(this.text, 5, 15);
     setImage(image);
    }
    
    public boolean stop()
    {
        if (this.sound.isPlaying())
        {
            this.sound.stop();
            makeImage(false);
            return true;
        }
        return false;
    }
    
    
}    
gyoshon gyoshon

2014/12/15

#
this is what i've done so far, how would i set it up, so that when i make my musicbox it can be able to make the music by clicking on it, also to "Start andStop" the music? and complied it brought me to my PlayerWorld(World), this is where i believe i should insert the music correct?
danpost danpost

2014/12/15

#
You seem to have removed the line you originally had in your act method. Why?
gyoshon gyoshon

2014/12/15

#
which line?! o.o
danpost danpost

2014/12/15

#
It was line 17 in your original posting of the class.
danpost danpost

2014/12/15

#
Out of curiosity, please post the 'stop' method in your PlayerWorld class.
gyoshon gyoshon

2014/12/15

#
o no i still have i changed it up early..
gyoshon gyoshon

2014/12/15

#
now when i compile it brought me to my World......
gyoshon gyoshon

2014/12/15

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

public class PlayerWorld extends World
{
    
   
    public PlayerWorld()
    {    
        super(600, 400, 1); 
        
        int x = 110;
        int y = 70;
        int s = 50;
        addObject(this.buttons[0], x, y);
        addObject(this.buttons[1], x, y + s);
        addObject(this.buttons[2], x, y + 2 * s);
        addObject(this.buttons[3], x, y + 3 * s);
        addObject(this.buttons[4], x, y + 4 * s);
        
        addObject(new StopButton(), 315, 78);
    }
    
    public void stop()
    {
        for (PlayMusicButton button : this.buttons) {
            if (button.stop()) {
                break;
            }
        }
    }
    
}
gyoshon gyoshon

2014/12/15

#
this is where i'm stuck, making the buttons and the music play and stop
There are more replies on the next page.
1
2
3