Mehrere Tasten gleichzeitig (Java 1.3)

Moin!
Die Bufferstrategie hat nichts mit der Funktion an sich zu tun. Entferne das einfach alles und hole die das Graphics Objekt direkt vom Frame, dann müsste es trotzdem klappen...

*grüssle*
MeinerEiner
 
Sorry, aber so einfach scheint das nicht zu sein

Code:
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;

/**
 * @author Darimont
 *
 */
public class MultiKeyExample extends JFrame {

	private final Dimension DIM = new Dimension(320, 240);

	private Graphics2D objGra;

	private boolean[] downKeys = new boolean[4];

	final static int UP = 0, DOWN = 1, LEFT = 2, RIGHT = 3;

	private int x = 50, y = 50;


	private Thread runner = new Thread() {
		{
			setPriority(MIN_PRIORITY);
		}

		public void run() {

			while (true) {
				Graphics2D g = objGra;
				g.clearRect(0, 0, DIM.width, DIM.height);
				if (downKeys[UP]) {
					y -= 3;
				}

				if (downKeys[DOWN]) {
					y += 3;
				}

				if (downKeys) {
					x -= 3;
				}

				if (downKeys[RIGHT]) {
					x += 3;
				}

				g.fillOval(x, y, 20, 20);

				g.dispose();
				objGra.show();

				try {
					sleep(100L);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}

			}
		}
	};

	public MultiKeyExample() {
		super("MultiButtonExample");
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setResizable(false);
		setSize(DIM);
		setIgnoreRepaint(true);

		addKeyListener(new KeyAdapter() {

			public void keyPressed(KeyEvent e) {
				delegateKeyCommand(e.getKeyCode(), true);
			}

			public void keyReleased(KeyEvent e) {
				delegateKeyCommand(e.getKeyCode(), false);
			}

			private void delegateKeyCommand(int code, boolean isDown) {
				switch (code) {

				case KeyEvent.VK_UP:
					downKeys[UP] = isDown;
					break;
				case KeyEvent.VK_DOWN:
					downKeys[DOWN] = isDown;
					break;
				case KeyEvent.VK_LEFT:
					downKeys = isDown;
					break;
				case KeyEvent.VK_RIGHT:
					downKeys[RIGHT] = isDown;
					break;
				default:
					;
				}
			}

		});

		setVisible(true);
		this.objGra = (Graphics2D)this.getGraphics();

	}

	public static void main(String[] args) {
		new MultiKeyExample().start();
	}

	/**
	 *
	 */
	private void start() {
		runner.start();
	}
}

Einmal findet er die Methode show() nicht:
Code:
g.dispose();
				objGra.show();

Und das hier auch nicht:
Code:
setIgnoreRepaint(true);
 
Ich hab hier kein 1.3, deswegen kann ichs nicht wirklich testen, aber bei mir funktioniert das auch alles ohne die Methoden, die bei dir bemängelt werden:
Code:
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;

/**
 * @author Darimont
 *
 */
public class Main extends JFrame {

    private final Dimension DIM = new Dimension(320, 240);

    private Graphics2D objGra;

    private boolean[] downKeys = new boolean[4];

    final static int UP = 0, DOWN = 1, LEFT = 2, RIGHT = 3;

    private int x = 50, y = 50;


    private Thread runner = new Thread() {
        {
            setPriority(MIN_PRIORITY);
        }

        public void run() {

            while (true) {
                //Graphics2D g = (Graphics2D) getGraphics();
                objGra.clearRect(0, 0, DIM.width, DIM.height);
                if (downKeys[UP]) {
                    y -= 3;
                }

                if (downKeys[DOWN]) {
                    y += 3;
                }

                if (downKeys) {
                    x -= 3;
                }

                if (downKeys[RIGHT]) {
                    x += 3;
                }

                objGra.fillOval(x, y, 20, 20);

                //objGra.dispose();
                
                try {
                    sleep(100L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }
    };

    public Main() {
        super("MultiButtonExample");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setResizable(false);
        setSize(DIM);
        
        addKeyListener(new KeyAdapter() {

            public void keyPressed(KeyEvent e) {
                delegateKeyCommand(e.getKeyCode(), true);
            }

            public void keyReleased(KeyEvent e) {
                delegateKeyCommand(e.getKeyCode(), false);
            }

            private void delegateKeyCommand(int code, boolean isDown) {
                switch (code) {

                case KeyEvent.VK_UP:
                    downKeys[UP] = isDown;
                    break;
                case KeyEvent.VK_DOWN:
                    downKeys[DOWN] = isDown;
                    break;
                case KeyEvent.VK_LEFT:
                    downKeys = isDown;
                    break;
                case KeyEvent.VK_RIGHT:
                    downKeys[RIGHT] = isDown;
                    break;
                default:
                    ;
                }
            }

        });

        setVisible(true);
        objGra = (Graphics2D)this.getGraphics();

    }

    public static void main(String[] args) {
        new Main().start();
    }

    /**
     *
     */
    private void start() {
        runner.start();
    }

}
 
Zurück