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

2013/7/25

doubt in green foot

viyyashkumar viyyashkumar

2013/7/25

#
how to execute a message box when game is over? and how to stop the game when the game is over? pls give me the commands
danpost danpost

2013/7/25

#
The is no easy executable command to show a message box; however, the Greenfoot class API has a method to stop the execution of your project.
Gevater_Tod4711 Gevater_Tod4711

2013/7/26

#
Here is a very simple example for a JDialog that can show up when your game ends:
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;

public class DialogTest extends JDialog {
	
	private static final long serialVersionUID = 1L;
	
	public static void main(String[] args)
    {
		new DialogTest();
            
    }
	
	public DialogTest() {
		setVisible(true);
		setDefaultCloseOperation(HIDE_ON_CLOSE);
        setTitle("Mein JDialog Beispiel");
        setLayout(new FlowLayout());
        setSize(200,200);
        setModal(true);
        setVisible(true);
        JLabel dialogLabel = new JLabel("Enter Your Text Here");
        JButton okButton = new JButton("OK");
        okButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent event)
            {
                dispose();
            }
        });
        
        add(dialogLabel);
        add(okButton);   
	}
	
}
danpost danpost

2013/7/26

#
The simplest thing to do would be to remove all objects, clear the background and draw the message on the background itself.
// imports
import java.awt.Color;
// method in world class to run when game is over
public void gameOver()
{
    removeObjects(getObjects(null)); // removes all actors
    getBackground().setColor(Color.white);
    getBackground().fill(); // clears the background
    GreenfootImage message = new GreenfootImage("Game Over", 96, Color.black, Color.white); // creates the message image
    getBackground().drawImage(message, (getWidth()-message.getWidth())/2, (getHeight()-message.getHeight())/2); // draws message on background
    Greenfoot.stop(); // stops the execution of the scenario
}
You need to login to post a reply.