Ians Final Project
<< Ethan's Final Project | OldProjectsTrailIndex | Joseph's Final Project >>
Requires the boing.wav file found under Fr. Chris's Bounce under Final Projects
Click here for a working demonstration
CrazyBall.java
import java.awt.event.*; import java.applet.Applet; import java.awt.*; import javax.swing.*; public class CrazyBall extends Applet implements ActionListener, MouseMotionListener { Sound boing; Timer timer; Image virtualMem; Graphics gBuffer; Ball ball1, ball2, ball3, ball4; Player player1; Wall top,bottom,left,right; int appletWidth; int appletHeight; int frame; int lastFrame; Button myButton; int count; int score; // OUR CLASS IS A SUBCLASS OF APPLET /** * this init() method will be run at the beginning * and where we create the Listeners */ public void init() { boing = new Sound("boing.wav",this); ball1 = new Ball(10); ball2 = new Ball(10); ball3 = new Ball(10); ball4 = new Ball(10); player1 = new Player(10); top = new Wall(0,0,800,10); bottom = new Wall(0,470,800,10); left = new Wall(0,10,10,590); right = new Wall(630,10,10,590); appletWidth = 640; appletHeight = 480; score=0; timer=new Timer(1, this); lastFrame=appletWidth; virtualMem = createImage(appletWidth,appletHeight); gBuffer = virtualMem.getGraphics(); gBuffer.setColor(Color.white); gBuffer.fillRect(0,0,appletWidth,appletHeight); resize(640, 480); myButton= new Button("Do It Again"); //the class is its own button listener myButton.addActionListener (this); add(myButton); this.addMouseMotionListener(this); } public void paint(Graphics g) { //We draw using the object methods of // the graphics buffer, not what is // currently on the screen gBuffer.setColor(Color.white); gBuffer.fillRect(0,0,appletWidth,appletHeight);gBuffer.setColor(Color.black); gBuffer.setColor(Color.black); gBuffer.drawString("Score: "+score, 20, 20); ball1.draw(gBuffer); ball2.draw(gBuffer); ball3.draw(gBuffer); ball4.draw(gBuffer); player1.draw(gBuffer); top.draw(gBuffer); bottom.draw(gBuffer); left.draw(gBuffer); right.draw(gBuffer); //Now we send the result to the screen g.drawImage(virtualMem,0,0,this); } public void update(Graphics g) { paint(g); //get rid of flicker with this method } /** * ActionListener is an interface and * requires the actionPerformed() method * to be defined..in this case we * look for a restart button being pressed */ public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if (source == timer) { score++; frame++; frame%=100; ball1.move(); ball2.move(); ball3.move(); ball4.move(); if (top.touches(ball1) || bottom.touches(ball1)) { ball1.bounceVertical(); boing.play(); count++; } if (left.touches(ball1) || right.touches(ball1)) { ball1.bounceHorizontal(); boing.play(); count++; } if (top.touches(ball2) || bottom.touches(ball2)) { ball2.bounceVertical(); boing.play(); count++; } if (left.touches(ball2) || right.touches(ball2)) { ball2.bounceHorizontal(); boing.play(); count++; } if (top.touches(ball3) || bottom.touches(ball3)) { ball3.bounceVertical(); boing.play(); count++; } if (left.touches(ball3) || right.touches(ball3)) { ball3.bounceHorizontal(); boing.play(); count++; } if (top.touches(ball4) || bottom.touches(ball4)) { ball4.bounceVertical(); boing.play(); count++; } if (left.touches(ball4) || right.touches(ball4)) { ball4.bounceHorizontal(); boing.play(); count++; } if (player1.touches(ball1) || player1.touches(ball2) || player1.touches(ball3) || player1.touches(ball4)) { timer.stop(); myButton.setLabel("Play Again"); ball1.setVisable(false); ball2.setVisable(false); ball3.setVisable(false); ball4.setVisable(false); } } else {//restart button pressed if (timer.isRunning()) { timer.stop(); myButton.setLabel("Start"); ball1.setVisable(false); ball2.setVisable(false); ball3.setVisable(false); ball4.setVisable(false); } else { score=0; timer.start(); myButton.setLabel("Stop"); ball1=new Ball(10); ball1.setVisable(true); ball2=new Ball(10); ball2.setVisable(true); ball3=new Ball(10); ball3.setVisable(true); ball4=new Ball(10); ball4.setVisable(true); boing.play(); } } repaint(); } @Override public void mouseDragged(MouseEvent arg0) { } @Override public void mouseMoved(MouseEvent e) { player1.move(e.getX(), e.getY()); repaint(); } }
Sound.java
import java.applet.*; import java.net.URL; public class Sound { private AudioClip clip; public Sound(String fileName, Applet a) { URL soundToPlay = getClass().getResource(fileName); clip = a.getAudioClip(soundToPlay); } public void play() { clip.play(); } public void loop() { clip.loop(); } }
Wall.java
import java.awt.Color; import java.awt.Graphics; import java.awt.geom.Rectangle2D; public class Wall { private Rectangle2D.Double rect; private Color color; private int x,y,width,height; public Wall(int x, int y, int width, int height) { rect=new Rectangle2D.Double(x, y, width, height); this.x=x; this.y=y; this.width=width; this.height=height; setColor(Color.BLACK); } public boolean touches(Ball ball) { return rect.intersects(ball.area()); } public void draw(Graphics g) { Color c=g.getColor(); g.setColor(color); g.fillRect(x, y, width, height); g.setColor(c); } public void setColor(Color color) { this.color = color; } public Color getColor() { return color; } }
Player.java
import java.awt.Color; import java.awt.Graphics; public class Player { private int radius; private int x; private int y; public Player(int radius) { x=randInt(350,450); y=randInt(250,350); this.radius=radius; } public void move(int x, int y){ this.x = x; this.y = y; } public void draw(Graphics g) { Color c = g.getColor(); g.setColor(Color.RED); g.fillOval(x-radius, y-radius, 2*radius, 2*radius); g.setColor(c); } public int randInt(int min, int max) { return min+(int)((max-min)*Math.random()); } public boolean touches(Ball ball) { boolean result = false; if (((ball.getX()-x)*(ball.getX()-x))+((ball.getY()-y)*(ball.getY()-y)) <= 400) return true; return result; } }
Ball.java
import java.awt.Color; import java.awt.Graphics; import java.awt.geom.Rectangle2D; public class Ball { private int x; private int y; private int radius; private int rise; private int run; private boolean visable; public Ball(int radius) { x=randInt(350,450); y=randInt(250,350); this.radius=radius; rise=randInt(1,8); run=randInt(-8,8); setVisable(false); } public int randInt(int min, int max) { return min+(int)((max-min)*Math.random()); } public void move() { x+=run; y+=rise; } public void bounceVertical() { rise*=-1; } public void bounceHorizontal() { run*=-1; } public void draw(Graphics g) { if (visable){ Color c=g.getColor(); g.setColor(Color.BLUE); g.fillOval(x-radius, y-radius, 2*radius, 2*radius); g.setColor(c); } } public void setVisable(boolean visable) { this.visable = visable; } public boolean isVisable() { return visable; } public Rectangle2D area() { return new Rectangle2D.Double(x-radius, y-radius, 2*radius, 2*radius); } public void setRise(int rise) { this.rise=rise; } public void setRun(int run) { this.run=run; } public int getRise() { return rise; } public int getRun() { return run; } public void setX(int x) { this.x=x; } public void setY(int y) { this.y=y; } public int getX() { return x;//center x } public int getY() { return y;//center y } public int getRadius() { return radius; } }