Ball Applet

i would like to do that the Ball object can

move inside the applet area and

bounce back when boundary is reached.

When the mouse is clicked within the applet,

the co-ordinates are display one the Java console.

However , i don't know how to

1)Display Total time taken when the game is finished

2)The time passed when a ball is killed at the applet's bottom

here is my current code

import java.awt.Color;

import java.awt.Font;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.geom.Ellipse2D;

import javax.swing.JApplet;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import java.lang.String;

publicclass BallAppletextends JAppletimplements Runnable{

staticfinalint INC_X = 5;//define x increment

staticfinalint INC_Y = 5;//define y increment

staticfinalint BALL_WIDTH = 15;//define ball oval width

staticfinalint BALL_HEIGHT = 15;//define ball oval height

staticfinalint TOTAL_NUMBER = 10;//define total number of balls

staticfinalint MSG_SIZE = 18;//define the font size of message

intintBallKill = 0;//number of balls killed

longlngBeginTime;//begin time for playing game

longlngEndTime;//end time for playing game

boolean blnBeginGame =false;//whether game is begun

intintMaxX, intMaxY;//applet size

Lab54_Ball[] ball;//ball array

publicvoid init(){

this.getContentPane().setBackground(Color.BLACK);

intMaxX = getSize().width;//obtain applet width

intMaxY = getSize().height-MSG_SIZE-2;//obtain applet height

/* Prepare for mouse click action */

addMouseListener(new MouseListener(){

publicvoid mouseClicked( MouseEvent e ){

handleMouseClicked( e );

}

/* Override the mouse click event */

publicvoid mousePressed( MouseEvent e ){}

publicvoid mouseReleased( MouseEvent e ){}

publicvoid mouseEntered( MouseEvent e ){}

publicvoid mouseExited( MouseEvent e ){}

});

/* Create the Ball threads */

/*Create all the balls */

for (int i=0;i<11;i++){

ball[i] =new Lab54_Ball(intMaxX, intMaxY, BALL_WIDTH, BALL_HEIGHT, INC_X, INC_Y);

}

Thread thread =new Thread(this);

thread.start();

}

publicvoid paint(Graphics g){

super.paint(g);//ensure display is cleaned in each repaint()

Graphics2D g2d = (Graphics2D) g;//casting g to Graphics2D

g2d.setPaint( Color.MAGENTA );

for (int i=0;i<11;i++){

g2d.fill(new Ellipse2D.Double(INC_X, INC_Y, ball[i].intWidth, ball[i].intHeight) );//fill an ellipse

}

/* Display the game messages */

g2d.setPaint(Color.WHITE);

g2d.setFont(new Font("Courier", Font.BOLD, MSG_SIZE) );

if( !blnBeginGame ){

g2d.drawString("Click to Start", 0, intMaxY+MSG_SIZE);

}else{

Display message at the applet's bottom

- Total time taken when the game is finished

- The time passed when a ball is killed */

}

}

publicvoid run(){

while(true){//infinite loop

try{

repaint();//update display

Thread.currentThread().sleep(50);//refresh display in 50ms

}catch(InterruptedException e){

e.printStackTrace();

}

}

}

publicvoid handleMouseClicked(MouseEvent e){

int intX, intY;

intX = e.getX();//mouse x-coordinate

intY = e.getY();//mouse y-coordinate

/* Send mouse coordinates (x,y) to java console */

System.out.println("Mouse at (" + intX +"," + intY +")");

if(blnBeginGame){//game is running

/* Check whether ball is hit */

for(int i=0; i<TOTAL_NUMBER; i++){

if(ball[i] !=null){//for the ball still exists

synchronized( ball[i] ){

IF the ball is hit,

- Kill the ball

- Record the time

- Count the number of balls killed */

ball[i] =null;

}

}

}

}else{

blnBeginGame =true;

lngBeginTime = System.currentTimeMillis();

}

}

}

Message was edited by:

benjaminCcc

Message was edited by:

benjaminCcc>

[9282 byte] By [benjaminCcca] at [2007-11-26 21:56:28]
# 1
Calculate the total time by making an integer keep track of the time in the run method (the amount depends on the sleep time). Then add a line to your paint method.Have another variable to calculate the second time you want.
Tavea at 2007-7-10 3:53:00 > top of Java-index,Java Essentials,New To Java...