//Warning:
Three 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.JTextArea;
import
javax.swing.JTextField;
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;
//game
screen
public
JPanel gamePanel;
public
JTextField gameTextField;
public
JButton gameButton;
public
JTextArea gameTextArea;
public
BufferedImage gameImage;
public
JLabel gameImageLabel;
//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 2");
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)
{
System.out.println("Error
1. Image file missing?");
}
}
public void
showGameScreen()
{
try
{
gamePanel =
new JPanel();
gameImage =
ImageIO.read(new File("00-forest.png"));
gameImageLabel =
new JLabel(new ImageIcon(gameImage));
gamePanel.add(gameImageLabel);
gameTextArea =
new JTextArea(5, 30);
gameTextArea.setText("Welcome to the game. There isn't much here yet. Enjoy.");
gameTextArea.setEditable(false);
gamePanel.add(gameTextArea);
gameTextField =
new JTextField(25);
gamePanel.add(gameTextField);
gameButton =
new JButton("Go");
gameButton.addActionListener(this);
gamePanel.add(gameButton);
gc.add(gamePanel);
revalidate(); //needed after replacing a component
repaint(); //needed after replacing a component
}
catch
(IOException e)
{
System.out.println("Error 2.
Image file missing?");
}
}
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)
{
System.out.println("Error
3. Image file missing?");
}
}
public static void main(String[] args)
{
GameFrame game = new GameFrame();
}
@Override
public void
actionPerformed(ActionEvent e)
{
if (e.getSource() == startButton)
{
gc.removeAll();
showGameScreen();
}
else if (e.getSource()
== gameButton)
{
gc.removeAll();
showEndScreen();
}
else if (e.getSource() == restartButton)
{
gc.removeAll();
showStartScreen();
}
}
}
|