Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Need to convert JApplet to JFrame

807591May 30 2008 — edited May 30 2008
I need to write code for where I have 60 balls bouncing around inside a window. The client will then be able to select a button and it will pull out a ball with the number 1-60 written on it. The user will be able to do this up to 7 times. Each time it is done the past numbers that have already appeared can not reappear. What I am stuck on right now is geting my balls into a JFrame. Can anyone give advice or show how to. I currently have my 60 balls running in a JApplet. Here is the JAVA code and the HTML code. Thanks!

***************************************************
Here is the JAVA code
***************************************************

import java.awt.*;
import java.applet.*;
import java.util.*;
import javax.swing.*;



class CollideBall{
int width, height;
public static final int diameter=20;
//coordinates and value of increment
double x, y, xinc, yinc, coll_x, coll_y;
boolean collide;
Color color;
Graphics g;
Rectangle r;

//the constructor
public CollideBall(int w, int h, int x, int y, double xinc, double yinc, Color c){
width=w;
height=h;
this.x=x;
this.y=y;
this.xinc=xinc;
this.yinc=yinc;
color=c;
r=new Rectangle(150,80,130,90);
}

public double getCenterX() {return x+diameter/2;}
public double getCenterY() {return y+diameter/2;}

public void alterRect(int x, int y, int w, int h){
r.setLocation(x,y);
r.setSize(w,h);
}

public void move(){
if (collide){
double xvect=coll_x-getCenterX();
double yvect=coll_y-getCenterY();
if((xinc>0 && xvect>0) || (xinc<0 && xvect<0))
xinc=-xinc;
if((yinc>0 && yvect>0) || (yinc<0 && yvect<0))
yinc=-yinc;
collide=false;
}

x+=xinc;
y+=yinc;

//when the ball bumps against a boundary, it bounces off
if(x<6 || x>width-diameter){
xinc=-xinc;
x+=xinc;
}

if(y<6 || y>height-diameter){
yinc=-yinc;
y+=yinc;
}

//cast ball coordinates to integers
int x=(int)this.x;
int y=(int)this.y;

//bounce off the obstacle
//left border
if(x>r.x-diameter&&x<r.x-diameter+7&&xinc>0&&y>r.y-diameter&&y<r.y+r.height){
xinc=-xinc;
x+=xinc;
}
//right border
if(x<r.x+r.width&&x>r.x+r.width-7&&xinc<0&&y>r.y-diameter&&y<r.y+r.height){
xinc=-xinc;
x+=xinc;
}
//upper border
if(y>r.y-diameter&&y<r.y-diameter+7&&yinc>0&&x>r.x-diameter&&x<r.x+r.width){
yinc=-yinc;
y+=yinc;
}
//bottom border
if(y<r.y+r.height&&y>r.y+r.height-7&&yinc<0&&x>r.x-diameter&&x<r.x+r.width){
yinc=-yinc;
y+=yinc;
}

}

public void hit(CollideBall b){
if(!collide){
coll_x=b.getCenterX();
coll_y=b.getCenterY();
collide=true;
}
}

public void paint(Graphics gr){
g=gr;
g.setColor(color);
//the coordinates in fillOval have to be int, so we cast
//explicitly from double to int
g.fillOval((int)x,(int)y,diameter,diameter);

g.setColor(Color.white);
g.drawArc((int)x,(int)y,diameter,diameter,45,180);

g.setColor(Color.darkGray);
g.drawArc((int)x,(int)y,diameter,diameter,225,180);
}
}

public class BouncingBalls extends Applet implements Runnable {
Thread runner;
Image Buffer;
Graphics gBuffer;
CollideBall ball[];
//Obstacle o;
//how many balls?
static final int MAX=60;
boolean intro=true,drag,shiftW,shiftN,shiftE,shiftS;
boolean shiftNW,shiftSW,shiftNE,shiftSE;
int xtemp,ytemp,startx,starty;
int west, north, east, south;

public void init() {
Buffer=createImage(getSize().width,getSize().height);
gBuffer=Buffer.getGraphics();

ball=new CollideBall[MAX];

int w=getSize().width-5;
int h=getSize().height-5;

//our balls have different start coordinates, increment values
//(speed, direction) and colors
for (int i = 0;i<60;i++){
ball=new CollideBall(w,h,50+i,20+i,1.5,2.0,Color.white);
/* ball[1]=new CollideBall(w,h,60,210,2.0,-3.0,Color.red);
ball[2]=new CollideBall(w,h,15,70,-2.0,-2.5,Color.pink);
ball[3]=new CollideBall(w,h,150,30,-2.7,-2.0,Color.cyan);
ball[4]=new CollideBall(w,h,210,30,2.2,-3.5,Color.magenta);
ball[5]=new CollideBall(w,h,360,170,2.2,-1.5,Color.yellow);
ball[6]=new CollideBall(w,h,210,180,-1.2,-2.5,Color.blue);
ball[7]=new CollideBall(w,h,330,30,-2.2,-1.8,Color.green);
ball[8]=new CollideBall(w,h,180,220,-2.2,-1.8,Color.black);
ball[9]=new CollideBall(w,h,330,130,-2.2,-1.8,Color.gray);
ball[10]=new CollideBall(w,h,330,10,-2.1,-2.0,Color.gray);
ball[11]=new CollideBall(w,h,220,230,-1.2,-1.8,Color.gray);
ball[12]=new CollideBall(w,h,230,60,-2.3,-2.5,Color.gray);
ball[13]=new CollideBall(w,h,320,230,-2.2,-1.8,Color.gray);
ball[14]=new CollideBall(w,h,130,300,-2.7,-3.0,Color.gray);
ball[15]=new CollideBall(w,h,210,90,-2.0,-1.8,Color.gray);*/
}

}

public void start(){
if (runner == null) {
runner = new Thread (this);
runner.start();
}
}

/* public void stop(){
if (runner != null) {
runner.stop();
runner = null;
}
}*/

public void run(){
while(true) {
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);

try {runner.sleep(15);}
catch (Exception e) { }

//move our balls around
for(int i=0;i<MAX;i++)
ball[i].move();

handleCollision();

repaint();
}
}

boolean collide(CollideBall b1, CollideBall b2){
double wx=b1.getCenterX()-b2.getCenterX();
double wy=b1.getCenterY()-b2.getCenterY();

//we calculate the distance between the centers two
//colliding balls (theorem of Pythagoras)
double distance=Math.sqrt(wx*wx+wy*wy);

if(distance<b1.diameter)
return true;

return false;
}

private void handleCollision()
{
//we iterate through all the balls, checking for collision
for(int i=0;i<MAX;i++)
for(int j=0;j<MAX;j++)
{
if(i!=j)
{
if(collide(ball[i], ball[j]))
{
ball[i].hit(ball[j]);
ball[j].hit(ball[i]);
}
}
}
}

public void update(Graphics g)
{
paint(g);
}

public void paint(Graphics g)
{
gBuffer.setColor(Color.lightGray);
gBuffer.fillRect(0,0,getSize().width,getSize().height);

gBuffer.draw3DRect(5,5,getSize().width-10,getSize().height-10,false);



//paint our balls
for(int i=0;i<MAX;i++)
ball[i].paint(gBuffer);
g.drawImage (Buffer,0,0, this);
}
}


***************************************************
Here is the HTML code
***************************************************
<html>
<body bgcolor="gray">

<br><br>

<div align="center">
<applet code="BouncingBalls.class" width="1000" height="650"></applet>
</div>

</body>
</html>
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 27 2008
Added on May 30 2008
2 comments
260 views