Java – Swing Applications 02
TEXT-BASED ADVENTURE GAME IN A GUI

separator-blank.png

TEXT-BASED ADVENTURE GAME IN GUI – VERSION 2

 

VERSION 2 DESCRIPTION

 

This is a basic GUI program that shows three sections of a game:

  • a start screen,
  • a game screen (where the game will take place)
  • an end screen. 

A button allows you to go from one to the other. 

 

VERSION 2 FILES

 


The following three images are needed in your Eclipse project.

   gameOfGames.jpg

   gameOver.jpg

   00-forest.png

 

VERSION 2 CODE

 

//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();

             }

       }

}

 

separator-campeauIsAwesome.png