Skip to main content
GameDev.net gamedev.net
🔒 Locked

Applet Programming - Repaint?

Started by 31337 Nov 22, 2003 at 7:16 PM 2 replies 350+ views
Original Post
31337
31337
Hello, I am trying to get my application to call the paint function in my applet over and over again so that my applet gets consistantly updated, however it isn''t working out that way. What it does is the browser only calls repaint when its started and when I uncover it from being hidden and stuff. I have tried several approaches to resolve this problem yet to no avail. I have tried using timers, making my paint function recursive, and I''ve tried to just call repaint at the end of my painting function yet that doesn''t seem to work either. I am also confused as to what I want to use, swing or awt (swing is essentially awt right?). I am trying to write a java pong game in the form of an applet. Thanks for your help.
OpenGL Revolutions http://students.hightechhigh.org/~jjensen/
brassfish89
brassfish89
A timer is what you want. I''m not really a java guy so I can''t help you, but there''s a java game programming tutorial in the articles section of this site.
andrew71
andrew71
For an applet you will need to create a thread (for an application you use the main thread) that just calls repaint() in a loop. This requests that the browser redraws the screen. Something like:
import java.applet.Applet;
import java.awt.*;

/*
<applet code=Animator height=300 width=300>
*/

public class Animator extends Applet implements Runnable
{
private Thread thread;
private int x = 0;

public void init() {}

public void start()
{
thread = new Thread(this);
thread.start();
}

public void stop()
{
thread = null;
}

public void run()
{
Thread thisThread = Thread.currentThread();
while (thread == thisThread)
{
try
{
Thread.sleep(1000 / 20); // pause giving maximum 20 frames per second
}
catch (InterruptedException e)
{
// ignore exceptions
}

x++; // change position where we will draw rectangle
repaint(); // request screen redraw
}
}

public void update(Graphics g)
{
paint(g);
}

public void paint(Graphics g)
{
g.setColor(Color.WHITE);
g.fillRect(0, 0, getSize().width, getSize().height); // clear background;

g.setColor(Color.RED);
g.fillRect(x, 100, 50, 50); // draw a rectangle
}
}

Try the Sun java tutorials. eg applet tutorial:
http://java.sun.com/docs/books/tutorial/applet/index.html

An alternative to using repaint() is active rendering:
http://java.sun.com/docs/books/tutorial/extra/fullscreen/rendering.html

You may want to try gamedev''s java forum (it''s hidden down the bottom) or http://www.javagaming.org/cgi-bin/JGNetForums/YaBB.cgi
31337
31337
Ok thanks a lot for your help. I found the answer to my problem though, I wasn''t double buffering (I guess I assumed java did that automatically) and I wasn''t using threading. But now I am, much like that code snippet you showed. Thanks guys.
OpenGL Revolutions http://students.hightechhigh.org/~jjensen/

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.