Solved

Activate graphics in new threads in java 6

Posted on 2009-05-13
11
186 Views
Last Modified: 2012-05-06
I am fairly new to java and having some trouble with graphics in threads.
 
I am making an application that solves a maze by using a ball.  When the ball comes into contact with a wall at a steep angle, it will divide into two separate balls.  This is done using threads.  When a collision is triggered, the current thread will stop and two new threads will carry on with the end coordinates of the previous thread.

I have been unable to finish this part of the code as I cannot get new threads to display graphics.  For some reason, only threads declared in the superclass are able to display graphics.  Once the graphics have been sorted, I will be able to finish the ball-splitting part of the program.  I have attached snippets of the code as well as full code in txt files.  

On another note, I want to pass several variables to the new threads, such as x, y and angle. Is it possible to do this when it is creating a new version of itself?
public void run () {

		boolean alive = true;

		int x_speed = 0;

		int y_speed = 0;

		int angle = 60;

		int wall_angle = 0;

		boolean collision = false;

		int collision_angle =0;

		// lower ThreadPriority 

		Thread.currentThread().setPriority(Thread.MIN_PRIORITY); 

		

		// run a long while (true) this means in our case "always" 

		while (alive = true)

		{

			System.out.println("maze(x,y)=" + maze[x_pos][y_pos]);

			x_speed = get_x(x_pos, x_speed, angle);		

			y_speed = get_y(y_pos, y_speed, angle);

			collision = detect_collision(x_pos, y_pos, collision);

			wall_angle = get_wall_angle(x_pos, y_pos, wall_angle);

			if (collision==true){

			collision_angle = get_collision_angle(angle, wall_angle, collision_angle);

				if ((collision_angle <55) || (collision_angle >125)) {

					angle = wall_angle;

					angle = solve_collision_angle(angle, wall_angle, collision_angle);

					collision=false;

				}

				else{ //make new thread

				new Thread(this).start();

				alive = false;

				break;}

			}

			

			//System.out.println(collision);
 

			x_pos += x_speed;

			//System.out.println("x = " + x_pos + " ");

			y_pos += y_speed;

			//System.out.println("y = " + y_pos + " ");
 

		// repaint the applet 

			

		repaint(); 
 

		try 

		{

		// Stop thread for 20 milliseconds 

		Thread.sleep (20); 

		} 

		catch (InterruptedException ex) 

		{

		// do nothing 

		} 

		// set ThreadPriority to maximum value 

		Thread.currentThread().setPriority(Thread.MAX_PRIORITY); 

		}

		

	} 
 

// paint function

	public void paint (Graphics draw_tip) {
 

		int i=0;

		int j=0;

		int scale = 20;

		

		super.paint(draw_tip);

		Graphics2D comp2D = (Graphics2D)draw_tip;
 

		comp2D.setPaint(Color.black);
 

		int x = 0;

		int y = 0;

		

		//draw the maze

		for (i=0; i<=19; i++){

			for (j=0; j<=19; j++){

				//System.out.println("maze[i][j] = " + maze[i][j]);

			if (big_maze[i][j] == 1){

			comp2D.fillRect(j*scale, i*scale, 20, 20);}

			}}

		

		// paint a filled colored circle 

		Graphics2D tip = (Graphics2D)draw_tip;

		tip.setPaint (Color.red); 

		tip.fillOval (x_pos - real_radius, y_pos - real_radius, 2 * real_radius, 2 * real_radius); 

	}

Open in new window

Ball.txt
BallMain.txt
TextFrame.txt
0
Comment
Question by:dgossage
11 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 24377603
Only components can display graphics so your threads will have to access the paint or paintComponent (depending on what kind of component it is) method of one of those
0
 
LVL 14

Expert Comment

by:CPColin
ID: 24377639
Also, your line 13 is eternally setting "alive" to "true," as opposed to checking to see if it is true. It should instead read:
while (alive)

{

   ...

}

Open in new window

0
 

Author Comment

by:dgossage
ID: 24379958
Thanks, I thought the while loop was acting a bit funny.  Do you have any examples of threads accessing paintComponents?  Is this easy to do?
0
 
LVL 92

Expert Comment

by:objects
ID: 24380110
>  Do you have any examples of threads accessing paintComponents?

threads should never call paintComponent(), thats the job of the paint system.

you should separate your thread class from your component class.
your thread class is effectively a ball class and would handle updating position for that ball.
your component class would contain a list of balls which it would display
when you add a new ball, create a new ball/thread and add it to the list
0
 

Author Comment

by:dgossage
ID: 24380258
Ok, I think I understand it a bit now, although I am still a novice so it doesnt sound all that straightforward. so the paint component needs to be in a separate class.  Would this be a sub-class of the ball  class or of the ball's superclass?

Could you reccomend a good tutorial for this?  Ever one Ive tried tries to put them in the same class, but are usually only dealing with drawing one object.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 92

Expert Comment

by:objects
ID: 24380267
> Would this be a sub-class of the ball  class or of the ball's superclass?

no, it would contain a list of balls



0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 24380276
> Would this be a sub-class of the ball  class or of the ball's superclass?

no, it would contain a list of balls

A better approach would actually be to only use a single thread to manage all the balls, a separate thread for every ball is not really necessary and won't scale that well.

0
 

Author Comment

by:dgossage
ID: 24380315
Thanks, that sounds like a better idea.  However, each ball will eventually be travelling at different dynamic velocities.  This will be done with the sleep() command.  Will this be possible, as I think the sleep() applies to the whole thread?
0
 
LVL 92

Expert Comment

by:objects
ID: 24380333
you wouldn't use sleep() like that  instead you should be thinking of frames per second for the entire animation and moving your balls appropriately


0
 

Author Comment

by:dgossage
ID: 24380351
By that, do you mean moving each ball a certain number of pixels per frame?  And presumably writing in some code to prevent appearing in walls as well.
0
 
LVL 92

Expert Comment

by:objects
ID: 24380602
basically yes

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
maven example error 3 52
rhino JavaScript import, load 25 66
varialbe initialization 11 30
tomcat not starting 6 24
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to implement Singleton Design Pattern in Java.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now