Jump to content

  • Log In with Google      Sign In   
  • Create Account


thread error


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
139 replies to this topic

#41 burnt_casadilla   Members   -  Reputation: 438

Like
0Likes
Like

Posted 11 August 2012 - 02:57 PM

yay got number 2!!

[source lang="java"] static final int NUM_OF_BALLS = 4; 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; } } 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); } 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 = 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){} } } public void paint(Graphics g) { Ball ball[]; ball = new Ball[NUM_OF_BALLS]; ball[0] = new Ball(100, 100, 20, 20, Color.red); ball[1] = new Ball(200, 200, 20, 20, Color.blue); ball[2] = new Ball(50, 50, 20, 20, Color.green); ball[3] = new Ball(25, 25, 20, 20, Color.magenta); for(int i = 0; i<NUM_OF_BALLS; i++) { g.setColor(ball[i].mycolor); g.fillOval((int)ball[i].x, (int)ball[i].y, (int)ball[i].width, (int)ball[i].height); } g.setColor(Color.black); g.fillOval(xpos - radius, ypos - radius, 2 * radius, 2 * radius); } }[/source]

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


Sponsor:

#42 j-locke   Members   -  Reputation: 815

Like
1Likes
Like

Posted 11 August 2012 - 03:01 PM

but when i move the
Ball1 ball1 = new Ball1();
Ball2 ball2 = new Ball2();
Ball3 ball3 = new Ball3();
Ball4 ball4 = new Ball4();
to the init method, i cant call the variables through the paint method

is there a reason i cant call a paint method from each class?


Sorry, I loaded the page up and apparently didn't reload after this was added.. or I simply overlooked it. Both are valid possibilities; I was killing zombies in StarCraft 2 while typing my response...

The reason you can't move those lines entirely into the init method is when you do that you're declaring those variables inside the init method (declaring is the Ball ball1 part). You also happen to be initializing them (that's the ball1 = new Ball1() part), but don't let that make you forget that you're doing 2 "activities/actions/things" with 1 line of code. When you DECLARE a variable inside a method, that variable goes out of scope (can't reliably be accessed anymore) when you leave that method.

So to do what you're trying to do, you want to DECLARE the variables inside the game class (making it a field of the game class and available everywhere within the game class, including inside the game class's paint method). And then INITIALIZE them inside the init method.

#43 j-locke   Members   -  Reputation: 815

Like
0Likes
Like

Posted 11 August 2012 - 03:03 PM

Good job getting both of those steps nailed down. Now get those new calls out of the paint method. :)

#44 burnt_casadilla   Members   -  Reputation: 438

Like
0Likes
Like

Posted 11 August 2012 - 03:06 PM

which i cant figure out >Posted Image ill keep trying lol

ooh nevermind that was easy -.-

now i need to figure out how to get 10 dots on the screen after waiting 10 seconds after each dot

first i want a timer in the top left corner somehow

Edited by burnt_casadilla, 11 August 2012 - 03:13 PM.

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


#45 j-locke   Members   -  Reputation: 815

Like
0Likes
Like

Posted 11 August 2012 - 03:20 PM

So, if you've now gotten the new calls out of the paint method, it sounds like you're in a good position to try having the Ball's paint themselves.

Seeing if it's gonna work should be very easy. If it doesn't, solving it could be a much larger undertaking... So to try that, inside the for loop in game's paint method, call ball[i].paint.

And then in the Ball class' paint method, have it setColor and fillOval. I actually don't know if you can safely pass the Graphics object from the game object to the Ball object. I haven't made enough applets to be able to say for sure. It was mentioned earlier in this thread by BitMaster that you won't be able to pass the Graphics object to other threads. But your code is in such a place right now, that it should be just a matter of adding a few lines of code and deleting a few lines of code and you'll be able to know for sure.

#46 burnt_casadilla   Members   -  Reputation: 438

Like
0Likes
Like

Posted 11 August 2012 - 03:23 PM

So i have to add a paint method to my ball class and then call the ball paint method from the main paint method? im confused lol

when i created a paint method into the ball class and added the for method in there, nothing was drawn to the screen
[source lang="java"]int i;[/source]

[source lang="java"] 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; } public void paint(Graphics g) { for(i = 0; iLESSTHANNUM_OF_BALLS; i++) { g.setColor(ball[i].mycolor); g.fillOval((int)ball[i].x, (int)ball[i].y, (int)ball[i].width, (int)ball[i].height); } } }[/source]
[source lang="java"] public void paint(Graphics g) { ball[i].paint(g); g.setColor(Color.black); g.fillOval(xpos - radius, ypos - radius, 2 * radius, 2 * radius); }[/source]



oops forgot the for loop in the main paint method. works perfectly now

[source lang="java"] public void paint(Graphics g) { for(i = 0; iLESSTHANNUM_OF_BALLS; i++) { ball[i].paint(g); } g.setColor(Color.black); g.fillOval(xpos - radius, ypos - radius, 2 * radius, 2 * radius); } }[/source]

Edited by burnt_casadilla, 11 August 2012 - 03:40 PM.

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


#47 j-locke   Members   -  Reputation: 815

Like
0Likes
Like

Posted 11 August 2012 - 03:46 PM

Haha, no problem. What you described is right.

First, a little insight into what's happening.
As you've seen, the underlying framework will call the Applet's paint method for you whenever it needs to be repainted. You've extended the Applet class with your game class (public class game extends Applet). So now the underlying framework is calling the paint method in your game class.
Right now you're drawing the balls directly in that method.

What you're gonna try now is instead of drawing directly in the game class' paint method is to call the ball class' paint method and let the ball take care of drawing itself.

So, things you need to actually do are:
1. Inside the Ball class, add a paint method that takes a Graphics object as a parameter. Then use that Graphics object to draw itself. So, from your existing code you can see that drawing a ball means using a line of code to set the color and a line of code to draw a filled oval.

So after step 1, your code should be able to compile. Nothing will have changed yet though. If you run it, it will be exactly like before. Because even though you've added that method with the code inside of it, nobody is calling that method so it doesn't affect how your program actually runs.

2. For this part, you're going to put the new method from step 1 to use. Inside game's paint method, you have a loop that loops through each ball in the array of balls. What you want to do is call the method from step 1 on each of the balls in that array.

Edited by j-locke, 11 August 2012 - 03:48 PM.


#48 burnt_casadilla   Members   -  Reputation: 438

Like
0Likes
Like

Posted 11 August 2012 - 03:56 PM

ok so now im creating two random x and y integers and setting the balls at those points, but only one is showing up. i think all the balls are getting the same point and drawing over each other

[source lang="java"] int randomx = (int)(Math.random() * 400 - 20); int randomy = (int)(Math.random() * 400 - 20); 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 = randomx; this.y = randomy; 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[i].mycolor); g.fillOval((int)ball[i].x, (int)ball[i].y, (int)ball[i].width, (int)ball[i].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(randomx, randomy, 20, 20, Color.red); ball[1] = new Ball(randomx, randomy, 20, 20, Color.blue); ball[2] = new Ball(randomx, randomy, 20, 20, Color.green); ball[3] = new Ball(randomx, randomy, 20, 20, Color.magenta); }[/source]

Edited by burnt_casadilla, 11 August 2012 - 03:56 PM.

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


#49 burnt_casadilla   Members   -  Reputation: 438

Like
0Likes
Like

Posted 11 August 2012 - 04:36 PM

figured that out the hard way lol

[source lang="java"] public Ball ball[]; static final int NUM_OF_BALLS = 4; int i; int randomx = (int)(Math.random() * 400); int randomy = (int)(Math.random() * 400); int randomx2 = (int)(Math.random() * 400); int randomy2 = (int)(Math.random() * 400); int randomx3 = (int)(Math.random() * 400); int randomy3 = (int)(Math.random() * 400); int randomx4 = (int)(Math.random() * 400); int randomy4 = (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[i].mycolor); g.fillOval((int)ball[i].x, (int)ball[i].y, (int)ball[i].width, (int)ball[i].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(randomx, randomy, 20, 20, Color.red); ball[1] = new Ball(randomx2, randomy2, 20, 20, Color.blue); ball[2] = new Ball(randomx3, randomy3, 20, 20, Color.green); ball[3] = new Ball(randomx4, randomy4, 20, 20, Color.magenta); System.out.println(randomx + " " + randomy); System.out.println(randomx2 + " " + randomy2); System.out.println(randomx3 + " " + randomy3); System.out.println(randomx4 + " " + randomy4);[/source]

Edited by burnt_casadilla, 11 August 2012 - 04:38 PM.

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


#50 j-locke   Members   -  Reputation: 815

Like
0Likes
Like

Posted 11 August 2012 - 04:45 PM

Alright, cool that drawing from Ball worked. Now, one concern and then an answer to your random situation.

Right now, your Ball class' paint method goes through the array and draws each of the balls. This goes back to a couple of our earlier posts about the idea that a Ball object should represent 1 ball. So it should not know or care about the fact that you have an array of balls stored in the game class. So in the paint method for Ball, it shouldn't have a loop; it should simply draw itself.

As you need to draw multiple balls, that means in your game class' paint method, you do want to have a loop. Since that object is representing the whole game, it's fine that it knows about there being an array of balls.

On to the random number issue you've encountered. What you thought is exactly right! you are creating a random number and storing it in randomx and creating another random number and storing it in randomy. You then use those 2 numbers over and over as you create the balls. In fact, with the current code, you're using these numbers even more than you intend to. In the game class' init method, you're passing in randomx, randomy. And then in the Ball class' constructor you're using the variables randomx and randomy rather than the x and y values that you pass into the constructor.

In this case, I'd say change the Ball constructor so that it uses the x and y values that are passed into it. Similar to above, that lets the Ball class not need to worry about things like if numbers are hand-chosen or randomly-generated or whatever else you may come up with for choosing positioning.

As usual in programming, there are lots of ways to solve the problem. One of the many ways to get the random numbers doing what you want is to make a method that will return you a random number using the formula you've decided to use for generating random numbers. So what that approach would look like is making a method in the game class (named for example, randomNumber) that takes no parameters and returns an int. Inside that method, you could just generate a random number using your equation and return that number (return (int)(Math.random() * 400);). With that method in place, you can call randomNumber() any time you want to get a new random number based on that equation. One place where you'd be wanting to use that in creating your array of balls (ball[0] = new Ball(randomNumber(), randomNumber(), 20, 20, Color.red)).

There is a LOT in this post, I realize as I wrap it up. So definitely remember that this isn't intended to be something to do in one fell swoop. There are several sub-steps within this post that can even be broken down into sub-steps themselves.

#51 j-locke   Members   -  Reputation: 815

Like
0Likes
Like

Posted 11 August 2012 - 04:49 PM

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!

#52 burnt_casadilla   Members   -  Reputation: 438

Like
0Likes
Like

Posted 11 August 2012 - 05:02 PM

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

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


#53 j-locke   Members   -  Reputation: 815

Like
0Likes
Like

Posted 11 August 2012 - 05:15 PM

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.

#54 burnt_casadilla   Members   -  Reputation: 438

Like
0Likes
Like

Posted 11 August 2012 - 05:18 PM

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

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


#55 j-locke   Members   -  Reputation: 815

Like
0Likes
Like

Posted 11 August 2012 - 05:25 PM

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.

#56 burnt_casadilla   Members   -  Reputation: 438

Like
0Likes
Like

Posted 11 August 2012 - 05:43 PM

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[i].mycolor); g.fillOval((int)ball[i].x, (int)ball[i].y, (int)ball[i].width, (int)ball[i].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[i].paint(g); } g.setColor(Color.black); g.fillOval(xpos - radius, ypos - radius, 2 * radius, 2 * radius); }}[/source]

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


#57 j-locke   Members   -  Reputation: 815

Like
0Likes
Like

Posted 11 August 2012 - 05:45 PM

What errors is it giving you? and what line of code?

#58 burnt_casadilla   Members   -  Reputation: 438

Like
0Likes
Like

Posted 11 August 2012 - 05:49 PM

no errors anymore. idk what i did to give me errors Posted Image 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, 11 August 2012 - 06:14 PM.

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


#59 j-locke   Members   -  Reputation: 815

Like
0Likes
Like

Posted 11 August 2012 - 06:10 PM

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.

#60 burnt_casadilla   Members   -  Reputation: 438

Like
0Likes
Like

Posted 11 August 2012 - 06:16 PM

When i deleted the for loop, nothing worked at all. it drew one red ball on the screen and i couldnt close the app lol

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





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS