//Warning: Two
images are required.
import java.awt.Container;
import
java.awt.event.ActionEvent;
import
java.awt.event.ActionListener;
import
java.awt.image.BufferedImage;
import java.io.File;
import
java.io.IOException;
import
javax.imageio.ImageIO;
import
javax.swing.JButton;
import
javax.swing.JFrame;
import
javax.swing.JLabel;
import
javax.swing.JPanel;
import
javax.swing.ImageIcon; //I had to add
this import manually. Why?
public class GameFrame extends JFrame implements ActionListener
{
public Container gc;
//start screen
public JPanel startPanel;
public JButton startButton;
public BufferedImage startImage;
public JLabel startImageLabel;
//end screen
public JPanel endPanel;
public JButton restartButton;
public BufferedImage gameOverImage;
public JLabel gameOverImageLabel;
public GameFrame()
{
gc = this.getContentPane();
showStartScreen();
this.setTitle("Game of
Games - Version 1");
this.setSize(500,500);
this.setVisible(true);
}
public void
showStartScreen()
{
try
{
startPanel = new JPanel();
startImage = ImageIO.read(new File("gameOfGames.jpg"));
startImageLabel = new JLabel(new ImageIcon(startImage));
startPanel.add(startImageLabel);
startButton = new JButton("Start");
startButton.addActionListener(this);
startPanel.add(startButton);
gc.add(startPanel);
revalidate(); //needed after replacing a
component
repaint(); //needed after
replacing a component
}
catch (IOException e)
{}
}
public void showEndScreen()
{
try
{
endPanel = new JPanel();
gameOverImage = ImageIO.read(new File("gameOver.jpg"));
gameOverImageLabel = new JLabel(new ImageIcon(gameOverImage));
endPanel.add(gameOverImageLabel);
restartButton = new JButton("Restart");
restartButton.addActionListener(this);
endPanel.add(restartButton);
gc.add(endPanel);
revalidate(); //needed after replacing a
component
repaint(); //needed after replacing a
component
}
catch (IOException e)
{}
}
public static void main(String[] args)
{
GameFrame game = new GameFrame();
}
@Override
public void
actionPerformed(ActionEvent e)
{
if (e.getSource() == startButton)
{
gc.removeAll();
showEndScreen();
}
else if (e.getSource() == restartButton)
{
gc.removeAll();
showStartScreen();
}
}
}
|