//Warning: Seven images are required.
//
//Known issue: There is no way to get to the Game
Over screen at
//the moment.
This can easily be added.
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;
//game
state
public
GameState state;
//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 3");
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 err)
{
System.out.println("Error
1. Image file missing?");
}
}
public void showGameScreen()
{
try
{
state = new
GameState();
gamePanel = new JPanel();
gameImage = ImageIO.read(new File(state.getImageFileName()));
gameImageLabel = new JLabel(new ImageIcon(gameImage));
gamePanel.add(gameImageLabel);
gameTextArea = new JTextArea(5, 30);
gameTextArea.setText(state.getDescription());
gameTextArea.setLineWrap(true); //make
text wraps
gameTextArea.setWrapStyleWord(true); //wrap
by word
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 err)
{
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 err)
{
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) //GO
button
{
try
{
//We simply need to update the information inside
each
//component.
No need to change the GUI around.
//1-Update the state.
String
data = gameTextField.getText();
state.updateState(data);
//2-Update the image.
gameImage =
ImageIO.read(new File(state.getImageFileName()));
gameImageLabel.setIcon(new
ImageIcon(gameImage));
//gameImageLabel = new JLabel(new
ImageIcon(gameImage));
//3-Update the text area
gameTextArea.setText(state.getDescription());
}
catch
(IOException err)
{
System.out.println("Error 4.
Image file missing?");
}
}
else if (e.getSource() == restartButton)
{
gc.removeAll();
showStartScreen();
}
}
}
|