Help!! - Questions

E

Evil Monkey

Hi there,

I am having a bit of trouble with a game I am building based on breakout.

I am trying to animate a ball travelling across a screen but i am having a
problem with the screen flickering when I animate it.

As I understand it the problem can be resolved using a double buffer.

I am using a class that extends JPanel (part of swing) which is meant to
solve the problem automatically but it dosent.How do I tell JPanel to use a
double buffer so the animation is smooth??

My other problem is that the ball is moving too quickly ..what would I need
to do to slow it down?? The ball is currently set to increment the variables
delta x and y by
only a few pixels before repainting it. Can anyone suggest how I can slow it
down? Should I be looking to use a timer?

I woudl like to put soem numbers on my background which is set to
black..where will I need to look to solve this problem ??

My last problem is that I have a rectangle drawn from a rectangle class i
have defined that I can move using a keyListener. However I
know I need to use multithreading to allow them to move at the *same* time.
Where should i look to do this??Would it be the rectangle class or the class
that extends JPanel that eeds to implement runnable?

mant thanks for any help

regards
 
R

Ryan Stewart

Evil Monkey said:
Hi there,

I am having a bit of trouble with a game I am building based on breakout.

I am trying to animate a ball travelling across a screen but i am having a
problem with the screen flickering when I animate it.

As I understand it the problem can be resolved using a double buffer.

I am using a class that extends JPanel (part of swing) which is meant to
solve the problem automatically but it dosent.How do I tell JPanel to use a
double buffer so the animation is smooth??
JPanel.setDoubleBuffered(boolean aFlag)

I've never touched it though, so I can't say that it'll do what you want.
Why not create a BufferedImage, draw to it, then draw the image to the
JPanel when it's finished?
My other problem is that the ball is moving too quickly ..what would I need
to do to slow it down?? The ball is currently set to increment the variables
delta x and y by
only a few pixels before repainting it. Can anyone suggest how I can slow it
down? Should I be looking to use a timer?
No timer. What's "a few pixels"? And what's your framerate? Reduce those
numbers to slow the ball down.
I woudl like to put soem numbers on my background which is set to
black..where will I need to look to solve this problem ??
Graphics.drawString(String str, int x, int y)
My last problem is that I have a rectangle drawn from a rectangle class i
have defined that I can move using a keyListener. However I
know I need to use multithreading to allow them to move at the *same* time.
Where should i look to do this??Would it be the rectangle class or the class
that extends JPanel that eeds to implement runnable?
I would suspect it should be in your main game class. Based on your
questions, it seems you're somewhat stumbling along on your own. I'd suggest
getting a book to guide you. I found Java 1.4 Game Programming by Andrew
Mulhollnad and Glenn Murphy to be an excellent starting guide.
 
H

Harald Hein

Evil Monkey said:
As I understand it the problem can be resolved using a double
buffer.

Maybe, if the problem is indeed due to your painting. Double buffering
does not smooth an animation per se. It is just a technique where you
paint all changes into a buffer and not directly to a display. Once you
are done with changing things, you draw (BitBlt) the whole buffer to
the screen in one run. This display update is supposed to be done
during the frame flyback, so the new display contents becomes visible
with the next frame, and the update is not split over two or more
frames (you can't get information about the frame flyback timing in
Java, so you have to hope that the Graphics methods doing the BitBlt
are correctly implemented).
I am using a class that extends JPanel (part of swing) which is
meant to solve the problem automatically but it dosent.How do I
tell JPanel to use a double buffer so the animation is smooth??

It is on by default for Swing components. You have to turn it off
explicitely if you don't want it. This is why I started my first
sentence in this posting with a "maybe". Are you sure your animation
problems are due to the screen painting?
My other problem is that the ball is moving too quickly ..what
would I need to do to slow it down?? The ball is currently set to
increment the variables delta x and y by
only a few pixels before repainting it. Can anyone suggest how I
can slow it down? Should I be looking to use a timer?

Sounds like a plan :).
I woudl like to put soem numbers on my background which is set to
black..where will I need to look to solve this problem ??

What's the problem? Set the number's color to white and paint them.
My last problem is that I have a rectangle drawn from a rectangle
class i have defined that I can move using a keyListener. However
I know I need to use multithreading to allow them to move at the
*same* time.

Not necessarily. *same* time is a rather vague term in this context. As
long as both movements are finished before the next frame is painted,
you can do the moves in serial order. In fact, you should. You will
find that multithreading will not speed up these kind of things on a
single CPU machine. There is only one CPU, so the operations will
anyhow be executed one after the other. Plus you will get some
threading overhead.

Let's say you want to paint 15 frame / second. This gives you 1/15 of a
second to do all the processing and painting (to a double buffer) which
is necessary to update the display. Either you manage to do all the
calculations in that time, or you hve to reduce the frame rate. At some
point, the frame rate will be so low that the game becomes unusable. At
this point you either have to tune your algorithms, or get a faster
computer :)
Where should i look to do this??

A good book about computer graphics and especially simple computer
games.
 
E

Evil Monkey

hi again,

I used the BufferImage technique and that worked .. the only thing is it
look at little jerky but at least is much slower so i can see what is
happening. - anyway to solve this??

the way my animation works is that JPanel uses rectangle and ball classes to
drwa these objects on the panel and fills them using its paint component
method. The paint component method then calls a bounce ball method which
moves the poistions of the ball and then ive used the repaint call to
animate it.

Any idea how do you play about with javas framerate ??

thanks for the tip on DrawString method that workled a treat! - just what i
needed. : )

Im going to try and get the book you recommended. I have a good idea of java
but its the first time ive used the graphics area of it. I have a text to
follow but I cant get my head around it.
 
A

Andrew Thompson

....
I am having a bit of trouble with a game I am building based on breakout.

Cool game, or at least, I assume it is,
since you did not supply sample code.

It is a bit hard to help you without a
short, self contained, compileable, example..
 
R

Ryan Stewart

Evil Monkey said:
hi again,

I used the BufferImage technique and that worked .. the only thing is it
look at little jerky but at least is much slower so i can see what is
happening. - anyway to solve this??
From what you say in the next paragraph, it appears you're using the
paint(Graphics g) method and repaint() calls? That's not a good way to go
about it. You should call setIgnoreRepaint(true) early in your code. You're
going to be manually refreshing the screen 20 or 30 or 50 times a second
already, why let more slip through? It's better to forget paint() altogether
and just make a render() method or something like that. When you use
repaint() it only requests a call to the paint() method, it doesn't cause it
to happen. That may be causing some of your jerkiness. This is getting
pretty in depth. The book will lay it all out for you.
the way my animation works is that JPanel uses rectangle and ball classes to
drwa these objects on the panel and fills them using its paint component
method. The paint component method then calls a bounce ball method which
moves the poistions of the ball and then ive used the repaint call to
animate it.

Any idea how do you play about with javas framerate ??
There's not really any "Java's framerate". Framerate simply refers to how
many frames you draw per second. I suppose you could use a Timer to help,
but I have no idea how that would work or what kind of overhead is involved
with it. The animation code I've written in the past goes something like
this:

long startTime, waitTime, elapsedTime;
int delayTime 1000/25; // This determines the framerate. The 25 is how many
frames per second to draw.

while (whatever) { // This is the main game loop.
startTime = System.currentTimeMillis();
// Do your game logic and call your render method here.
elapsedTime = System.currentTimeMillis() - startTime;
waitTime = Math.max(delayTime - elapsedTime, 5);
try {
Thread.sleep(waitTime);
} catch (InterruptedException e) {
System.out.println(e);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top