Sign in to follow this  
Followers 0
ChristianFrantz

thread error

139 posts in this topic

yeah right now it just creates an oval at 150, 150 and it does nothing lol. but the dot is movable through the sides of the screen. now im trying to make an array to store different dots and then draw the dots to the screen. its not going to well

[source lang="java"] final int NUM_BALLS = 4;

Ball b[];

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);
}

class AnimationThread implements Runnable
{
JApplet c;

public AnimationThread(game game)
{
this.c = c;
}

public void run()
{
c.repaint();
}
}

class Ball extends Ellipse2D.Float
{
public Ball(int x, int y, int width, int height, Color mycolor) {

}
public Ball() {
// TODO Auto-generated constructor stub
}
int x;
int y;
int width;
int height;
Color mycolor;

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

}

public void Ball(int x, int y, int width, int height, Color mycolor)
{
b = new Ball[NUM_BALLS];
b[0] = new Ball(10, 10, 15, 15, Color.yellow);
b[1] = new Ball(30, 30, 15, 15, Color.red);
b[2] = new Ball(50, 50, 15, 15, Color.blue);
b[3] = new Ball(70, 70, 15, 15, Color.red);

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

ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(3);
executor.scheduleAtFixedRate(new AnimationThread(this), 0L, 20L, TimeUnit.MILLISECONDS);

}

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

}

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)
{

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 = new Ball();

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

for(int i = 0; i<NUM_BALLS; i++)
{
b[i].paint(g);
}

}
}[/source]

the problem is in my ball class, i already know that

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

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

}

public void Ball(int x, int y, int width, int height, Color mycolor)
{
b = new Ball[NUM_BALLS];
b[0] = new Ball(10, 10, 15, 15, Color.yellow);
b[1] = new Ball(30, 30, 15, 15, Color.red);
b[2] = new Ball(50, 50, 15, 15, Color.blue);
b[3] = new Ball(70, 70, 15, 15, Color.red);

}[/source]

i know i have to make a constructor for the Ball class somewhere but idk where Edited by burnt_casadilla
0

Share this post


Link to post
Share on other sites
[source lang="java"] class Ball extends Ellipse2D.Float
{
int x;
int y;
int width;
int height;
Color mycolor;

public Ball(int x, int y, int width, int height, Color mycolor)
{
b = new Ball[NUM_BALLS];
b[0] = new Ball(10, 10, 15, 15, Color.yellow);
b[1] = new Ball(30, 30, 15, 15, Color.red);
b[2] = new Ball(50, 50, 15, 15, Color.blue);
b[3] = new Ball(70, 70, 15, 15, Color.red);

}

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

}
[/source]

changed it to this, but the paint method at the bottom isnt reaching the variables from the constructor

[source lang="java"] public void paint(Graphics g)
{
Ball ball = new Ball(x, y, width, height, mycolor);

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

for(int i = 0; i<NUM_BALLS; i++)
{
b[i].paint(g);
}

}
}[/source]
0

Share this post


Link to post
Share on other sites
In the second code block on your most recent post, is there more code to that for loop? It looks like it's cutting off in the middle of the loop for me.
[i]for(int i = 0; i[/i]
0

Share this post


Link to post
Share on other sites
for(int i = 0; i<NUM_BALLS; i++)
{
b[i].paint(g);
}

}
}

yeah idk why it does that lol Edited by burnt_casadilla
0

Share this post


Link to post
Share on other sites
Can you post you whole current code file. I'm gonna paste it into an IDE and take a look at running it myself.
0

Share this post


Link to post
Share on other sites
this is what i have so far. i keep changing things around hoping something will work

import java.applet.*;
import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;
import java.util.ArrayList;
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;
static final int WIDTH = 400;
static final int HEIGHT = 400;
private Image dbImage;
private Graphics dbg;
final int NUM_BALLS = 4;

Ball b[];

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);
}

class AnimationThread implements Runnable
{
JApplet c;

public AnimationThread(game game)
{
this.c = c;
}

public void run()
{
c.repaint();
}
}

class Ball extends Ellipse2D.Float
{
int ballx;
int bally;
int ballwidth;
int ballheight;
Color mycolor;

public Ball(int ballx, int bally, int ballwidth, int ballheight, Color mycolor)
{
b = new Ball[NUM_BALLS];
b[0] = new Ball(10, 10, 15, 15, Color.yellow);
b[1] = new Ball(30, 30, 15, 15, Color.red);
b[2] = new Ball(50, 50, 15, 15, Color.blue);
b[3] = new Ball(70, 70, 15, 15, Color.green);

}

public void paint(Graphics g)
{
g.setColor(mycolor);
g.fillOval(ballx, bally, ballwidth, ballheight);
}
}
public void init()
{
this.setSize(WIDTH, HEIGHT);

ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(3);
executor.scheduleAtFixedRate(new AnimationThread(this), 0L, 20L, TimeUnit.MILLISECONDS);

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

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)
{

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 = new Ball();
for(int i = 0; i<NUM_BALLS; i++)
{
b[i].paint(g);
}


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

}
}
0

Share this post


Link to post
Share on other sites
From what i can tell, i shouldnt have another paint method in my ball class and should just call the ball class from the original paint method at the bottom
0

Share this post


Link to post
Share on other sites
What is this version of the code doing for you?

For me, it didn't compile right away. The line [i]Ball ball = new Ball();[/i] is trying to call a method that doesn't exist (there is no empty constructor for the Ball class).

When I changed that line so it was calling the real constructor, I get a stack overflow error (Exception in thread "AWT-EventQueue-1" java.lang.StackOverflowError) because when I call the constructor once, it recursively calls the constructor again and again until I'm out of available stack memory.

So, I wanted to see what kind of things your'e experiencing before we overhaul this and try to adjust the mindset of this approach.

edit: restating the reason for StackOverflowError Edited by j-locke
0

Share this post


Link to post
Share on other sites
i moved all of the initializing and drawing within the ball class, but i think its not drawing anything because its in the ball class so i have to somehow draw it from the main game class but whenver i do that i get an error

[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 = 400;
static final int HEIGHT = 400;
private Image dbImage;
private Graphics dbg;

final int NUM_BALLS = 4;

Ball b[];

public class Ball extends Ellipse2D.Float
{
int ballx;
int bally;
int ballwidth;
int ballheight;
Color mycolor;

public Ball(int ballx, int bally, int ballwidth, int ballheight, Color mycolor)
{
this.ballx = ballx;
this.bally = bally;
this.ballwidth = ballwidth;
this.ballheight = ballheight;
}
public void initBall()
{
b = new Ball[NUM_BALLS];
b[0] = new Ball(10, 10, 15, 15, Color.yellow);
b[1] = new Ball(30, 30, 15, 15, Color.red);
b[2] = new Ball(50, 50, 15, 15, Color.blue);
b[3] = new Ball(70, 70, 15, 15, Color.green);

}


public void paint(Graphics g) {

Ball ball = new Ball(ballx, bally, ballwidth, ballheight, mycolor);

for(int i = 0; iGREATERTHANNUM_BALLS; i++)
{
b[i].paint(g);
}

g.setColor(mycolor);
g.fillOval(ballx, bally, ballwidth, ballheight);

}

public void init()
{
initBall();
}

}
[/source]

unless there was a way to run the paint method within the BALL class, its not drawing anything but the main dot which is not in the ball class

when i move the paint method in the ball class to outside of the ball class, it cant call the variables used to create the ball.

i dont get any errors running it with the above code

i created a new file that holds the ball class so its a little less confusing and add package.game; to the top

im wondering if it would be easier to put the draw method into a thread and then run the thread from the main class... Edited by burnt_casadilla
0

Share this post


Link to post
Share on other sites
Yes. Much better use of what a class constructor is supposed to do!

And moving the creation of the objects to an initialization method is very good too. Even better if the initBall method is moved out of the Ball class so it just resides in the game class. As a way to mentally approach that, should a Ball (which is what that class is designed to represent) know about creating and populating an array of balls; in my mind that's definitely a no.
In fact, I kind of think that maybe you do have initBall outside of the Ball class.. and maybe just in pasting the code, a closing brace was accidentally left out, but I can't say for sure of course.

What is the error and what line is the error on that it's giving you? I see it looks like in the Ball constructor you're not saving the passed in color.
0

Share this post


Link to post
Share on other sites
I'm not getting any errors anymore. Im thinking i have to initialize the whole Ball class in my main class because even tho im getting no errors, nothing is getting drawn but the main ball

alright i cleaned it up a bit

main GAME class

[source lang="java"]package Ball;

import game.Ball;

import java.applet.*;
import java.awt.*;

import javax.swing.*;
import java.awt.geom.*;
import java.util.ArrayList;
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;
static final int WIDTH = 400;
static final int HEIGHT = 400;
private Image dbImage;
private Graphics dbg;

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)
{
g.setColor(Color.black);
g.fillOval(xpos - radius, ypos - radius, 2 * radius, 2 * radius);

}

}
[/source]

BALL class in another file

[source lang="java"]package game;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.geom.Ellipse2D;

public class Ball extends Ellipse2D.Float
{
final int NUM_BALLS = 4;

Ball b[];

int ballx;
int bally;
int ballwidth;
int ballheight;
Color mycolor;

public Ball(int ballx, int bally, int ballwidth, int ballheight, Color mycolor)
{
this.ballx = ballx;
this.bally = bally;
this.ballwidth = ballwidth;
this.ballheight = ballheight;
}
public void initBall()
{
b = new Ball[NUM_BALLS];
b[0] = new Ball(10, 10, 15, 15, Color.yellow);
b[1] = new Ball(30, 30, 15, 15, Color.red);
b[2] = new Ball(50, 50, 15, 15, Color.blue);
b[3] = new Ball(70, 70, 15, 15, Color.green);

}

}[/source]

now where and how in the main game class would i put some code in order to run the ball class? Edited by burnt_casadilla
0

Share this post


Link to post
Share on other sites
The code is changing so frequently that I can't really speak on any version of the code because it seems it's changed as soon as you post it on here. So let me throw out some general things to look at and think about.

1. Don't create a new object in your paint method. If you're calling new in your paint method, get it out. Get it to an init method that makes sense for whatever it's initializing.

2. Make the Ball class represent one Ball. So give it the information it needs to represent that one ball and don't let it have any methods beyond that (so the Ball class should know nothing about this array of Ball's that the game is managing).

3. Take your code to where it's working (that was definitely happening with the approach you had implemented as of last night). And then make smaller changes to try to get it where you want it. So for this one I mean, don't try to change up 4 or 5 things at once. Just one at a time. So revert to drawing the ball object in the game's paint method. And then when you have that working, try to EITHER try to move drawing that one object into the Ball class OR try to draw the array of Ball's all from the game's paint method. But do one and get it working like you want first then you can take on the other one.

I think using number 3 will get you back to some working code that is progressing towards where you want it to be.
And using numbers 1 and 2 will get you writing code with some good attributes.
1

Share this post


Link to post
Share on other sites
Good thinking. Now i got 4 balls on the screen [img]http://public.gamedev.net//public/style_emoticons/default/biggrin.png[/img]

[source lang="java"]

import game.Ball;

import java.applet.*;
import java.awt.*;

import javax.swing.*;
import java.awt.geom.*;
import java.util.ArrayList;
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;
static final int WIDTH = 400;
static final int HEIGHT = 400;
private Image dbImage;
private Graphics dbg;

class Ball1 extends Ellipse2D.Float
{
int x = 150;
int y = 150;
int width = 20;
int height = 20;
}

class Ball2 extends Ellipse2D.Float
{
int x = 200;
int y = 200;
int width = 20;
int height = 20;
}

class Ball3 extends Ellipse2D.Float
{
int x = 50;
int y = 50;
int width = 20;
int height = 20;
}

class Ball4 extends Ellipse2D.Float
{
int x = 100;
int y = 100;
int width = 20;
int height = 20;
}
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)
{
Ball1 ball1 = new Ball1();
Ball2 ball2 = new Ball2();
Ball3 ball3 = new Ball3();
Ball4 ball4 = new Ball4();

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

g.setColor(Color.blue);
g.fillOval((int)ball1.x, (int)ball1.y, (int)ball1.width, (int)ball1.height);

g.setColor(Color.red);
g.fillOval((int)ball2.x, (int)ball2.y, (int)ball2.width, (int)ball2.height);

g.setColor(Color.green);
g.fillOval((int)ball3.x, (int)ball3.y, (int)ball3.width, (int)ball3.height);

g.setColor(Color.magenta);
g.fillOval((int)ball4.x, (int)ball4.y, (int)ball4.width, (int)ball4.height);

}

}[/source]

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? Edited by burnt_casadilla
0

Share this post


Link to post
Share on other sites
Awesome on the 4 drawn!

Now in the spirit of small steps, try these things, but do them one at a time so you can get that small piece working before bringing it all together.

1. Get it so all of those are using the same Ball class. So no classes named Ball1, Ball2, etc. Just have variables named like that. [i]So [/i][i]Ball ball1[/i][i] = new Ball(); [/i]and so on.

2. Then get it so those balls are in one array. Like [i]Ball balls[[/i]]; And then you can draw those balls from game's paint method with a for loop.
[code]
for (int i = 0; i< NUM_OF_BALLS; i++)
{
g.setColor(balls[i].myColor);
g.fillOval((int)balls[i].x, (int)balls[i].y, (int)balls[i].width, (int)balls[i].height);
}
[/code]
0

Share this post


Link to post
Share on other sites
[source lang="java"] class Ball
{
int x;
int y;
int width;
int height;

public Ball(int x, int y, int width, int height)
{
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
}
Ball ball1 = new Ball(100, 100, 20, 20);
Ball ball2 = new Ball(200, 200, 20, 20);
Ball ball3 = new Ball(50, 50, 20, 20);
Ball ball4 = new Ball(25, 25, 20, 20);

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)
{

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

g.setColor(Color.blue);
g.fillOval((int)ball1.x, (int)ball1.y, (int)ball1.width, (int)ball1.height);

g.setColor(Color.red);
g.fillOval((int)ball2.x, (int)ball2.y, (int)ball2.width, (int)ball2.height);

g.setColor(Color.green);
g.fillOval((int)ball3.x, (int)ball3.y, (int)ball3.width, (int)ball3.height);

g.setColor(Color.magenta);
g.fillOval((int)ball4.x, (int)ball4.y, (int)ball4.width, (int)ball4.height);

}

}

[/source]

got number 1 i think Edited by burnt_casadilla
0

Share this post


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

Share this post


Link to post
Share on other sites
[quote name='burnt_casadilla' timestamp='1344714359' post='4968486']
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?
[/quote]

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 [i]Ball ball1[/i] part). You also happen to be initializing them (that's the [i]ball1 = new Ball1()[/i] 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.
1

Share this post


Link to post
Share on other sites
Good job getting both of those steps nailed down. Now get those new calls out of the paint method. :)
0

Share this post


Link to post
Share on other sites
which i cant figure out >[img]http://public.gamedev.net//public/style_emoticons/default/sad.png[/img] 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
0

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
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 ([i]public class game extends Applet[/i]). 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 [b]using a line of code to set the color[/b] and a [b]line of code to draw a filled oval[/b].

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
0

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
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, [i]randomNumber[/i]) 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 ([i]return (int)(Math.random() * 400);[/i]). With that method in place, you can call [i]randomNumber() [/i]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 ([i]ball[0] = new Ball(randomNumber(), randomNumber(), 20, 20, Color.red)[/i]).

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.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0