import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import
javax.swing.JPanel;
public class CardboardQuestionPanel
extends JPanel implements
KeyListener
{
//datafields
boolean screen1 = true;
boolean screen2 = false;
boolean screen3 = false;
boolean screen4 = false;
boolean screen5 = false;
boolean screen6 = false;
boolean screen7 = false;
boolean screen8 = false;
public
CardboardQuestionPanel()
{
addKeyListener(this);
}
public void
addNotify() //this
method is needed to give keyboard focus to the panel...
{
super.addNotify();
requestFocus();
}
public void
paint(Graphics g)
{
super.paint(g); //required to repaint bg everytime
Graphics2D g2D = (Graphics2D) g;
//=================================================================================================
if (screen1 == true)
{
BufferedImage img = null;
try
{
img = ImageIO.read(new File("pizzaboxbyte.png")); //img in project folder
(Eclipse)
g2D.setColor(Color.white);
g2D.fillRect(0, 0, 500, 500); //background
g2D.setColor(Color.black);
g2D.drawString("Consider
the image below.", 170, 20);
g2D.drawImage(img, 65, 30, null);
g2D.drawString("Who
do you think is likely responsible for the bite taken our of the box?", 50,
443);
g2D.drawString("(Press
any key to continue)", 170, 458);
}
catch
(IOException e)
{
System.out.println("Error");
e.printStackTrace(); //Optional - Shows the location
of the error issue.
}
}
//=================================================================================================
else if (screen2 == true)
{
BufferedImage img = null;
try
{
img = ImageIO.read(new File("unknownGirl.JPG")); //img in project folder
(Eclipse)
g2D.setColor(Color.white);
g2D.fillRect(0, 0, 500, 500); //background
g2D.setColor(Color.black);
g2D.drawString("Was it
this girl? She seems pretty happy
with her pizza.", 84, 20);
g2D.drawImage(img, 30, 70, null);
g2D.drawString("(Press
any key to continue)", 170, 458);
}
catch
(IOException e)
{
System.out.println("Error");
e.printStackTrace(); //Optional - Shows the location
of the error issue.
}
}
//=================================================================================================
else if (screen3 == true)
{
BufferedImage img = null;
try
{
img = ImageIO.read(new File("JohnStewart.jpg")); //img in project folder
(Eclipse)
g2D.setColor(Color.white);
g2D.fillRect(0, 0, 500, 500); //background
g2D.setColor(Color.black);
g2D.drawString("Or
perhaps it was John Stewart. He's a
pretty funny guy!", 84, 20);
g2D.drawImage(img, 35, 70, null);
g2D.drawString("(Press
any key to continue)", 170, 458);
}
catch
(IOException e)
{
System.out.println("Error");
e.printStackTrace(); //Optional - Shows the location
of the error issue.
}
}
//=================================================================================================
else if (screen4 == true)
{
BufferedImage img = null;
try
{
img = ImageIO.read(new File("michealAngelo.png")); //img in project folder
(Eclipse)
g2D.setColor(Color.white);
g2D.fillRect(0, 0, 500, 500); //background
g2D.setColor(Color.black);
g2D.drawString("Was it
Michealangelo? He sure loves
pizza.", 120, 20);
g2D.drawImage(img, 67, 110, null);
g2D.drawString("(Press
any key to continue)", 170, 458);
}
catch
(IOException e)
{
System.out.println("Error");
e.printStackTrace(); //Optional - Shows the location
of the error issue.
}
}
//=================================================================================================
else if (screen5 == true)
{
BufferedImage img = null;
try
{
img = ImageIO.read(new File("pirateKingFram.jpg")); //img in project folder
(Eclipse)
g2D.setColor(Color.white);
g2D.fillRect(0, 0, 500, 500); //background
g2D.setColor(Color.black);
g2D.drawString("Or maybe
it was a Pirate Fram?", 120, 20);
g2D.drawImage(img, 39, 80, null);
g2D.drawString("(Press
any key to continue)", 170, 458);
}
catch
(IOException e)
{
System.out.println("Error");
e.printStackTrace(); //Optional - Shows the location
of the error issue.
}
}
//=================================================================================================
else if (screen6 == true)
{
g2D.drawString("Make your
choice.", 160, 20);
g2D.drawString("a)
Unknown girl", 160, 60);
g2D.drawString("b) John
Stewart", 160, 90);
g2D.drawString("c)
Michaelangelo", 160, 120);
g2D.drawString("d) Pirate
Fram", 160, 150);
g2D.drawString("e) None
of the above", 160, 180);
}
//=================================================================================================
else if (screen7 == true)
{
BufferedImage img = null;
try
{
img = ImageIO.read(new File("twins.png")); //img in project folder
(Eclipse)
g2D.setColor(Color.white);
g2D.fillRect(0, 0, 500, 500); //background
g2D.setColor(Color.black);
g2D.drawString("You're
wrong. It was the mysterious Fram
twins pictured here.", 60, 20);
g2D.drawImage(img, 25, 80, null);
}
catch
(IOException e)
{
System.out.println("Error");
e.printStackTrace(); //Optional - Shows the location
of the error issue.
}
}
//=================================================================================================
else if (screen8 == true)
{
BufferedImage img = null;
try
{
img = ImageIO.read(new File("twins.png")); //img in project folder
(Eclipse)
g2D.setColor(Color.white);
g2D.fillRect(0, 0, 500, 500); //background
g2D.setColor(Color.black);
g2D.drawString("You're
right. It was the mysterious Fram
twins pictured here.", 60, 20);
g2D.drawImage(img, 25, 80, null);
}
catch
(IOException e)
{
System.out.println("Error");
e.printStackTrace(); //Optional - Shows the location
of the error issue.
}
}
//=================================================================================================
}
public void
keyTyped(KeyEvent e)
{
}
public void
keyPressed(KeyEvent e)
{
}
public void
keyReleased(KeyEvent e)
{
if (screen1 == true)
{
//move to screen 2
screen2 = true;
screen1 = false;
repaint();
}
else if (screen2 == true)
{
//move to screen 3
screen3 = true;
screen2 = false;
repaint();
}
else if (screen3 == true)
{
//move to screen 4
screen4 = true;
screen3 = false;
repaint();
}
else if (screen4 == true)
{
//move to screen 5
screen5 = true;
screen4 = false;
repaint();
}
else if (screen5 == true)
{
//move
to screen 6
screen6 = true;
screen5 = false;
repaint();
}
else if (screen6 == true)
{
System.out.println(e.getKeyChar());
if
(e.getKeyChar() == 'a' ||
e.getKeyChar() == 'b' || e.getKeyChar()
== 'c' || e.getKeyChar() == 'd')
{
screen7 = true; //go to YOU'RE WRONG screen
screen6 = false;
repaint();
}
if
(e.getKeyChar() == 'e')
{
screen8 = true; //go
to YOU'RE RIGHT screen
screen6 = false;
repaint();
}
}
}
}
|