Skip to main content

skippy animation

4 replies [Last post]
gamezealot
Offline
Joined: 2006-02-17

Heres my code. I have a lot of stuff commented out, thats for later use. But, its supposed to be a rectangle that travels across the bottom of the screen, i'm working on a game. I've already made Pong, kind of. Now i'm working on another game, but this is giving me a major problem right from the beginning. The rectangle will travel across the bottom, but run at like 3 frames a sec.

<br />
//Andrew Basti</p>
<p>import java.awt.event.*;<br />
import java.awt.*;<br />
import javax.swing.*;</p>
<p>public class Blimp extends JApplet implements ActionListener, KeyListener<br />
{<br />
	JPanel drawingArea = new JPanel();<br />
	JPanel scoreArea = new JPanel();</p>
<p>	JLabel score = new JLabel("Score ");<br />
	Timer timer1;<br />
	//Timer timer2;</p>
<p>	Graphics g;<br />
	Graphics gg;</p>
<p>	int play1 = 0;</p>
<p>	int timex = 50;<br />
	//int timey = 100;</p>
<p>	//String score = new String("Player 1: " + play1 + "          Player 2: " + play2);</p>
<p>	int xx = 0;<br />
	int yy = 0;<br />
	int x = 0;<br />
	int y = 300;<br />
	int h = 40;<br />
	int w = 20;</p>
<p>	//public boolean pressedRight = true;<br />
	//public boolean pressedLeft = false;</p>
<p>	Color bgColor = Color.DARK_GRAY;<br />
	//Color dropColor = Color.RED;</p>
<p>	public void init()<br />
	{<br />
		timer1 = new Timer(timex,this);<br />
		//timer1.setInitialDelay(3000);</p>
<p>		this.getContentPane().add(scoreArea, "North");<br />
		this.getContentPane().add(drawingArea, "Center");</p>
<p>		scoreArea.add(score);<br />
		drawingArea.setBackground(Color.DARK_GRAY);</p>
<p>		this.addKeyListener(this);<br />
	}<br />
	public void start()<br />
	{<br />
		g = drawingArea.getGraphics();<br />
		gg = scoreArea.getGraphics();<br />
		timer1.start();<br />
		//drawingArea.requestFocus();<br />
	}<br />
	public void stop()<br />
	{<br />
		timer1.stop();<br />
	}<br />
	public void actionPerformed(ActionEvent ae)<br />
	{<br />
		Object source = ae.getSource();</p>
<p>		if(source == timer1)<br />
		{<br />
			stepCatcher();<br />
			animateCatcher();<br />
		}<br />
	}<br />
	public void stepCatcher ()<br />
	{<br />
		xx=x;<br />
		yy=y;<br />
		x+=5;<br />
	}<br />
	public void animateCatcher ()<br />
	{<br />
		g.setColor(bgColor);<br />
		g.fillRect(xx,yy,h,w);<br />
		g.setColor(Color.YELLOW);<br />
		g.fillRect(x,y,h,w);<br />
	}<br />
	public void keyPressed(KeyEvent ke)<br />
	{<br />
		/*int which = ke.getKeyChar();<br />
		if (which == 'k' || which == 'K')<br />
		{<br />
			pressedRight = true;<br />
			pressedLeft = false;<br />
		}<br />
		else if (which == 'j' || which == 'J')<br />
		{<br />
			pressedLeft = true;<br />
			pressedRight = false;<br />
		}<br />
		//animate2();*/<br />
	}<br />
	public void keyReleased(KeyEvent ke){}<br />
	public void keyTyped(KeyEvent ke)<br />
	{<br />
		/*int which = ke.getKeyChar();<br />
		if (which == 'k' || which == 'K')<br />
		{<br />
			pressedRight = true;<br />
			pressedLeft = false;<br />
		}<br />
		else if (which == 'j' || which == 'J')<br />
		{<br />
			pressedLeft = true;<br />
			pressedRight = false;<br />
		}*/<br />
		//animate2();<br />
	}<br />
}<br />

Message was edited by: shan-man (added [ code ] tags)

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
nightrider
Offline
Joined: 2003-06-11

> There's a couple of things you can do to make stuff
> run faster. First off, I highly recommend you change
> the way you do your painting. I don't think it's safe
> to fetch the graphics once using getGraphics() and
> then use them throughout your application. Also,
> doing it this way bypasses Swing's double-buffering.

Just one question about that: wouldn't it improve smoothness of animation if you bypass double-buffering ?

I understand that double-buffering avoids flickers on screen, but it requires an additional copy of the image (maybe that's not a big issue with accelerated hardware).

shan-man
Offline
Joined: 2006-02-17

Hi Nightrider,

Nightrider wrote:
> Just one question about that: wouldn't it improve
> smoothness of animation if you bypass
> double-buffering ?
>
> I understand that double-buffering avoids flickers on
> screen, but it requires an additional copy of the
> image (maybe that's not a big issue with accelerated
> hardware).

Yes, not double-buffering gives your CPU less work to do, but would you really call flickery animation "smooth"? As mentioned in the tutorial article below, with graphical applications, [i]perceived performance[/i] is typically more important than [i]numerical performance[/i].

[url]http://java.sun.com/docs/books/tutorial/extra/fullscreen/doublebuf.html[/url]

Regards,
Shannon

shan-man
Offline
Joined: 2006-02-17

Hi gamezealot,

There's a couple of things you can do to make stuff run faster. First off, I highly recommend you change the way you do your painting. I don't think it's safe to fetch the graphics once using getGraphics() and then use them throughout your application. Also, doing it this way bypasses Swing's double-buffering.

The correct way to do things would be to override paintComponent() on the panels to paint the animation. Then, your timer would simply calculate the next step and call repaint() on the panel.

Also, to speed things up, you could try decreasing the value of timex. When I set it to 10, things were much smoother.

Lastly, I'd like to recommend that instead of using the timer directly to calculate your animation frames you use it simply to drive the repainting. Then, on each repaint you can calculate based on time elapsed what you should be painting. This way, on slow machines where the timer can't keep up, the progress of your animation won't suffer. It will be time-based so the block will move across the screen at the same rate.

I hope that helps!
Shannon

gamezealot
Offline
Joined: 2006-02-17

Hey, thanks a bunch. I gotta wait till tomorrow at school to make all those corrections. I'm having serious problems with Java on my Mac at home. So i can only do things with java at school. But thanks for all your help. Like I said, I've only been learning java for a little over a month now. But I learn it for 5 hours a day, I goto Lincoln Tech (trade school).