Car Crash

This is a silly little Java app that I wrote for a Java class. It is two beetles (the car) that I drew myself using simple shapes driving around till they crash with a big explosion.

(Chadwick is my maiden name)

[code lang=”java”]
/**************************************************************************
* Name: Cynthia Chadwick FileName: Chadwick_Lab6/MainPanel.java
* CS 162 – Fall 2010 Lab #6 – Project 8.7 – pg 478
* Mon, Wed, 9:00am – 10:20am
* Date: 11/8/2010
*
* Program Name: Car Crash
* Program Description: Two cars move across the screen, when the first car
* reaches the end it turns around and crashes into
* the second car, resulting in a cartoon bang.
* Output – A pretty picture with animated cars and animated clouds
* Input – No outside input, a timer that runs the animation
* Process – MainPanel
* MainPanel : Constructor
* Creates Car objects : ArrayList<Car> Cars
* Creates Cloud objects : ArrayList<Cloud> Clouds
* Create and start timers
* paintComponent : method
* paints drawBackground : method
* paints Cars
* paints checkExplosion : method
* checkExplosion : method
* checks to see if the cars have collided
* if true drawExplosion
* drawExplosion : method
* fills a polygon
* draws a string
* draw a polygon
* drawBackground : method
* draws the background
* draws the clouds
* Listeners for cars and clouds
**************************************************************************/
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.ArrayList;
import javax.swing.*;
public class MainPanel extends JPanel
{
private Timer timerCarA, timerCarB, timeClouds;
private ArrayList<Car> Cars;
private Graphics2D g;
private ArrayList<Cloud> Clouds;
/**********************************************************************
* MainPanel Constructor
* Create Cars, Clouds, and set timers
**********************************************************************/
public MainPanel()
{
Cars = new ArrayList<Car>();
Cars.add(new Car(0, false, Color.blue));
Cars.add(new Car(-190, false, Color.red));

Clouds = new ArrayList<Cloud>();
Clouds.add(new Cloud(425, 50, 10, 10));
Clouds.add(new Cloud(750, 100, 20, 10));
Clouds.add(new Cloud(150, 200, 10, 5));

timeClouds = new Timer(45, new RepaintClouds());
timerCarA = new Timer(15, new RepaintCarA());
timerCarB = new Timer(45, new RepaintCarB());
timeClouds.start();
timerCarA.start();
timerCarB.start();
}
/**********************************************************************
* paintComponent paints everything to the screen
**********************************************************************/
public void paintComponent(Graphics page)
{
super.paintComponents(page);
g = (Graphics2D) page;
g.setBackground(Color.cyan);

drawBackground();

Cars.get(0).draw(g);
Cars.get(1).draw(g);

checkExplosion();
}
/**********************************************************************
* Check if there was explosion
**********************************************************************/
private void checkExplosion()
{
if(Cars.get(0).getRight() && !Cars.get(1).getRight())
{
if(Cars.get(0).getX() == Cars.get(1).getX()+180)
{
timerCarA.stop();
timerCarB.stop();
drawExplosion(Cars.get(0).getX(), Cars.get(0).getY());
}
}
else if(Cars.get(1).getRight() && !Cars.get(0).getRight())
{
if(Cars.get(1).getX() == Cars.get(0).getX()+180)
{
timerCarA.stop();
timerCarB.stop();
drawExplosion(Cars.get(1).getX(), Cars.get(1).getY());
}
}
else if(Cars.get(1).getRight() && Cars.get(0).getRight())
{
if(Cars.get(0).getX()+180 == Cars.get(1).getX())
{
timerCarA.stop();
timerCarB.stop();
drawExplosion(Cars.get(0).getX(), Cars.get(0).getY());
}
}
else
{
if(Cars.get(0).getX() == Cars.get(1).getX()+180)
{
timerCarA.stop();
timerCarB.stop();
drawExplosion(Cars.get(0).getX(), Cars.get(0).getY());
}
}
}
/**********************************************************************
* Draw an explosion
**********************************************************************/
private void drawExplosion(int x, int y)
{
Polygon p = new Polygon();
p.addPoint(x-50, y-350);
p.addPoint(x-50, y-250);
p.addPoint(x-200, y-350);
p.addPoint(x-150, y-200);
p.addPoint(x-250, y-250);
p.addPoint(x-150, y-100);
p.addPoint(x-300, y-50);
p.addPoint(x-150, y);
p.addPoint(x-250, y+100);
p.addPoint(x-150, y+50);
p.addPoint(x-250, y+250);
p.addPoint(x-150, y+150);
p.addPoint(x-150, y+300);
p.addPoint(x-50, y+200);
p.addPoint(x, y+300);
p.addPoint(x+50, y+150);
p.addPoint(x+150, y+250);
p.addPoint(x+100, y+50);
p.addPoint(x+250, y+150);
p.addPoint(x+200, y);
p.addPoint(x+300, y-50);
p.addPoint(x+250, y-100);
p.addPoint(x+300, y-200);
p.addPoint(x+150, y-100);
p.addPoint(x+200, y-300);
p.addPoint(x+100, y-150);
p.addPoint(x+100, y-350);
p.addPoint(x+50, y-200);
p.addPoint(x-50, y-350);

g.setColor(Color.white);
g.fill(p);

g.setColor(Color.black);
Font font = new Font("Serif", Font.BOLD, 96);
g.setFont(font);
g.drawString("Bang!", x-100, y+25);
g.draw(p);
}
/**********************************************************************
* Draw the background
**********************************************************************/
private void drawBackground()
{
Shape sky = new Rectangle2D.Float(0, 0, 800, 600);
g.setColor(Color.blue);
g.fill(sky);

Shape grass = new Arc2D.Float(-150, 300, 450, 600, 0, 180, Arc2D.CHORD);
Shape grassA = new Arc2D.Float(100, 300, 500, 600, 0, 180, Arc2D.CHORD);
Shape grassB = new Arc2D.Float(350, 300, 700, 600, 0, 180, Arc2D.CHORD);
g.setColor(Color.green);
g.fill(grass);
g.fill(grassA);
g.fill(grassB);

Shape road = new Rectangle2D.Float(0, 400, 800, 100);
g.setColor(Color.gray);
g.fill(road);

Shape roadLineWhiteA = new Rectangle2D.Float(0, 405, 800, 2);
Shape roadLineWhiteB = new Rectangle2D.Float(0, 493, 800, 2);
g.setColor(Color.white);
g.fill(roadLineWhiteA);
g.fill(roadLineWhiteB);
Shape roadLineYellowA = new Rectangle2D.Float(-1, 445, 802, 5);
g.setColor(Color.yellow);
g.draw(roadLineYellowA);

Clouds.get(0).draw(g);
Clouds.get(1).draw(g);
Clouds.get(2).draw(g);
}
/**********************************************************************
* Listener to move Car A
**********************************************************************/
class RepaintCarA implements ActionListener
{
public void actionPerformed(ActionEvent arg0)
{
if(Cars.get(0).getRight())
{
Cars.get(0).setX(Cars.get(0).getX()-1);
Cars.get(0).addCount(1);
} else {
Cars.get(0).setX(Cars.get(0).getX()+1);
Cars.get(0).addCount(-1);
}

repaint();
}
}
/**********************************************************************
* Listener to move Car B
**********************************************************************/
class RepaintCarB implements ActionListener
{
public void actionPerformed(ActionEvent arg0)
{
if(Cars.get(1).getRight())
{
Cars.get(1).setX(Cars.get(1).getX()-1);
Cars.get(1).addCount(1);
} else {
Cars.get(1).setX(Cars.get(1).getX()+1);
Cars.get(1).addCount(-1);
}
repaint();
}
}
/**********************************************************************
* Listener to move the Clouds
**********************************************************************/
class RepaintClouds implements ActionListener
{
public void actionPerformed(ActionEvent arg0)
{
for(int a=0; a < Clouds.size(); a++)
{
if(Clouds.get(a).getX() == -200)
{
Clouds.get(a).setX(900);
} else {
Clouds.get(a).setX(Clouds.get(a).getX()-1);
}
}
repaint();
}
}
}
[/code]