0% found this document useful (0 votes)
12 views9 pages

Disaster Survival

disaster

Uploaded by

Tarun Dwivedi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views9 pages

Disaster Survival

disaster

Uploaded by

Tarun Dwivedi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

DisasterSurvival (2) 4/26/24, 12:45 AM

/*
* Soham Dwivedi
* Period 2
* 4/23/24
* Working on:
* The final game project of the year. This project will encompass all
we have done
* throughout the year, and will be worked on for 5 weeks.
*/

import java.awt.Graphics;
import java.awt.Color;
import java.awt.Font;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Insets;

import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.CardLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;

import javax.swing.JButton;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JLabel;
import javax.swing.JSlider;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JCheckBox;

import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.AdjustmentEvent;
import javax.swing.event.ChangeEvent;

Page 1 of 9
DisasterSurvival (2) 4/26/24, 12:45 AM

import javax.swing.event.ChangeListener;

public class DisasterSurvival


{
public DisasterSurvival()
{
}

public static void main(String [] args)


{
DisasterSurvival ds = new DisasterSurvival();
ds.run();
}
// conventions, same for essentially every prog
public void run()
{
JFrame frame = new JFrame("DisasterSurvival");
frame.setSize( 800, 600);

frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setLocation(0,0);
frame.setResizable(false);
DisasterHolder dh = new DisasterHolder();

frame.getContentPane().add( dh );
frame.setVisible(true);
}
}
// holds the panels and adds them to CardLayout
class DisasterHolder extends JPanel
{
public DisasterHolder()
{

CardLayout cards = new CardLayout();


setLayout(cards);

StartPanel sp = new StartPanel(this, cards);


InstructionPanel ip = new InstructionPanel(this,
cards);
GamePanel gp = new GamePanel(this, cards);
add(sp, "start");
add(ip, "instructions");
add(gp, "game");
}
}
/*
* StartPanel holds all components and contains an actionPerformed
method to
* address button use. There is also a sliderHandler class, so that

Page 2 of 9
DisasterSurvival (2) 4/26/24, 12:45 AM

the difficulty
* can be stored in a variable, later used in the GamePanel. The field
vars
* are the components being used in handler classes or actionPerformed
methods.
* The StartPanel is a null layout.
*/
class StartPanel extends JPanel implements ActionListener,
ChangeListener
{
private DisasterHolder panCards; //card stuff
private CardLayout cards;
private int diff; //difficulty
private JSlider diffSlide; //difficulty slider so it can be
accessed by handlers
private JButton instruct; // same for these as above but for
// respective components
private JButton play;
private JButton exit;
//constructor initializes all components
public StartPanel(DisasterHolder panelCardsIn, CardLayout
cardsIn)
{
setLayout(null);
setBackground(Color.GREEN);
cards = cardsIn;
panCards = panelCardsIn;

Font font = new Font("Serif", Font.BOLD, 25);


Font font2 = new Font("Serif", Font.BOLD, 20);

exit = new JButton("Exit Game");


exit.setBounds(320, 10, 150, 50);
exit.setFont(font);
exit.addActionListener(this);
exit.setForeground(Color.ORANGE);
exit.setBackground(Color.BLACK);
exit.setOpaque(true);
exit.setBorder(null);

play = new JButton("Play the Game");


play.setBounds(300, 200, 200, 50);
play.setFont(font);
play.setForeground(Color.ORANGE);
play.setBackground(Color.BLACK);
play.setOpaque(true);
play.setBorder(null);

instruct = new JButton("Read Instructions");


instruct.setBounds(275, 325, 250, 50);

Page 3 of 9
DisasterSurvival (2) 4/26/24, 12:45 AM

instruct.setFont(font);
instruct.setForeground(Color.ORANGE);
instruct.setBackground(Color.BLACK);
instruct.setOpaque(true);
instruct.setBorder(null);

diffSlide = new JSlider(JSlider.HORIZONTAL, 1, 3,1);


diffSlide.setMajorTickSpacing(1);
diffSlide.addChangeListener(this);
diffSlide.setPaintTicks(true);
diffSlide.setPaintLabels(true);
diffSlide.setBounds(300, 450, 200, 50);

JLabel slideLabel = new JLabel("Choose Difficulty: 1,


2, 3");
slideLabel.setFont(font2);
slideLabel.setBounds(290, 490, 300, 50);

play.addActionListener(this);
instruct.addActionListener(this);

add(instruct);
add(diffSlide);
add(play);
add(slideLabel);
add(exit);
}
// decides which card to show, and exits when exit button is
pressed
public void actionPerformed(ActionEvent evt)
{
if(evt.getSource()== instruct)
cards.show(panCards, "instructions");
else if(evt.getSource() == play)
cards.show(panCards, "game");
else if(evt.getSource() == exit)
System.exit(1);
}
// stateChanged gets the change event of the slider, which
decides the
// difficulty.
public void stateChanged(ChangeEvent evt)
{
int val = diffSlide.getValue();
if(val==1)
diff = 1;
else if(val==2)
diff = 2;
else
diff = 3;

Page 4 of 9
DisasterSurvival (2) 4/26/24, 12:45 AM

}
/* paintComponent draws the background image and the title
image,
* calling the mthod getMyImage
*/
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Image bg = null;
String imgName2 = new String("BackgroundImage.png");
bg = getMyImage(imgName2, bg);
g.drawImage(bg, 0, 0, 800, 600, null);

Image title = null;


String imgName = new String("DisasterTitle.png");
title = getMyImage(imgName, title);
g.drawImage(title, 150, 100, 500 , 50, null);
}
/* getMyImage returns an image and accepts String imgNameIn
and Image image.
* It uses a try-catch block to read the image.
*/
public Image getMyImage(String imgNameIn, Image image)
{
File imageFile = new File(imgNameIn);
try
{
image = ImageIO.read(imageFile);
}
catch(IOException e)
{
System.err.println("\n\n\n" + imgNameIn + "
can't be found.");
e.printStackTrace();
}
return image;
}
}
/* the IntstructionPanel class contains 2 components:
* The text area, and the checkbox.
* Both are initialized and added to the panel, which is a border
layout.
* There is a handler class for the checkbox, which allows the user to
return back
* to the original panel.
*/
class InstructionPanel extends JPanel
{
private DisasterHolder panelCards; //panelCards is this
panel's card layout

Page 5 of 9
DisasterSurvival (2) 4/26/24, 12:45 AM

private CardLayout cards; // the cards that store other panels


// Constructor initalizes all components
public InstructionPanel(DisasterHolder panelCardsIn,
CardLayout cardsIn)
{
setLayout(new BorderLayout());
setBackground(Color.GREEN);
cards = cardsIn;
panelCards = panelCardsIn;
Font font = new Font("Serif", Font.BOLD, 20);

JTextArea welcome = new JTextArea("Welcome to Disaster


Survival!"+
" In this game, you will be tasked in finding
the right spots to"
+ " \"hide\" in, by clicking the button
corresponding to the right"
+ " spot in the image. Each disaster will be
different, and some"
+ " can be mixed up together (only on
difficulty 3)."
+ " Difficulty 1 gives you extra tries to get
the right answer."
+ " Difficulty 2 gives no extra tries.
Difficulty 3 mixes disasters,"
+ " making the answers harder to think of.
There is an answer key at the end,"
+ " so the overall learning experience is
better.", 10, 20);
welcome.setLineWrap(true);
welcome.setBounds(250, 75,500, 400);
welcome.setWrapStyleWord(true);
welcome.setEditable(false);
welcome.setMargin(new Insets(15, 15, 15, 15));
welcome.setFont(font);

JScrollPane scroll = new JScrollPane(welcome);


scroll.setBounds(150,75, 500,400);
add(scroll, BorderLayout.NORTH);

JCheckBox agreements = new JCheckBox("I understand the


directions. Take"
+ " me back to the start.");
agreements.setBounds(150, 500, 550, 30);
agreements.setBackground(Color.GREEN);
AgreementsHandler ah = new AgreementsHandler();
agreements.addActionListener(ah);
agreements.setFont(font);
add(agreements, BorderLayout.CENTER);

Page 6 of 9
DisasterSurvival (2) 4/26/24, 12:45 AM

}
//paintComponent draws the background image for this panel.
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Image bg = null;
String imgName2 = new String("BackgroundImage.png");
bg = getMyImage(imgName2, bg);
g.drawImage(bg, 0, 0, 800, 600, null);
}
//try-catch block reads image
public Image getMyImage(String imgNameIn, Image image)
{
File imageFile = new File(imgNameIn);
try
{
image = ImageIO.read(imageFile);
}
catch(IOException e)
{
System.err.println("\n\n\n" + imgNameIn + "
can't be found.");
e.printStackTrace();
}
return image;
}
//handles the check box
class AgreementsHandler implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
cards.show(panelCards, "start");
}
}
}
/* Game panel uses radio buttons and draws images to switch between
it's cards,
* allowing the user to well, play the game!
*/
class GamePanel extends JPanel implements ActionListener
{
private DisasterHolder panelCards; //used to switch between
cards inside GamePanel
private CardLayout cards; //holds all cards so they can be
accessed
private JButton exit; // allows user to exit the game, needs
to be FV so handlers
// can access
public GamePanel(DisasterHolder panelCardsIn, CardLayout
cardsIn)

Page 7 of 9
DisasterSurvival (2) 4/26/24, 12:45 AM

{
Font font = new Font("Serif", Font.BOLD, 20);
cards = cardsIn;
panelCards = panelCardsIn;
setLayout(new BorderLayout());
setBackground(Color.BLUE);

exit = new JButton("Exit Game");


exit.setBounds(320, 10, 150, 50);
exit.setFont(font);
exit.addActionListener(this);
exit.setForeground(Color.ORANGE);
exit.setBackground(Color.BLACK);
exit.setOpaque(true);
exit.setBorder(null);
exit.addActionListener(this);

add(exit, BorderLayout.NORTH);

JPanel radioButtonsHolder = new JPanel();


radioButtonsHolder.setLayout(new GridLayout(5, 1));
ButtonGroup bg = new ButtonGroup();

JRadioButton b1 = new JRadioButton ("1");


JRadioButton b2 = new JRadioButton ("2");
JRadioButton b3 = new JRadioButton ("3");
JRadioButton b4 = new JRadioButton ("4");
JRadioButton b5 = new JRadioButton ("5");

bg.add(b1);
bg.add(b2);
bg.add(b3);
bg.add(b4);
bg.add(b5);

radioButtonsHolder.add(b1);
radioButtonsHolder.add(b2);
radioButtonsHolder.add(b3);
radioButtonsHolder.add(b4);
radioButtonsHolder.add(b5);

radioButtonsHolder.setBackground(Color.ORANGE);
add(radioButtonsHolder, BorderLayout.WEST);
}
//actionPerformed currently only handles the exit button.
public void actionPerformed(ActionEvent evt)
{
if(evt.getSource() == exit)
System.exit(1);
}

Page 8 of 9
DisasterSurvival (2) 4/26/24, 12:45 AM

Page 9 of 9

You might also like