• Advertisement
Sign in to follow this  

thread error

This topic is 1984 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

Haha, nice job working through the random number stuff. It sounds like you're getting pretty close to getting through the stuff that you had in mind when you started this post. Sounds like a nice few days of progress!

Share this post


Link to post
Share on other sites
Advertisement
Your method for a random number works but it keeps creating random numbers. That's kind of good since im looking to create a new one every 10 seconds but for this exercise i need to figure out how to stop it lol

Share this post


Link to post
Share on other sites
Hmm, what do you mean you want to stop it? You will only use the random numbers when you create the object. You'll just be storing it and re-using the same value when you draw it every time.

Share this post


Link to post
Share on other sites
well when i run the program it creates new numbers every 20 milliseconds when its updating the ball position when i move it with the keys. so maybe theres a way that i can just set it to every 10 seconds without effecting the draw update time of the ball

Share this post


Link to post
Share on other sites
Ok, so post what your game class is looking like right now.

It shouldn't be generating a new random number every 20 milliseconds if I'm understanding what you want to do correctly. You should use the random number in the init method where you create the ball. But you shouldn't be getting random numbers anywhere else.

Share this post


Link to post
Share on other sites
now it creates one number but gives me a thousand errors lol

[source lang="java"]

import java.applet.*;
import java.awt.*;
import java.util.Timer;

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;

public Ball ball[];

static final int NUM_OF_BALLS = 4;

int i;
int t;

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)
{
for(i = 0; ilessthanNUM_OF_BALLS; i++)
{
g.setColor(ball.mycolor);
g.fillOval((int)ball.x, (int)ball.y, (int)ball.width, (int)ball.height);
}//end for
}//end paint
}//ball class

public void update(Graphics g)//doublebuffer dont 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 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 void start()
{
Thread th = new Thread(this);
th.start();//start main game
}

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

for(i = 0; ilessthanNUM_OF_BALLS; i++);
{
ball.paint(g);
}

g.setColor(Color.black);
g.fillOval(xpos - radius, ypos - radius, 2 * radius, 2 * radius);
}
}[/source]

Share this post


Link to post
Share on other sites
no errors anymore. idk what i did to give me errors tongue.png but in the paint method where i display the coordinates of each ball, the numbers are changing really fast

well i figured out the timer

[source lang="java"]Thread updateBalls = new updateBalls();

public class updateBalls extends Thread implements Runnable
{
public void run()
{
for(t = 0; t < 10; t++)
{
try
{
Thread.sleep(1000);
}
catch(InterruptedException e){}
}
if(t == 5)
{
randomNumber();
}
}
}
[/source]

now i need to repaint the screen every 5 seconds somehow Edited by burnt_casadilla

Share this post


Link to post
Share on other sites
You solved them with magic! :)

So, for one thing you need to work on that Ball class' paint method. There shouldn't be a loop in it. One of my longer posts a few things back talks about why and how.

Those number are changing fast in there because you do get a different number every time you call the randomNumber method. But you aren't actually use that number anywhere.. just printing it out on the screen.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
Excellent. Now that's a solid little Ball class. Does that leave you now working on getting them appearing every 10 seconds?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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] Edited by burnt_casadilla

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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] Edited by j-locke

Share this post


Link to post
Share on other sites
After a break from programming for 8 hours straight and a good nights sleep, this finally makes sense to me. thank you biggrin.png

oh and i like the array list. good thinking cuz im gonna want even more dots when its finished. my ultimate goal is to create dots every 5 seconds and keep creating them until the main dot comes in contact with one and then its game over Edited by burnt_casadilla

Share this post


Link to post
Share on other sites
in this block of code

[source lang="java"] public void update(Graphics g)
{

updateGame();

g.setColor(Color.BLACK);
g.fillRect(0, 0, WIDTH, HEIGHT);

g.setColor(Color.cyan);

for (ball ball : ball)
{
ball.paint(g);
}
}
[/source]

im getting an error that says "can only iterate over an instance of an array" and i have no idea what that means lol

Share this post


Link to post
Share on other sites
It's talking about your line for (ball ball : ball).

How that line is supposed to read is for (<datatype> <variable-name> : <collection-object>).
So with you saying ball 3 times, I'm not sure what you intended to put there. With the usual Java naming convention, you should name your classes (which are data types that created by you) with a capital letter (which you had done last time I checked, except where you named the game class lowercase).

So maybe you meant for (Ball ball : balls). Since an array represents a collection of things (usually more than 1), I find it useful to name the collection as plural.

Directly to your error, it sounds like your array is not named ball so the third 'ball' in your statement should be changed to match the array's name.

Share this post


Link to post
Share on other sites

After a break from programming for 8 hours straight and a good nights sleep, this finally makes sense to me. thank you biggrin.png

oh and i like the array list. good thinking cuz im gonna want even more dots when its finished. my ultimate goal is to create dots every 5 seconds and keep creating them until the main dot comes in contact with one and then its game over


You're welcome. Glad I could provide some help. And getting your eyes off the code can be the best approach to understanding sometimes! :)

Good to know what your game is actually about now. For the collision, ball.getBounds() and/or ball.getBounds2D() could probably prove helpful.

.getBounds() documentation: http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/geom/RectangularShape.html#getBounds()
.getBounds2D() documentation: http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/geom/Ellipse2D.Float.html#getBounds2D()

Share this post


Link to post
Share on other sites
i decided to take it one step at a time since i was having more trouble. im starting with switching the balls to an array list and getting that running first before trying the time thing

Share this post


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

  • Advertisement