In: Computer Science
In Java
Deliverables
Requirements
Sample Output:
Code to Copy:
Intro.java
// import the necessary package.
import javax.swing.*;
import java.awt.event.*;
// Declare the class.
public class Intro extends JPanel {
static JFrame frame;
// Declare the constructor.
public Intro() {
initComponents();
}
@SuppressWarnings("unchecked")
// declare method.
private void initComponents() {
jLabel1 = new JLabel();
mainMap = new JButton();
options = new JButton();
instructions = new JButton();
credits = new JButton();
// Display text message.
jLabel1.setText("Welcome to
Game");
// Set the text.
mainMap.setText("Main Map");
// Action listener to the main
Map
mainMap.addActionListener(new
ActionListener() {
public void
actionPerformed(ActionEvent evt) {
mainMapActionPerformed(evt);
}
});
// set the text and call the action
listener
options.setText("Options");
options.addActionListener(new
ActionListener() {
public void
actionPerformed(ActionEvent evt) {
optionsActionPerformed(evt);
}
});
// set the text and call the action
listener
instructions.setText("Instructions");
instructions.addActionListener(new
ActionListener() {
public void
actionPerformed(ActionEvent evt) {
instructionsActionPerformed(evt);
}
});
// set the text and call the action
listener
credits.setText("Credits");
credits.addActionListener(new
ActionListener() {
public void
actionPerformed(ActionEvent evt) {
creditsActionPerformed(evt);
}
});
// Set the layout of the
panel.
GroupLayout layout = new
GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup().addGap(157, 157,
157).addComponent(jLabel1))
.addGroup(layout.createSequentialGroup().addContainerGap().addComponent(mainMap)
.addGap(37, 37,
37).addComponent(options).addGap(35, 35, 35)
.addComponent(instructions).addGap(39, 39,
39).addComponent(credits)))
.addContainerGap(GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)));
layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup().addGap(45, 45,
45).addComponent(jLabel1).addGap(52, 52, 52)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(mainMap)
.addComponent(options).addComponent(instructions).addComponent(credits))
.addContainerGap(36, Short.MAX_VALUE)));
}
// Definition of the method.
private void mainMapActionPerformed(ActionEvent evt)
{
Intro.frame.dispose();
new MainMap(Intro.frame);
}
// definition of the method.
private void creditsActionPerformed(ActionEvent evt)
{
Intro.frame.dispose();
new CreditsPanel(Intro.frame);
}
// definition of the method.
private void instructionsActionPerformed(ActionEvent
evt) {
Intro.frame.dispose();
new
InstructionsPanel(Intro.frame);
}
// definition of the method.
private void optionsActionPerformed(ActionEvent evt)
{
Intro.frame.dispose();
new
OptionsPanel(Intro.frame);
}
// Start the main method.
public static void main(String[] args) {
frame = new JFrame();
frame.setContentPane(new
Intro());
frame.setSize(500, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setTitle("Game
Introduction");
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
// declare variables.
private JButton credits;
private JButton instructions;
private JLabel jLabel1;
private JButton mainMap;
private JButton options;
}
MainMap.java
//import the necessary package.
import javax.swing.*;
import java.awt.event.*;
//Declare the class.
public class MainMap extends JPanel {
JFrame introScreen;
JFrame frame;
// Declare the constructor.
public MainMap(JFrame introScreen) {
this.introScreen =
introScreen;
frame = new JFrame();
frame.setContentPane(this);
frame.setSize(500, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setTitle("Main Game");
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
initComponents();
}
@SuppressWarnings("unchecked")
// declare method.
private void initComponents() {
jButton1 = new JButton();
jButton2 = new JButton();
jButton3 = new JButton();
jButton5 = new JButton();
jButton6 = new JButton();
// Set the text and call the
action listener
jButton1.setText("Game 1");
jButton1.addActionListener(new
ActionListener() {
public void
actionPerformed(ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
// Set the text and call the action
listener
jButton2.setText("Game 2");
jButton2.addActionListener(new
ActionListener() {
public void
actionPerformed(ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
// Set the text and call the
action listener
jButton3.setText("Game 3");
jButton3.addActionListener(new
ActionListener() {
public void
actionPerformed(ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
// Set the text and call the
action listener
jButton5.setText("Go Home");
jButton5.addActionListener(new
ActionListener() {
public void
actionPerformed(ActionEvent evt) {
jButton5ActionPerformed(evt);
}
});
// Set the text and call the
action listener
jButton6.setText("Game
Over");
jButton6.addActionListener(new
ActionListener() {
public void
actionPerformed(ActionEvent evt) {
jButton6ActionPerformed(evt);
}
});
// Set the layout of the
panel.
GroupLayout layout = new
GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup().addContainerGap().addComponent(jButton1)
.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup().addComponent(jButton5).addGap(35,
35, 35)
.addComponent(jButton6).addGap(0, 0,
Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup().addComponent(jButton2).addGap(18,
18, 18)
.addComponent(jButton3).addGap(18, 18, 18)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED,
GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addContainerGap()));
layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup().addGap(80, 80, 80)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(jButton1).addComponent(jButton2).addComponent(jButton3))
.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(jButton5)
.addComponent(jButton6)).addContainerGap(44,
Short.MAX_VALUE)));
}
// Definition of the method.
private void jButton1ActionPerformed(ActionEvent evt)
{
frame.setVisible(false);
new Game1(frame);
}
// Definition of the method.
private void jButton5ActionPerformed(ActionEvent evt)
{
frame.dispose();
introScreen.setVisible(true);
}
// Definition of the method.
private void jButton2ActionPerformed(ActionEvent evt)
{
frame.setVisible(false);
new Game2(frame);
}
// Definition of the method.
private void jButton3ActionPerformed(ActionEvent evt)
{
frame.setVisible(false);
new Game3(frame);
}
// Definition of the method.
private void jButton6ActionPerformed(ActionEvent evt)
{
frame.setVisible(false);
new GameOver(frame);
}
// Declare variables.
private JButton jButton1;
private JButton jButton2;
private JButton jButton3;
private JButton jButton5;
private JButton jButton6;
}
Game1.java
//import the necessary package.
import javax.swing.*;
import java.awt.event.*;
public class Game1 extends JPanel {
JFrame mainFrame;
JFrame frame;
// Declare the constructor.
public Game1(JFrame mainFrame) {
this.mainFrame = mainFrame;
frame = new JFrame();
frame.setContentPane(this);
frame.setSize(300, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setTitle("Game 1");
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
initComponents();
}
@SuppressWarnings("unchecked")
private void initComponents() {
jButton1 = new JButton();
jLabel1 = new JLabel();
// Set the text and call the action
listener
jButton1.setText("Main Map");
jButton1.addActionListener(new
ActionListener() {
public void
actionPerformed(ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jLabel1.setText("Game 1 will be here");
// Set the layout of the
panel.
GroupLayout layout = new
GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup().addGap(58, 58, 58)
.addComponent(jButton1))
.addGroup(layout.createSequentialGroup().addGap(47, 47,
47).addComponent(jLabel1)))
.addContainerGap(260, Short.MAX_VALUE)));
layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup().addGap(44, 44,
44).addComponent(jLabel1).addGap(33, 33, 33)
.addComponent(jButton1).addContainerGap(186,
Short.MAX_VALUE)));
}
// Definition of the method.
private void jButton1ActionPerformed(ActionEvent evt)
{
frame.dispose();
mainFrame.setVisible(true);
}
// Declare variables.
private JButton jButton1;
private JLabel jLabel1;
}
Game2.java
//import the necessary package.
import javax.swing.*;
import java.awt.event.*;
public class Game2 extends JPanel {
JFrame mainFrame;
JFrame frame;
// Declare the constructor.
public Game2(JFrame mainFrame) {
this.mainFrame = mainFrame;
frame = new JFrame();
frame.setContentPane(this);
frame.setSize(300, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setTitle("Game 2");
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
initComponents();
}
@SuppressWarnings("unchecked")
private void initComponents() {
jButton1 = new JButton();
jLabel1 = new JLabel();
// Set the text and call the
action listener
jButton1.setText("Main Map");
jButton1.addActionListener(new
ActionListener() {
public void
actionPerformed(ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jLabel1.setText("Game 2 will be
here");
// Set the layout of the
panel.
GroupLayout layout = new
GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup().addGap(58, 58, 58)
.addComponent(jButton1))
.addGroup(layout.createSequentialGroup().addGap(47, 47,
47).addComponent(jLabel1)))
.addContainerGap(260, Short.MAX_VALUE)));
layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup().addGap(44, 44,
44).addComponent(jLabel1).addGap(33, 33, 33)
.addComponent(jButton1).addContainerGap(186,
Short.MAX_VALUE)));
}
// Definition of the method.
private void jButton1ActionPerformed(ActionEvent evt)
{
frame.dispose();
mainFrame.setVisible(true);
}
// Declare variables.
private JButton jButton1;
private JLabel jLabel1;
}
Game3.java
//import the necessary package.
import javax.swing.*;
import java.awt.event.*;
public class Game3 extends JPanel {
JFrame mainFrame;
JFrame frame;
// Declare the constructor.
public Game3(JFrame mainFrame) {
this.mainFrame = mainFrame;
frame = new JFrame();
frame.setContentPane(this);
frame.setSize(300, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setTitle("Game 3");
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
initComponents();
}
private void initComponents() {
jButton1 = new JButton();
jLabel1 = new JLabel();
// Set the text and call the
action listener
jButton1.setText("Main Map");
jButton1.addActionListener(new
ActionListener() {
public void
actionPerformed(ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jLabel1.setText("Game 3 will be here");
// Set the layout of the
panel.
GroupLayout layout = new
GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup().addGap(58, 58, 58)
.addComponent(jButton1))
.addGroup(layout.createSequentialGroup().addGap(47, 47,
47).addComponent(jLabel1)))
.addContainerGap(260, Short.MAX_VALUE)));
layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup().addGap(44, 44,
44).addComponent(jLabel1).addGap(33, 33, 33)
.addComponent(jButton1).addContainerGap(186,
Short.MAX_VALUE)));
}
// Definition of the method.
private void jButton1ActionPerformed(ActionEvent evt)
{
frame.dispose();
mainFrame.setVisible(true);
}
// Declare variables.
private JButton jButton1;
private JLabel jLabel1;
}
GameOver.java
//import the necessary package.
import javax.swing.*;
import java.awt.event.*;
public class GameOver extends JPanel {
JFrame mainFrame;
JFrame frame;
// Declare the constructor.
public GameOver(JFrame mainFrame) {
this.mainFrame = mainFrame;
frame = new JFrame();
frame.setContentPane(this);
frame.setSize(300, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setTitle("Game 1");
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
initComponents();
}
private void initComponents() {
jButton1 = new JButton();
jLabel1 = new JLabel();
// Set the text and call the action
listener
jButton1.setText("Main Map");
jButton1.addActionListener(new
ActionListener() {
public void
actionPerformed(ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jLabel1.setText("Game Over Panel");
// Set the layout of the
panel.
GroupLayout layout = new
GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup().addGap(58, 58, 58)
.addComponent(jButton1))
.addGroup(layout.createSequentialGroup().addGap(47, 47,
47).addComponent(jLabel1)))
.addContainerGap(265, Short.MAX_VALUE)));
layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup().addGap(44, 44,
44).addComponent(jLabel1).addGap(33, 33, 33)
.addComponent(jButton1).addContainerGap(186,
Short.MAX_VALUE)));
}
// Definition of the method.
private void jButton1ActionPerformed(ActionEvent evt)
{
frame.dispose();
mainFrame.setVisible(true);
}
// Declare variables.
private JButton jButton1;
private JLabel jLabel1;
}
OptionsPanel.Java
//import the necessary package.
import javax.swing.*;
import java.awt.event.*;
public class OptionsPanel extends JPanel {
JFrame introScreen;
JFrame frame;
// Declare the constructor.
public OptionsPanel(JFrame introScreen) {
this.introScreen =
introScreen;
frame = new JFrame();
frame.setContentPane(this);
frame.setSize(300, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setTitle("Game
Options");
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
initComponents();
}
private void initComponents() {
jLabel1 = new JLabel();
jButton1 = new JButton();
jLabel1.setText("Options Screen");
// Set the text and call the
action listener
jButton1.setText("Go Home");
jButton1.addActionListener(new
ActionListener() {
public void
actionPerformed(ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
// Set the layout of the
panel.
GroupLayout layout = new
GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(
layout.createSequentialGroup().addGap(99, 99, 99)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup().addGap(15, 15, 15)
.addComponent(jLabel1))
.addComponent(jButton1))
.addContainerGap(213, Short.MAX_VALUE)));
layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup().addGap(60, 60,
60).addComponent(jLabel1).addGap(18, 18, 18)
.addComponent(jButton1).addContainerGap(185,
Short.MAX_VALUE)));
}
// Definition of the method.
private void jButton1ActionPerformed(ActionEvent evt)
{
frame.dispose();
introScreen.setVisible(true);
}
// Declare variables.
private JButton jButton1;
private JLabel jLabel1;
}
InstructionsPanel.java
//import the necessary package.
import javax.swing.*;
import java.awt.event.*;
public class InstructionsPanel extends JPanel {
JFrame introScreen;
JFrame frame;
// Declare the constructor.
public InstructionsPanel(JFrame introScreen) {
this.introScreen =
introScreen;
frame = new JFrame();
frame.setContentPane(this);
frame.setSize(300, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setTitle("Game
Instructions");
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
initComponents();
}
private void initComponents() {
jButton1 = new JButton();
jLabel1 = new JLabel();
// Set the text and call the
action listener
jButton1.setText("Go Home");
jButton1.addActionListener(new
ActionListener() {
public void
actionPerformed(ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jLabel1.setText("Instructions");
// Set the layout of the
panel.
GroupLayout layout = new
GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup().addGap(97, 97, 97)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(jButton1).addGroup(layout.createSequentialGroup()
.addGap(19, 19, 19).addComponent(jLabel1)))
.addContainerGap(100, Short.MAX_VALUE)));
layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup().addGap(55, 55,
55).addComponent(jLabel1).addGap(42, 42, 42)
.addComponent(jButton1).addContainerGap(69,
Short.MAX_VALUE)));
}
// Definition of the method.
private void jButton1ActionPerformed(ActionEvent evt)
{
frame.dispose();
introScreen.setVisible(true);
}
// Declare variables.
private JButton jButton1;
private JLabel jLabel1;
}
CreditsPanel.java
//import the necessary package.
import javax.swing.*;
import java.awt.event.*;
public class CreditsPanel extends JPanel {
JFrame introScreen;
JFrame frame;
// Declare the constructor.
public CreditsPanel(JFrame introScreen) {
this.introScreen =
introScreen;
frame = new JFrame();
frame.setContentPane(this);
frame.setSize(300, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setTitle("Game
Credits");
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
initComponents();
}
@SuppressWarnings("unchecked")
private void initComponents() {
jLabel1 = new JLabel();
jButton1 = new JButton();
jLabel1.setText(" Game
Credits");
// Set the text and call the action
listener
jButton1.setText("Go Home");
jButton1.addActionListener(new
ActionListener() {
public void
actionPerformed(ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
// Set the layout of the
panel.
GroupLayout layout = new
GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup().addGap(109, 109,
109)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
.addComponent(jLabel1,
GroupLayout.PREFERRED_SIZE, 82,
GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1))
.addContainerGap(91, Short.MAX_VALUE)));
layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup().addGap(37, 37, 37)
.addComponent(jLabel1, GroupLayout.PREFERRED_SIZE, 44,
GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18).addComponent(jButton1).addContainerGap(44,
Short.MAX_VALUE)));
}
// Definition of the method.
private void jButton1ActionPerformed(ActionEvent evt)
{
frame.dispose();
introScreen.setVisible(true);
}
// Declare variables.
private JButton jButton1;
private JLabel jLabel1;
}