Advertisement

thread error

Started by August 06, 2012 12:02 AM
138 comments, last by ChristianFrantz 12 years, 2 months ago
As soon as I got to the (Ball ball); part I had to hit myself because I knew that and it's so simple -_- but yeah that'll work for now until I can understand how to draw multiple balls at different times

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

Ah, that's programming. That's why we all keep writing code though; because there's always something else for us to learn. And once you've learned it, hopefully it'll stick with you so you can use it next time a similar situation comes up.

While it's not quite your final goal, you should now be able to pretty easily change that code to handle say 10 balls instead of just one. This would create all 10 and have all 10 at random locations right at the start (so not appearing over time like you eventually want). But as a good step in the right direction, you could add the code to make that a reality. A collection (like ArrayList that you tried using earlier) and a for loop should allow you to make that change by only adding 5 or so lines of code.
Advertisement
[source lang="java"] 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 = 150;
int y = 150;
int width = 20;
int height = 20;

}
}

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

g.drawOval((int)ball.x, (int)ball.y, (int)ball.width, (int)ball.height);
}
}
[/source]

ok now what? in the ball class i created variables that define the x, y, width and height for the ball and created a new ball in the public void paint(Graphics g) method and then drew the ball to the variables, but no ball is being drawn :(

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

hahahahahahahaaha finally!!! all i did was change the ball class to this

[source lang="java"]class Ball extends Ellipse2D.Float
{

int x = 150;
int y = 150;
int width = 20;
int height = 20;

}
[/source]

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

Nice job going back over the code and cleaning it up a bit.

One problem with what you're doing is you're creating a new instance of Ball everytime the paint method gets called (which is a LOT). You could likely get away with doing it through a smaller game, but I'd say it's worth breaking that habit right now. The init method is a much better place for creating that Ball object.

So, what do you have your project doing right now? Drawing the one oval and the one dot? Using the arrow keys to move the oval around? Wrapping that oval from one side of the screen to the other when it reaches the end?
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.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

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

Advertisement
[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.paint(g);
}

}
}[/source]

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

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

}
}

yeah idk why it does that lol

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

Can you post you whole current code file. I'm gonna paste it into an IDE and take a look at running it myself.

This topic is closed to new replies.

Advertisement