Hi !
Featured games (84)
games approved by the League of Dukes
Games in Showcase (549)
Games in Android Showcase (137)
games submitted by our members
Games in WIP (594)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Best way to update screen?  (Read 1017 times)
0 Members and 1 Guest are viewing this topic.
Offline JustMe

Junior Newbie

« Posted 2005-12-05 21:33:05 »


I am working my way through de basics of Java with the intention to go on creating games with it.
As far as I know there are 3 ways to update your applet:
1) no repaint, just paint the changes
2) repaint();
3) double buffering;

The last is set to be the best.

Well, I've written a little experiment. I create a little rectangle and tell it to bounce around in my applet window. To update the applet I use double buffering. (At least I am convinced I do )
The source is included at the end of this post.

When I run the code I don't see a smooth movement of the rectangle. Instead it's a kind of blury with a motion trail.

Am I doing something wrong? Or are there better approaches to update the 'screen'?

Thanks for any advice,

 * Created on 5 december 2005, 20:15
 * First try: Just moving a rectangle across the screen
import java.applet.*;
import java.awt.*;
public class Main extends Applet implements Runnable {
    // vars for double buffering
    private Image dbImage;
    private Graphics dbg;
    //rectangle vars
    int x_pos = 10;
    int y_pos = 100;
    int x_width = 8;
    int y_height = 20;
    int x_speed = 4; //pixels per move
    int y_speed = 4; //pixels per move
    public void init() {
        //resize the applet
        this.setSize( 640, 480 );
    public void start() {
        // define a new thread
        Thread th = new Thread(this);
        // start this thread
    public void stop() { }
    public void destroy() { }
    public void run() {
        // lower ThreadPriority
        // run a long while (true) this means in our case "always"
        while (true) {
            x_pos = x_pos + x_speed;
            if( (x_pos + x_width) >= this.getSize().width || x_pos <= 0 )
                x_speed = - x_speed;
            y_pos = y_pos + y_speed;
            if( ( y_pos + y_height ) >= this.getSize().height || y_pos <= 0 )
                y_speed = - y_speed;
            // repaint the applet
            try {
                // Stop thread for 20 milliseconds
                Thread.sleep( 20 );
            } catch (InterruptedException ex) {
                // do nothing
            // set ThreadPriority to maximum value
    /* Update - Double buffering */
    public void update(Graphics g) {
        // initialize buffer
        if (dbImage == null) {
            dbImage = createImage(this.getSize().width, this.getSize().height);
            dbg = dbImage.getGraphics();
        // clear screen in background
        dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);
        // draw elements in background
        // draw image on the screen
        g.drawImage(dbImage, 0, 0, this);
   public void paint(Graphics g) {
        //Put things on the screen...
        g.setColor( );
        g.fillRect( x_pos,y_pos, x_width, y_height );

Offline JustMe

Junior Newbie

« Reply #1 - Posted 2005-12-05 22:35:29 »

A previous post introduced me to the space invader tutorials. Looks like they can help me.

Offline Jeff

JGO Coder

Got any cats?

« Reply #2 - Posted 2005-12-05 22:40:53 »

Repaint is asynchronous so its doubtful you wil lever get anytying smooth out of it.

Try using active rendering:

Keep in mind though that even then you will need to throttle your paint speed if yo uwan a smooth frame rate.  At minimum you must control your mvoement of the object by elapsed time or you will nevr get naythign even approaching smoothness.

I have on list for the "gems" to do a fundemental game  loo pas this comes up a lot.

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline JustMe

Junior Newbie

« Reply #3 - Posted 2005-12-06 15:44:14 »

Thanks. I'll try active rendering.

I have on list for the "gems" to do a fundemental game  loo pas this comes up a lot.

What list do you mean? Your FAQ at the bottom of your post? And what are gems?
Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.


Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CopyableCougar4 (14 views)
2015-01-26 04:47:56

Olo (14 views)
2015-01-25 21:26:00

Olo (16 views)
2015-01-25 18:44:22

Robo11 (28 views)
2015-01-25 06:14:26

basil_ (26 views)
2015-01-17 22:29:32

wxwsk8er (26 views)
2015-01-16 21:42:21

wxwsk8er (21 views)
2015-01-16 20:44:20

basil_ (25 views)
2015-01-14 09:46:59

basil_ (21 views)
2015-01-14 09:46:52

wxwsk8er (35 views)
2015-01-13 20:42:16
2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!