Hey all finshing up a java game here and I need to make a pop box come up at the
ID: 3838733 • Letter: H
Question
Hey all finshing up a java game here and I need to make a pop box come up at the end to ask the user for their name person’s name, their favorite number, and their email address
It also should check to make sure it is properly formatted. You should check to make sure it is correctly formatted.
Specifics:
Gather the person’s name.
Gather the person’s favorite number – should be a number.
Collect the individual’s email – should have an @ and the ". " operator.
Should pre-emptively check all those values as well as have exception handlers where
appropriate.
Should have a countdown clock that starts at 30 and counts down to 0.
The AI car should race the player until the clock hits zero.
The player object should be able to move using the arrow keys.
Ignore collision detection.
For any exception handlers, you should write any exceptions to a log file.
Driver.java
import javax.swing.JFrame;
public class Driver {
public static void main(String[] args) {
JFrame myFrame = new JFrame("Problem 2");
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// do something with a panel here...
MainPanel myPanel = new MainPanel();
myFrame.getContentPane().add(myPanel);
myFrame.pack();
myFrame.setVisible(true);
}
}
GameObject.java
import javax.swing.ImageIcon;
public abstract class GameObject {
protected int x;
protected int y;
protected String imagePath;
protected final int HEIGHT;
protected final int WIDTH;
public GameObject(int x, int y, String imagePath, int HEIGHT, int WIDTH)
{
this.x = x;
this.y = y;
this.imagePath = imagePath;
this.HEIGHT = HEIGHT;
this.WIDTH = WIDTH;
}
public abstract ImageIcon getImageIcon();
//{
//ImageIcon myIcon = new ImageIcon(imagePath);
//return myIcon;
//return new ImageIcon(imagePath);
//}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getHEIGHT() {
return HEIGHT;
}
public int getWIDTH() {
return WIDTH;
}
}
MainPanel.java
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.ImageIcon;
public class MainPanel extends JPanel {
Player myPlayer;
Player myOtherPlayer;
private int WIDTH = 1000;
private int HEIGHT = 1000;
private int WALLWIDTH = 100;
private int WALLHEIGHT = 100;
private ArrayList<Wall> walls = new ArrayList<Wall>();
Timer myTimer = new Timer(500, new timerListener());
JLabel myTimeLabel;
int time =1;
public MainPanel()
{
setPreferredSize(new Dimension(WIDTH,HEIGHT));
JLabel myLabel= new JLabel ("Game ends once 30 seconds is receahed:");
myLabel.setFont(new Font("Serif", Font.BOLD,32));
myLabel.setForeground(Color.WHITE);
myTimeLabel= new JLabel (Integer.toString(time));
myTimeLabel.setFont(new Font("Serif", Font.BOLD,32));
myTimer.start();
add(myLabel);
add(myTimeLabel);
myPlayer = new Player(100,100, "toad.png", KeyEvent.VK_UP, KeyEvent.VK_DOWN, KeyEvent.VK_LEFT, KeyEvent.VK_RIGHT,this, 50, 38);
myOtherPlayer = new Player(200,200, "toad.png", KeyEvent.VK_W, KeyEvent.VK_S, KeyEvent.VK_A, KeyEvent.VK_D,this, 50, 38);
createWalls();
}
public ArrayList<Wall> getWalls() {
return walls;
}
public void createWalls()
{
int j = 0;
for(int i = 0; i < HEIGHT/WALLHEIGHT; i++)
{
for(int k = 0; k < WIDTH/WALLWIDTH; k++)
{
if(i == 0 || i == (HEIGHT/WALLHEIGHT-1))
{
walls.add(new Wall(k*WALLWIDTH,j,"road.png", 100, 100));
}
}
j+=WALLHEIGHT;
}
}
private class timerListener implements ActionListener
{
public void actionPerformed(ActionEvent e) {
time++;
myTimeLabel.setText(Integer.toString(time));
myTimeLabel.setForeground(Color.WHITE);
if(time >= 30)
{
myTimer.stop();
}
repaint();
}
}
public void paintComponent(Graphics page)
{
super.paintComponent(page);
page.drawImage(myPlayer.getImageIcon().getImage(), myPlayer.getX(), myPlayer.getY(), null);
page.drawImage(myOtherPlayer.getImageIcon().getImage(), myOtherPlayer.getX(), myOtherPlayer.getY(), null);
for(int i = 0; i < walls.size(); i++)
{
page.drawImage(walls.get(i).getImageIcon().getImage(), walls.get(i).getX(), walls.get(i).getY(), null);
}
page.setFont(new Font("Arial", Font.PLAIN,32));
page.drawString("Player 1 Score: " + myPlayer.getScore(), 100, 800);
page.drawString("Player 2 Score: " + myOtherPlayer.getScore(), 100, 850);
if(time >= 30)
{
page.drawString("GAME OVER", WIDTH/2-100, HEIGHT/2);
}
}
}
Movement.java
public class Movement {
private int distanceLeft = 10;
private int distanceRight = 10;
public int getDistanceLeft() {
return distanceLeft;
}
public int getDistanceRight() {
return distanceRight;
}
}
Player.java
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import javax.swing.ImageIcon;
public class Player extends GameObject implements KeyListener{
//private int x;
//private int y;
//private ImageIcon myImage;
private int Up;
private int Down;
private int Left;
private int Right;
//private String imagePath;
private MainPanel myPanel;
private Movement myMovement = new Movement();
//private int HEIGHT = 38;
//private int WIDTH = 50;
private int score = 0;
public Player(int x, int y, String imagePath, int Up, int Down, int Left, int Right, MainPanel myPanel, int HEIGHT, int WIDTH)
{
super(x,y,imagePath, HEIGHT, WIDTH);
//this.x = x;
//this.y = y;
//this.imagePath = imagePath;
//myImage = new ImageIcon(imagePath);
this.Up = Up;
this.Down = Down;
this.Left = Left;
this.Right = Right;
this.myPanel = myPanel;
myPanel.addKeyListener(this);
myPanel.setFocusable(true);
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
//public ImageIcon getMyImage() {
// return myImage;
//}
public int getScore()
{
return score;
}
@Override
public void keyPressed(KeyEvent arg0) {
// TODO Auto-generated method stub
int key = arg0.getKeyCode();
if(key == Left)
{
x-=myMovement.getDistanceLeft();
if(checkWalls())
{
x+=10;
}
}
else if(key == Right)
{
x+=myMovement.getDistanceRight();
if(checkWalls())
{
x-=10;
}
}
myPanel.repaint();
}
public boolean checkWalls()
{
ArrayList walls = myPanel.getWalls();
for(int i = 0 ; i < walls.size(); i++)
{
if(areRectsColliding(x,x+HEIGHT,y,y+WIDTH,walls.get(i).getX(), walls.get(i).getX()+ walls.get(i).getHEIGHT(),
walls.get(i).getY(),walls.get(i).getY()+walls.get(i).getWIDTH()))
{
return true;
}
}
return false;
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
private boolean areRectsColliding(int r1TopLeftX, int r1BottomRightX,
int r1TopLeftY, int r1BottomRightY, int r2TopLeftX,
int r2BottomRightX, int r2TopLeftY, int r2BottomRightY) {
if (r1TopLeftX < r2BottomRightX && r1BottomRightX > r2TopLeftX
&& r1TopLeftY < r2BottomRightY && r1BottomRightY > r2TopLeftY) {
return true;
} else {
return false;
}
}
@Override
public ImageIcon getImageIcon() {
// TODO Auto-generated method stub
return new ImageIcon(imagePath);
}
}
Wall.java
import javax.swing.ImageIcon;
public class Wall extends GameObject{
//private int x;
//private int y;
//private String imagePath;
//private ImageIcon myImage;
//private int HEIGHT = 100;
//private int WIDTH = 100;
public Wall(int x, int y, String imagePath, int HEIGHT, int WIDTH)
{
super(x,y,imagePath, HEIGHT, WIDTH);
//this.x = x;
//this.y = y;
//this.imagePath = imagePath;
//myImage = new ImageIcon(imagePath);
}
@Override
public ImageIcon getImageIcon() {
// TODO Auto-generated method stub
return new ImageIcon(imagePath);
}
/*public int getX() {
return x;
}
public int getY() {
return y;
}*/
//public ImageIcon getImageIcon()
//{
// return myImage;
//}
/*
public int getHEIGHT() {
return HEIGHT;
}
public int getWIDTH() {
return
road.png
toad.png
Explanation / Answer
My modification are in MainPanel.java only
============================================================================
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;
public class MainPanel extends JPanel {
Player myPlayer;
String myPlayerName;
Player myOtherPlayer;
String myOtherPlayerName;
private int WIDTH = 1000;
private int HEIGHT = 1000;
private int WALLWIDTH = 100;
private int WALLHEIGHT = 100;
private ArrayList walls = new ArrayList();
private int MAXITEMS = 5;
private Timer timer;
int time=30;
public MainPanel()
{
timer = new Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
if(time>0){
time--;
repaint();
}
else{
JOptionPane.showMessageDialog(null, "Game is Over");
System.exit(0);
}
}
} );
myPlayerName = JOptionPane.showInputDialog("Enter player1 Name");
myOtherPlayerName= JOptionPane.showInputDialog("Enter player2 Name");
timer.start();
setPreferredSize(new Dimension(WIDTH,HEIGHT));
myPlayer = new Player(100,100, "toad.png", KeyEvent.VK_UP, KeyEvent.VK_DOWN, KeyEvent.VK_LEFT, KeyEvent.VK_RIGHT,this, 50, 38);
myOtherPlayer = new Player(200,200, "toad.png", KeyEvent.VK_W, KeyEvent.VK_S, KeyEvent.VK_A, KeyEvent.VK_D,this, 50, 38);
createWalls();
}
public ArrayList getWalls() {
return walls;
}
public void createWalls()
{
int j = 0;
for(int i = 0; i < HEIGHT/WALLHEIGHT; i++)
{
for(int k = 0; k < WIDTH/WALLWIDTH; k++)
{
if(i == 0 || i == (HEIGHT/WALLHEIGHT-1))
{
walls.add(new Wall(k*WALLWIDTH,j,"road.png", 100, 100));
}
}
j+=WALLHEIGHT;
}
}
public void paintComponent(Graphics page)
{
super.paintComponent(page);
page.drawImage(myPlayer.getImageIcon().getImage(), myPlayer.getX(), myPlayer.getY(), null);
page.drawImage(myOtherPlayer.getImageIcon().getImage(), myOtherPlayer.getX(), myOtherPlayer.getY(), null);
for(int i = 0; i < walls.size(); i++)
{
page.drawImage(((GameObject) walls.get(i)).getImageIcon().getImage(), ((GameObject) walls.get(i)).getX(), ((GameObject) walls.get(i)).getY(), null);
}
page.setFont(new Font("Arial", Font.PLAIN,32));
page.drawString("Player "+myPlayerName+" Score: " + myPlayer.getScore(), 100, 800);
page.drawString("Player "+myOtherPlayerName+" Score: " + myOtherPlayer.getScore(), 100, 850);
page.drawString("Count Down : " + time , 700, 800);
// //if(items.size() == 0)
// {
// page.drawString("GAME OVER", WIDTH/2-100, HEIGHT/2);
//
// }
}
}
===============================================================================
import javax.swing.JFrame;
public class Driver {
public static void main(String[] args) {
JFrame myFrame = new JFrame("Problem 2");
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// do something with a panel here...
MainPanel myPanel = new MainPanel();
myFrame.getContentPane().add(myPanel);
myFrame.pack();
myFrame.setVisible(true);
}
}
======================================================================
import javax.swing.ImageIcon;
public abstract class GameObject {
protected int x;
protected int y;
protected String imagePath;
protected final int HEIGHT;
protected final int WIDTH;
public GameObject(int x, int y, String imagePath, int HEIGHT, int WIDTH)
{
this.x = x;
this.y = y;
this.imagePath = imagePath;
this.HEIGHT = HEIGHT;
this.WIDTH = WIDTH;
}
public abstract ImageIcon getImageIcon();
//{
//ImageIcon myIcon = new ImageIcon(imagePath);
//return myIcon;
//return new ImageIcon(imagePath);
//}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getHEIGHT() {
return HEIGHT;
}
public int getWIDTH() {
return WIDTH;
}
}
============================================================
public class Movement {
private int distanceLeft = 10;
private int distanceRight = 10;
public int getDistanceLeft() {
return distanceLeft;
}
public int getDistanceRight() {
return distanceRight;
}
}
===================================================================
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import javax.swing.ImageIcon;
public class Player extends GameObject implements KeyListener{
//private int x;
//private int y;
//private ImageIcon myImage;
private int Up;
private int Down;
private int Left;
private int Right;
//private String imagePath;
private MainPanel myPanel;
private Movement myMovement = new Movement();
//private int HEIGHT = 38;
//private int WIDTH = 50;
private int score = 0;
public Player(int x, int y, String imagePath, int Up, int Down, int Left, int Right, MainPanel myPanel, int HEIGHT, int WIDTH)
{
super(x,y,imagePath, HEIGHT, WIDTH);
//this.x = x;
//this.y = y;
//this.imagePath = imagePath;
//myImage = new ImageIcon(imagePath);
this.Up = Up;
this.Down = Down;
this.Left = Left;
this.Right = Right;
this.myPanel = myPanel;
myPanel.addKeyListener(this);
myPanel.setFocusable(true);
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
//public ImageIcon getMyImage() {
// return myImage;
//}
public int getScore()
{
return score;
}
@Override
public void keyPressed(KeyEvent arg0) {
// TODO Auto-generated method stub
int key = arg0.getKeyCode();
if(key == Left)
{
x-=myMovement.getDistanceLeft();
if(checkWalls())
{
x+=10;
}
}
else if(key == Right)
{
x+=myMovement.getDistanceRight();
if(checkWalls())
{
x-=10;
}
}
myPanel.repaint();
}
public boolean checkWalls()
{
ArrayList walls = myPanel.getWalls();
for(int i = 0 ; i < walls.size(); i++)
{
if(areRectsColliding(x,x+HEIGHT,y,y+WIDTH,((GameObject) walls.get(i)).getX(), ((GameObject) walls.get(i)).getX()+ ((GameObject) walls.get(i)).getHEIGHT(),
((GameObject) walls.get(i)).getY(),((GameObject) walls.get(i)).getY()+((GameObject) walls.get(i)).getWIDTH()))
{
return true;
}
}
return false;
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
private boolean areRectsColliding(int r1TopLeftX, int r1BottomRightX,
int r1TopLeftY, int r1BottomRightY, int r2TopLeftX,
int r2BottomRightX, int r2TopLeftY, int r2BottomRightY) {
if (r1TopLeftX < r2BottomRightX && r1BottomRightX > r2TopLeftX
&& r1TopLeftY < r2BottomRightY && r1BottomRightY > r2TopLeftY) {
return true;
} else {
return false;
}
}
@Override
public ImageIcon getImageIcon() {
// TODO Auto-generated method stub
return new ImageIcon(imagePath);
}
}
=====================================================