danpost wrote...
Change '16' on line 7 to '10'.
Also, change line 8 to this:
if(timer == 0)
if(timer == 0)
// with the following field declared in the world class
private InfoPanel info;
// in the constructor of that world class
TextImage.setDefaultName("Copperplate Gothic Bold");
TextImage.setDefaultSize(32);
String[][] data =
{
{ "New Game", "" },
{ "Setting", "Speed: 50\nWidth: "+getWidth()+"\nHeight: "+getHeight() },
{ "Controls", "Left: move left\nRight: move right\nSpace: shoot" }
};
addObject(info = new InfoPanel(data), getWidth()/2, getHeight()/2);import greenfoot.*;
import java.awt.Color;
import java.util.Arrays;
public class InfoPanel extends Actor
{
private Actor[] buttons;
/**
* creates an InfoPanel object using the captions and texts given
*
* param buttonData an array of paired Strings; captions and their displayed texts
*/
public InfoPanel(String[][] buttonData)
{
buttons = new Actor[buttonData.length]; // initialize the button array
for (int i=0; i<buttonData.length; i++) // for each button to add
{
buttons[i] = new Button(buttonData[i][0]); // assign new button with assigned caption to array
((Button)buttons[i]).setClickImage(createClickImage(buttonData[i][1])); // save the click image to button
}
setImage((GreenfootImage)null); // the infopanel will show click images when needed
}
/**
* creates and returns the image with given text
*
* param text the text for the image to display
*/
private GreenfootImage createClickImage(String text)
{
GreenfootImage txtImg = new TextImage(text);
GreenfootImage image = new GreenfootImage(txtImg.getWidth()+20, txtImg.getHeight()+20);
image.setColor(new Color(93, 93, 93));
image.fill();
image.setColor(Color.lightGray);
image.fillRect(5, 5, image.getWidth()-11, image.getHeight()-11);
image.drawImage(txtImg, 10, 10);
return image;
}
/**
* adds the buttons into the world when the infopanel is placed into a world (or when clicked on -- see 'act' method)
*/
protected void addedToWorld(World world)
{
for (int i=0; i<buttons.length; i++)
{
world.addObject(buttons[i], getX(), getY()+((i*2+1-buttons.length)*buttons[i].getImage().getHeight()/2));
}
}
/**
* returns to unclicked state when infopanel is clicked (button clicks go to clicked state)
*/
public void act()
{
if (Greenfoot.mouseClicked(this))
{
setImage((GreenfootImage)null); // hides infopanel object
addedToWorld(getWorld()); // adds buttons back into world
}
}
/**
* returns the array of buttons for this infopanel
*/
public Actor[] getButtons()
{
return buttons;
}
/** ********************************************************************************* */
/**
* The Button class creates clickable objects for an infopanel object
*/
private class Button extends Actor
{
private GreenfootImage clickImage; // the image to set to the infopanel owner when button is clicked
/**
* creates the button with the given text
*/
private Button(String text)
{
GreenfootImage txtImg = new TextImage(text);
GreenfootImage image = new GreenfootImage(400, 120);
image.setColor(new Color(93, 93, 93));
image.fill();
image.setColor(Color.lightGray);
image.fillRect(25, 20, 349, 79);
image.drawImage(txtImg, 200-txtImg.getWidth()/2, 60-txtImg.getHeight()/2);
setImage(image);
}
/**
* removes self if owner is removed from world;
* sets image of owner and removes all owner buttons when clicked
*/
public void act()
{
if (InfoPanel.this.getWorld() == null)
{
getWorld().removeObject(this);
return;
}
if (Greenfoot.mouseClicked(this))
{
InfoPanel.this.setImage(clickImage);
getWorld().removeObjects(Arrays.asList(buttons));
}
}
/**
* helper method to set the image to give owner when this button is clicked
*/
public void setClickImage(GreenfootImage image)
{
clickImage = image;
}
}
}