thread error

Started by
138 comments, last by ChristianFrantz 11 years, 8 months ago
Well that's definitely not good for business, ha.
When you removed the loop from Ball's paint method, did you also change it so it wasn't doing any ball stuff? There wouldn't need to be any array stuff in there.
And you kept the loop in game's paint method?
Advertisement
oh yup now it works fine lol

[source lang="java"]class Ball
{
int x;
int y;
int width;
int height;
Color mycolor;

public Ball(int x, int y, int width, int height, Color mycolor)
{
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.mycolor = mycolor;
}//end ball

public void paint(Graphics g)
{
g.setColor(mycolor);
g.fillOval(x, y, width, height);
} //end paint
} //ball class
[/source]

[source lang="java"]public void paint(Graphics g)
{
g.drawString("time: " + t, 20, 20);
g.drawString("Red ball: " + randomNumber() + ", " + randomNumber(), 20, 40);
g.drawString("blue ball: " + randomNumber() + ", " + randomNumber(), 20, 60);
g.drawString("green ball: " + randomNumber() + ", " + randomNumber(), 20, 80);
g.drawString("magenta ball: " + randomNumber() + ", " + randomNumber(), 20, 100);


g.setColor(Color.black);
g.fillOval(xpos - radius, ypos - radius, 2 * radius, 2 * radius);

for(i = 0; ilessthanNUM_OF_BALLS; i++)
{
ball.paint(g);
}
}
}[/source]

If you see a post from me, you can safely assume its C# and XNA :)

I wanna reiterate since I am throwing up different issues kind of all at once. Remember to take one issue and work on solving it (small steps). So don't try to change and update and add 3 or 4 things all at once.

For the thread code you added, I wanna say I think adding threads is only going to make this more complex, harder to maintain and harder to understand what's going wrong when something does. But if you insist on diving into threads, We'll need to see the code where you start or schedule that thread to run. For the code you posted, if (t == 5) would never be true. You have a for loop that runs from 0 through 9 and quits when t equals 10 so t will equal 10 when that loop is exited. If it were to ever be true, the line of code inside just generates a random number but doesn't do anything with it or put it anywhere so that you can use it (assuming the randomNumber method is the same as before).
Excellent. Now that's a solid little Ball class. Does that leave you now working on getting them appearing every 10 seconds?
yeah threads are really confusing but i figure i gotta learn them sometime lol. right now i have this

[source lang="java"] public void paint(Graphics g)
{
g.drawString("time: " + t, 20, 20);
g.drawString("Red ball: " + randomNumber() + ", " + randomNumber(), 20, 40);
g.drawString("blue ball: " + randomNumber() + ", " + randomNumber(), 20, 60);
g.drawString("green ball: " + randomNumber() + ", " + randomNumber(), 20, 80);
g.drawString("magenta ball: " + randomNumber() + ", " + randomNumber(), 20, 100);

g.setColor(Color.black);
g.fillOval(xpos - radius, ypos - radius, 2 * radius, 2 * radius);

for(i = 0; ilessthanNUM_OF_BALLS; i++)
{
ball.paint(g);
try
{
Thread.sleep(5000);

}catch(InterruptedException e){}
}
}
}[/source]
this completely stops my whole program for running but every 5 seconds a new ball will pop up on the screen. Kinda what wanted but im not sure what i did to make the whole program stop lol

If you see a post from me, you can safely assume its C# and XNA :)

I wish I could say that after you've used Threads a little, they become easy. But they never do... Most programmers reach a point where they can write good threaded code, but still not actually correct.. just that it's correct enough that it doesn't bite them in the ass all that often.

Alright, a couple of things:

1. Right now you're generating a new random number every time you do one of those drawString statements. That is just like it sounds like, a random number with no particular meaning or relation to anything. It's random. It looks like you're wanting to treat those statements like debugging kind of statements that tell you where each ball actually is. For that information, you'd want to look at the variables the ball has (like ball[0].x, ball[0].y, etc).

2. The reason your whole program "stops" for 5 seconds is you tell this program's thread to sleep for 5 seconds (Thread.sleep(5000). So for 5 seconds this thread isn't responding or updating or drawing or anything. What you want probably isn't to do NOTHING for 5 seconds.. just that you have something you DO want to happen every 5 seconds. For help on that, I think you'll need to post all of your code, as this will be a time where I need to take in what you're doing on the various threads and to try to see what you might be doing that you don't mean to be doing on the various threads.
ah thanks. i didnt understand that each time i wrote randomNumber() in the drawString i was creating a new number lol

the updateBalls thread is obviously wrong but ive been messing around with a few things in order to figure out what id have to change

[source lang="java"]public class game extends Applet implements Runnable
{
int xpos = 100;
int ypos = 100;
int radius = 5;
int xspeed = 0;
int yspeed = 0;
static final int WIDTH = 450;
static final int HEIGHT = 450;
private Image dbImage;
private Graphics dbg;

boolean run = true;

public Ball ball[];

static final int NUM_OF_BALLS = 4;

int i;
int t;

Thread updateTime = new updateTime();

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

updateTime.start();
}

public class updateTime extends Thread implements Runnable
{
public void run()
{
for(t = 0; t < 10; t++)
{
try
{
Thread.sleep(1000);
}
catch(InterruptedException e){}
}
}
}

public int randomNumber()
{
return (int)(Math.random() * 400);
}

class Ball
{
int x;
int y;
int width;
int height;
Color mycolor;

public Ball(int x, int y, int width, int height, Color mycolor)
{
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.mycolor = mycolor;
}//end ball

public void paint(Graphics g)
{
g.setColor(mycolor);
g.fillOval(x, y, width, height);
} //end paint
} //ball class

public void update(Graphics g) //double buffer don't touch!!
{
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 class updateBalls extends Thread implements Runnable
{
public void run(Graphics g)
{
for(i = 0; i<NUM_OF_BALLS; i++)
{
ball.paint(g);
}
try
{
Thread.sleep(5000);
}catch(InterruptedException e){}
}
}*/

public void init()
{
this.setSize(WIDTH, HEIGHT);

ball = new Ball[NUM_OF_BALLS];

ball[0] = new Ball(randomNumber(), randomNumber(), 20, 20, Color.red);
ball[1] = new Ball(randomNumber(), randomNumber(), 20, 20, Color.blue);
ball[2] = new Ball(randomNumber(), randomNumber(), 20, 20, Color.green);
ball[3] = new Ball(randomNumber(), randomNumber(), 20, 20, Color.magenta);
}

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(run)
{
repaint();

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

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

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

public void paint(Graphics g)
{
g.drawString("time: " + t, 20, 20);
g.drawString("Red ball: " + randomNumber() + ", " + randomNumber(), 20, 40);
g.drawString("blue ball: " + randomNumber() + ", " + randomNumber(), 20, 60);
g.drawString("green ball: " + randomNumber() + ", " + randomNumber(), 20, 80);
g.drawString("magenta ball: " + randomNumber() + ", " + randomNumber(), 20, 100);

g.setColor(Color.black);
g.fillOval(xpos - radius, ypos - radius, 2 * radius, 2 * radius);

for(i = 0; ilessthanNUM_OF_BALLS; i++)
{
ball.paint(g);
}
}
}[/source]

If you see a post from me, you can safely assume its C# and XNA :)

I think if you're gonna stick with Threads so heavily, you'll need to go read up on some concurrency (http://docs.oracle.com/javase/tutorial/essential/concurrency/) to get a good idea of what goes on and what you want to go on.

I'll also write a more lengthy post about a good to do what you want to w/o threads.. it'll be an hour or so though probably before I can do that.
alright lol. while im waiting ill try to do some collision detection :P thanks for all your help!

If you see a post from me, you can safely assume its C# and XNA :)

Alright, sorry but I needed to support my gaming habit for a while. I just put this together and I did confirm that it runs. If you have any questions about why I did something, please do feel free to ask because I can't/won't say that everything is the best approach to a problem. I put it together to show you things being done based on time but not centered around running several threads within my program.

[source lang="java"]import java.applet.Applet;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

public class GameWithTime extends Applet implements Runnable
{
//Sets NEW_DOT_FREQUENCY as 3 seconds
private static long NEW_DOT_FREQUENCY = TimeUnit.SECONDS.toMillis(3);
private static final int SCREEN_WIDTH = 400;
private static final int SCREEN_HEIGHT = 400;

//Stores what the time was when updateGame last ran
private long lastUpdateTime = 0;
//Tracks how much time has passed since the last new dot was added
private long timeSinceLastNewDot = 0;
//List of all the enemy dots
private ArrayList<Dot> enemyDots;

private Thread animator;
private boolean isRunning;

//Initialize things for the game
public void init()
{
debugPrint("Initializing values.");
//Put 4 dots in the list initially
enemyDots = new ArrayList<Dot>();
enemyDots.add(new Dot(randomValue(), randomValue(), 10, 10));
enemyDots.add(new Dot(randomValue(), randomValue(), 10, 20));
enemyDots.add(new Dot(randomValue(), randomValue(), 20, 10));
enemyDots.add(new Dot(randomValue(), randomValue(), 20, 20));
}

public void start()
{
lastUpdateTime = System.currentTimeMillis();
isRunning = true;

animator = new Thread(this);
animator.start();
}

public void stop()
{
isRunning = false;
}

//Applet is closing, clean up
public void destroy()
{

}

public void run()
{
while (isRunning)
{
repaint();
try
{
Thread.sleep(15);
} catch (InterruptedException e)
{
}
}
}

//This update and paint approach is an alternative to double buffering to avoid flickering
public void update(Graphics g)
{
//Update variables
updateGame();

//Draw things to the screen
//Clear the screen
g.setColor(Color.BLACK);
g.fillRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);

g.setColor(Color.cyan);
//Draw each dot in the list
for (Dot dot : enemyDots)
{
dot.draw(g);
}
}

//This update and paint approach is an alternative to double buffering to avoid flickering
public void paint(Graphics g)
{
update(g);
}

private void updateGame()
{
//Get the current time
long currentTime = System.currentTimeMillis();
//Calculate how much time has passed since the last update
long elapsedTime = currentTime - lastUpdateTime;
//Store this as the most recent update time
lastUpdateTime = currentTime;

//Process inputs
updateVariablesBasedOnUserInput();

//Create a new dot if enough time has passed
//Update the time since last new dot was drawn
timeSinceLastNewDot += elapsedTime;
//See if enough time has passed to create a new dot
debugPrint(Long.toString(timeSinceLastNewDot));

if (timeSinceLastNewDot >= NEW_DOT_FREQUENCY)
{
//Generate random x and y values for new dot
int newX = randomValue();
int newY = randomValue();

//Add a new dot to the collection of dots
debugPrint("New dot created at x:" + newX + ", y:" + newY + ".");
enemyDots.add(new Dot(newX, newY, 20, 20));

//Reset time since a new dot was created
timeSinceLastNewDot = 0;
}
}

private void updateVariablesBasedOnUserInput()
{
//Set speeds/velocities based on user inputs
}

//Returns a new random value each time a call is made
private int randomValue()
{
return (int) (Math.random() * 400);
}

//Prints passed value to the console
private void debugPrint(String value)
{
System.out.println(value);
}

private class Dot extends Ellipse2D.Float
{
public Dot(int xValue, int yValue, int width, int height)
{
this.x = xValue;
this.y = yValue;
this.width = width;
this.height = height;
}

public void draw(Graphics graphics)
{
graphics.drawOval((int) x, (int) y, (int) width, (int) height);
}
}
}
[/source]

This topic is closed to new replies.

Advertisement