Jump to content
  • Advertisement
Sign in to follow this  
ChristianFrantz

thread error

This topic is 2161 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Why is this giving me an error? it says that the code is unreachable whatever that means

[source lang="java"] public class CreateDOTS extends Thread //create new dots every 10 seconds at random points on the screen
{
public void run()
{
while(true)
{
repaint();
int randomx = (int)(Math.random() * 399 + 1);//create random x
int randomy = (int)(Math.random() * 399 + 1);//create random y
}
try
{
Thread.sleep(5000);
}catch(InterruptedException e){}
}
}[/source Edited by burnt_casadilla

Share this post


Link to post
Share on other sites
Advertisement
In general, whenever you're asking for help with an error, it's helpful to tell us which line of code the error mentions and the exact text of the error.

Looking through the code, I imagine the error is at the Thread.sleep(5000) line?

It's telling you that it's not possible to ever reach that line of code. In this case, it's right as your loop above it says while(true). Since true will always evaluate to true, you'll enter that while loop and never get out of it (which is what would need to happen to reach the errored line).

Share this post


Link to post
Share on other sites
i figured it out lol. the try was outside of the while loop. Another question tho... i created a new class file in order to stop confusing myself, so how do i import that into the main file i am using?

oh figured that out too. now another...

i have this code

[source lang="java"]import game.java.createDOTS;

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


public class game extends Applet implements Runnable
{
int xpos = 100;
int ypos = 100;
int radius = 5;
int xspeed = 0;
int yspeed = 0;
int randomx;
int randomy;
static final int WIDTH = 400;
static final int HEIGHT = 400;
private Image dbImage;
private Graphics dbg;

public void update(Graphics g)
{
if(dbImage == null)
{
dbImage = createImage(this.getSize().width, this.getSize().height);
dbg = dbImage.getGraphics();
}

dbg.setColor(getBackground());
dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);

dbg.setColor(getForeground());
paint(dbg);

g.drawImage(dbImage, 0, 0, this);
}
public void init()
{
this.setSize(WIDTH, HEIGHT);
}

public void start()
{
Thread th = new Thread(this);
th.start();//start main game

Thread createDOTS = new createDOTS();
createDOTS.start();

System.out.println(randomx);
System.out.println(randomy);
}

public void stop()
{

}

public void destroy()
{

}

public boolean keyDown (Event e, int key)
{
if(key == Event.LEFT)
{
xspeed = -5;
yspeed = 0;
}

if(key == Event.RIGHT)
{
xspeed = 5;
yspeed = 0;
}

if(key == Event.UP)
{
yspeed = -5;
xspeed = 0;
}

if(key == Event.DOWN)
{
yspeed = 5;
xspeed = 0;
}

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

repaint();

if (xpos < 1)
{
xpos = 399;
}

if (xpos > 399)
{
xpos = 1;
}
if (ypos < 1)
{
ypos = 399;
}

if (ypos > 399)
{
ypos = 1;
}
ypos += yspeed;
xpos += xspeed;
try
{
Thread.sleep(20);
}
catch(InterruptedException ex)
{

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


public void paint(Graphics g)
{
g.setColor(Color.black);
g.fillOval(xpos - radius, ypos - radius, 2 * radius, 2 * radius);

}
}[/source]

with this class in another file

[source lang="java"]package game.java;
import java.applet.*;
import java.awt.*;

public class createDOTS extends Thread //create new dots every 10 seconds at random points on the screen
{
int randomx;
int randomy;
int high = 399;
int low = 1;
int dotradius = 10;

public void create_dots(int randomx, int randomy, Graphics g)
{
while(true)
{
randomx = (int)(Math.random() * (high - low + 1)) + low;//create random x
randomy = (int)(Math.random() * (high - low + 1)) + low;//create random y
System.out.println(randomx + " " + randomy);


g.setColor(Color.black);
g.fillOval(randomx, randomy, dotradius * 2, dotradius * 2);
try
{
Thread.sleep(5000);

}catch(InterruptedException e){}

}
}

}[/source]

and what i want to do with the createDOTS class is generate a random coordinate every 5 seconds and place a dot on the screen in that coordinate, but its not working and im not sure what i have to do to get it to work Edited by burnt_casadilla

Share this post


Link to post
Share on other sites
The documentation of Thread describes pretty well what you have to do: http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.html
Also, the Graphics context you have is in general not valid beyond the paint() method, so you cannot just transfer it to another thread anyway. You would need to add a point to a collection in the main Applet and tell the Applet to trigger a repaint.

Share this post


Link to post
Share on other sites
Threading is a very low level concept. There is a lot to be learned before you can effectively and safely use threads. The high level solution to this kind of problem is to create a timer. Another solution would be to include the dot spawning code in the main loop, using the value of System.currentTimeMillis() to control execution.

That said, your code doesn't appear to support multiple dots at the moment. I would suggest trying to get multiple dots to work first - for example by starting the game with 5 dots, before you try writing code to spawn additional dots. For example, you'll probably want a Dot class, and then the Game class would have a collection of Dot instances - perhaps held in an ArrayList.

Your code also seems to fall prey to the same problem described here. Again, this is caused by use of the low-level thread concept without enough care to respect the rules required. As mentioned in that thread, one solution is to synchronously request repaints via SwingUtilities.invokeAndWait().

Share this post


Link to post
Share on other sites
alright i added this to the beginning of my program.

[source lang="java"]class Ball extends Ellipse2D.Float//class to store the ball
{
private int diameter = 20;//diameter of ball
private int d;

public Ball(int diameter)
{
super((int)(Math.random() * (400 - 20)+ 1), (int)(Math.random() * (400 - 20) + 1), diameter, diameter);//create random points to spawn the ball
this.d = diameter;

}
}
class PaintSurface extends JComponent//class to draw the ball
{
public ArrayList<Ball> Balls = new ArrayList<Ball>();//arraylist to store 10 balls

public PaintSurface()
{
for (int i = 0; i < 10; i++)
Balls.add(new Ball(20));//add new ball to the arraylist up to 10
}

public void paintBalls (Graphics g)
{
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(Color.red);
for (Ball Ball : Balls)
{
g2.fill(Ball);//supposed to draw balls on the screen
}
}
}
[/source]

still doesnt work....

with this im just trying to add 10 balls at random places on the screen. seems simple enough Edited by burnt_casadilla

Share this post


Link to post
Share on other sites
Define "doesn't work". Compile error? Runtime error? Nothing drawn on screen? Ghosts escape from your computer?

I suspect one problem is that paintBalls() isn't getting called. The Swing framework only automatically calls some functions when things need to be drawn. Your PaintSurface should override "protected paintComponent(Graphics g)". This can simply call paintBalls() then, or you can move the paintBalls() code inside paintComponent().

Share this post


Link to post
Share on other sites
Was rip-off's solution about paintComponent able to get at least something being drawn on the screen?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!