Am I doing it right? and double buffering

Hi guys, I’m pretty new to programming, but I really want to make a game. I know this is a really easy question, but it is late, and I don’t want to spend the next couple of hours doing something wrong. I want to move an object around with the keyboard, is this the best way of doing it? And how do I implements double buffering to avoid flicker?



/**
 * Simple test of moving an objekt
 * 
 * @author Lau 
 * @version 0.1
 */

import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import javax.swing.*;

public class AnimationTest extends BasicJFrame implements Runnable
{
    private Ellipse2D.Double cirkel;
    private double xpos = 250;
    private double ypos = 250;
    private double rad = 25;
    private double xspeed;
    private double yspeed;
    private Thread animationThread;

    
    public AnimationTest()
    {
        super("Testarea", 500, 500);
        getContentPane().setBackground(Color.WHITE);
        
        addKeyListener(listener);
        
        xspeed = 0;
        yspeed = 0;
        
        setVisible (true);
        
        animationThread = new Thread(this);
        animationThread.start();
        
    }
    

    
    public void run()
    {
        while(Thread.currentThread() == animationThread)
        {
            xpos = xpos + xspeed;
            ypos = ypos + yspeed;
            
            repaint();
            try
            {
                Thread.sleep(10);   
            }
            catch(InterruptedException u)
            {
                System.err.println(u.toString());
            }
        }
    }   
    
    KeyListener listener = new KeyListener()
    {
        public void keyPressed(KeyEvent e)
        {
            int code = e.getKeyCode(); 
            
            switch(code)
            {
                case KeyEvent.VK_UP: yspeed = -3; break; 
                case KeyEvent.VK_LEFT: xspeed = -3; break; 
                case KeyEvent.VK_RIGHT: xspeed = +3; break; 
                case KeyEvent.VK_DOWN: yspeed = +3; break; 
            }
            
        };
        
        public void keyTyped(KeyEvent e)
        {};
     
        public void keyReleased(KeyEvent e)
        {
            int code = e.getKeyCode(); 
            
            switch(code)
            {
                case KeyEvent.VK_UP: yspeed = 0; break; 
                case KeyEvent.VK_LEFT: xspeed = 0; break; 
                case KeyEvent.VK_RIGHT: xspeed = 0; break; 
                case KeyEvent.VK_DOWN: yspeed = 0; break; 
            }
 
        };
    };
    
    public void paint(Graphics g)
    {
        super.paint(g);
        
        Graphics2D g2 = (Graphics2D) g;
        g2.setColor(Color.RED);
        
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                       RenderingHints.VALUE_ANTIALIAS_ON);
        
        cirkel = new Ellipse2D.Double(xpos - rad, ypos - rad, 2*rad, 2*rad);
            g2.fill(cirkel);
    }
}


???

Sorry I’ll post this in Newless Clubies

Its probably fine here,

If you replace your repaint() with a repaint(0) things would work better. repaint() just schedules a repaint of the screen but that happens on a different thread at some point in the future. repaint(0) requests a repaint() within 0 milliseconds.

While this will probably work you’d be better to be using buffer strategies. At the risk of tooting my own horn, this tutorial might help:

http://www.cokeandcode.com/info/tut2d.html

Kev

Thanks, this link will answer a lot of questions ;D