Moving an object

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);
    }
}


???

are you using swing or awt?

if you do use swing I sugjest reading up on the painting model at java.sun.com

in awt you create an image of the size of the bit you want to be dubble buffered. ( http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Component.html#createImage(int, int) )
get the graphics context of the image ( http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Image.html#getGraphics() ) draw on the returned context as you normaly would. then use g.drawImage() to paint the ‘buffer’-image.

init stuff (do once)


buffer = component.createImage( width, height );
gB = buffer.getGraphics();

change your paint:


gB.drawString("hello world",10,10);
// or something like
paintAbleObject.move(x,y);
paintAbleObject.draw(gB);
g.drawImage( buffer,0,0, observer);
// clear the buffer
gB.setColor(background);
gB.fillRect(0,0,width,heigth);

gB.dispose() wenn your done with it.

OR

You go into full-screen exclusive mode and do it the way real (non-applet) games do by flipping buffers. Mike martak has an excelletn set of tutorials here:

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

If my “Scroller” exampel is stil lfloating around it shows how you use it in a failry simple isometric game engine.

OR

you do what Cas does and do all your 2D as mono-planar 3D through JOGL or LWJGL.

ah thats where my non-gaming specialization, is appearend. fun reading material.

Thanks I’ll take a good look at the links ;D