dcsimg
multithread program identify thread
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   hema_R
Posted On:   Tuesday, August 6, 2002 04:25 AM

Hi, It has two threads.Each thread control one ball. When click a ball, the ball will stop. If click again, it begin to move, The problem here is how to identify which thread is clicked... I am attaching the code also I am not able to get the name of the thread(identify which thread is clicked) when i try to use Thread.currentThread(). it is throwing the ClassCastException ----Ticker thread = (Ticker) Thread.currentThread (); Exception occurred during event dispatching: java.lang.ClassCastException: java.awt.EventDispatchThread I wish to know if it is possible to control the Threads in this way, if not i would please request to suggest the different idea. B   More>>

Hi,

It has two threads.Each thread control one ball. When click a ball, the ball will stop. If click again,
it begin to move, The problem here is how to identify
which thread is clicked... I am attaching the code also
I am not able to get the name of the thread(identify which thread is clicked) when i try to use Thread.currentThread().
it is throwing the ClassCastException

----Ticker thread = (Ticker) Thread.currentThread ();
Exception occurred during event dispatching:
java.lang.ClassCastException: java.awt.EventDispatchThread


I wish to know if it is possible to control the Threads in this way, if not i would please request to suggest the different idea.


BallApplet.java
-------------------------------

			/*
			*/
			

class Ticker extends Thread
{
BallApplet1 host; double pi = 3.145926;
boolean oi,susflg;
Thread ct;
String name;


public Ticker(BallApplet1 obj,String nam,int x, int y , boolean oi) {
ct = new Thread(this,nam);
this.name=nam;
ct.start();
host = obj;
host.x=x;
host.y=y;
host.oi=oi;
this.oi=oi;
this.susflg=false;
}

boolean getflg(){
return susflg;
}

synchronized void setflg(boolean flg){
susflg=flg;
}

public String getname(){
return name;
}

public void run() {
int t =0;
while(true){
if(oi==false){
t = (t-2)%360;
double alpha = t*pi/180;
host.x = 200 + (int)(175*Math.cos(alpha));
host.y = 200-(int)(175*Math.sin(alpha));
host.repaint();
}
if(oi==true){
t = (t+2)%360;
double alpha = t*pi/180;
host.x = 200 + (int)(125*Math.cos(alpha));
host.y = 200-(int)(125*Math.sin(alpha));
host.repaint();
}
try {Thread.sleep(50L);}
catch(Exception e){}
}
}
}

public class BallApplet1 extends Applet
{
int x=375; int y =200; int flag = 0;
boolean oi;
Ticker control2,control;
Image img;
Graphics screen;
String mes="";

public void init() {
this.setSize(400, 400);
this.addMouseListener( new MouseAdapter()
{ public void mousePressed(MouseEvent e)
{
int a = e.getX();
int b = e.getY();
check(a,b); } } );
img = this.createImage(400,400);
screen = img.getGraphics();
control2 = new Ticker(this,"n1",325,200,true);
control = new Ticker(this,"n2",375,200,false);
}

public synchronized void check(int a, int b) {
mes="";
if ((a-x)*(a-x) +(b-y)*(b-y)> 625)
return;
flag = 1-flag;
//----------it throws exception here.....
Ticker thread = (Ticker) Thread.currentThread ();
//mes=thread.getname();
//if ( t instanceof Ticker ) {
//Ticker thread = (Ticker) Thread.currentThread ();
if ( Thread.currentThread().getName().equals ("n2") ) {
mes="n1";
// }
}

}

public void update(Graphics g) { paint(g); }
public void paint(Graphics g) {
screen.setColor(Color.blue);
screen.fillRect(0,0,400,400);
screen.setColor(new Color(255,255,180));
screen.fillOval(50, 50, 300, 300);
screen.setColor(Color.red);
screen.fillOval(x-25, y-25, 50, 50);
g.drawImage(img,0,0,this);
g.drawString(mes,40,40);
}

}


Thanx
rhema    <<Less

Re: multithread program identify thread

Posted By:   Lasse_Koskela  
Posted On:   Tuesday, August 6, 2002 06:18 AM

When the user clicks the canvas/whatever where you get an event object, read the mouse coordinates and compare to a list of all objects' coordinates (balls) to see which ball was clicked.


In the ball object, you should store a reference to the Thread. Somewhat like this:



public class X
{
public void mouseClicked(...)
{
// read mouse coordinates
mouseX = ...;
mouseY = ...;

// loop thru all Ball objects
for (int i=0; i < balls.length; i++)
{
if (balls[i].isAt(mouseX, mouseY))
{
// inform the Ball object which was clicked
balls[i].click();
}
}
}
}

public class Ball
{
private Thread myThread;

public boolean isAt(int x, int y)
{
...
}

public void click()
{
this.myThread.ballWasClicked();
}
}
About | Sitemap | Contact