Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (532)
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 909 times)
0 Members and 1 Guest are viewing this topic.
Offline JustMe

Junior Newbie





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

Hi,

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,
JustMe


/*
 * Main.java
 *
 * 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
        th.start();
    }
   
    public void stop() { }
   
    public void destroy() { }
   
    public void run() {
        this.setBackground(Color.blue);
       
        // lower ThreadPriority
        Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
       
        // 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
            repaint();
           
            try {
                // Stop thread for 20 milliseconds
                Thread.sleep( 20 );
               
            } catch (InterruptedException ex) {
                // do nothing
               
            }
           
            // set ThreadPriority to maximum value
            Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
        }
    }
   
    /* 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.setColor(getBackground());
        dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);
       
        // draw elements in background
        dbg.setColor(getForeground());
        paint(dbg);
       
        // draw image on the screen
        g.drawImage(dbImage, 0, 0, this);
       
    }
 
   public void paint(Graphics g) {
        //Put things on the screen...
        g.setColor( Color.red );
        g.fillRect( x_pos,y_pos, x_width, y_height );
    }   
   
}

Offline JustMe

Junior Newbie





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

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

JustMe
Offline Jeff

JGO Coder




Got any cats?


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

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

Try using active rendering:

http://java.sun.com/docs/books/tutorial/extra/fullscreen/rendering.html

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!

http://wiki.java.net/bin/view/Games/JeffFAQ
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 16:44:14 »

Thanks. I'll try active rendering.

Quote
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.

pw (17 views)
2014-07-24 01:59:36

Riven (17 views)
2014-07-23 21:16:32

Riven (14 views)
2014-07-23 21:07:15

Riven (17 views)
2014-07-23 20:56:16

ctomni231 (45 views)
2014-07-18 06:55:21

Zero Volt (40 views)
2014-07-17 23:47:54

danieldean (32 views)
2014-07-17 23:41:23

MustardPeter (36 views)
2014-07-16 23:30:00

Cero (51 views)
2014-07-16 00:42:17

Riven (50 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org 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‑gaming.org
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!