applets with 2 threads.Problem with moving balls.
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   nstn_nstn
Posted On:   Wednesday, February 28, 2007 07:20 PM

applets with 2 threads. First ball is first threads and second ball is second thread. Ball don't move and it's a problem. Code: "Game2Threads.java": ------------------------ import java.applet.Applet; import java.awt.Color; import java.awt.Graphics; import java.awt.event.*; import java.awt.image.*; import java.awt.Font; import java.awt.Image; public class Game2Threads extends Applet { Ball ball_1 = new Ball(1,50,50,this); Ball ball_2 = new Ball(1,50,50,this); public void init(){ new Ball(1,50,50,this).star   More>>

applets with 2 threads. First ball is first threads and second ball is second thread. Ball don't move and it's a problem.


Code:

"Game2Threads.java":

------------------------

import java.applet.Applet;

import java.awt.Color;

import java.awt.Graphics;

import java.awt.event.*;

import java.awt.image.*;

import java.awt.Font;

import java.awt.Image;







public class Game2Threads extends Applet

{

Ball ball_1 = new Ball(1,50,50,this);

Ball ball_2 = new Ball(1,50,50,this);



public void init(){

new Ball(1,50,50,this).start();

new Ball(2,150,150,this).start();

}

public void paint(Graphics g) {

super.paint(g);

ball_1.paint(g);

ball_2.paint(g);

}

}



---------------------------------------------------

Second file : "Ball.java":





import java.applet.Applet;

import java.awt.Color;

import java.awt.Graphics;

import java.awt.event.*;

import java.awt.image.*;

import java.awt.Font;

import java.awt.Image;



class Ball extends Thread{

public int ball_x;

public int ball_y;

public int ball_w = 15;

public int ball_h = 15;

public int ball_dx = 1;

public int ball_dy = 1;

Color color;

public int noBall;

Applet applet;



public Ball(int nrBall, int x,int y, Applet a){

this.noBall = nrBall;

this.ball_x = x;

this.ball_y = y;

this.color = color;

if(noBall==1){

this.color=new Color(0, 100, 0);

}

if(noBall==2){

this.color=new Color(70, 130,130);

}

}



public void paint(Graphics g){

g.setColor(color);

g.fillArc(ball_x, ball_y, ball_w, ball_h, 1, 358);

}



public void ball1(int getSzer,int getWys){

ball_x+=ball_dx;

ball_y+=ball_dy;

if ((ball_x <0)||(ball_x+ball_w>getSzer))

ball_dx=-ball_dx;

if ((ball_y <0)||(ball_y+ball_h>getWys))

ball_dy=-ball_dy;



}

public void ball2(int getSzer,int getWys){

ball_x += 2 * ball_dx;

ball_y += 2 * ball_dy;

if ((ball_x <0)||(ball_x+ball_w>getSzer))

ball_dx=-ball_dx;

if ((ball_y <0)||(ball_y+ball_h>getWys))

ball_dy=-ball_dy;

}



public void run(){

while(true){

if(noBall == 1){

ball1(200,300);

}

if(noBall == 2){

ball2(200,300);

}

applet.paint(applet.getGraphics());

//this.paint();

try{

Thread.sleep(5);

}catch (Exception e){

System.out.println(e);

}

}

}



}





--------------END CODE---------------





Generally in the end I want to have 2 moving balls in my applets. Balls must move only in a square/rectangle.


Where is the problem?? What's wrong with this code?



best regards

nstn


   <<Less

Re: applets with 2 threads.Problem with moving balls.

Posted By:   Robert_Lybarger  
Posted On:   Wednesday, February 28, 2007 08:34 PM

I'm guessing your problem is entirely in this block:

public class Game2Threads extends Applet
{
Ball ball_1 = new Ball(1,50,50,this);
Ball ball_2 = new Ball(1,50,50,this);

public void init(){
new Ball(1,50,50,this).start();
new Ball(2,150,150,this).start();
}
public void paint(Graphics g) {
super.paint(g);
ball_1.paint(g);
ball_2.paint(g);
}
}

The applet is painting the ball_1 and ball_2 objects, but those are the objects that you "start"ed in the init method.
I stopped looking at this point, though you probably want to actually set the Applet reference passed to the Ball constructors to the 'applet' variable ... else you'll probably NPE in the run method.
About | Sitemap | Contact