# Update und repaint() im ActionListener



## remax (21. Okt 2007)

Hallo!

Ich bin gerade dabei das Spiel "Tron" nachzubauen. Es hat auch schon wunderbar funktioniert, allerdings wollte ich jetzt ein Menü einbauen. Wenn ich nun auf normal klicke, verschwindet das Menü und mein Spielfeld erscheint. Jedoch hört das Spiel dann auf. Normalerweise konnte ich dann mit den Pfeiltasten und W,A,S,D die Schlangen steuern. Könnt ihr mir helfen?


```
import java.awt.*;
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class draw extends Applet implements ActionListener, KeyListener {

String direction1 = "none";
String direction2 = "none";

int x1; int y1; int x2; int y2;
int punkte1 = 0;
int punkte2 = 0;

Image offscreen;
Graphics bufferGraphics;

Dimension dim;
int curX, curY;

Label gameover = new Label("set direction", Label.CENTER);
Label punkte = new Label("Score: Player1 " + punkte1 + " : " + punkte2 + " Player2");

int[][][] coordinates = new int[1000][1000][1];

Label titel = new Label("TRON", Label.CENTER);
JButton normal = new JButton("normal");
JButton bug = new JButton("mit Feature");

	public void init() {

			setFocusable(true);
			resize(605,600);
			setLayout(null);
			dim = getSize();
			offscreen = createImage( dim.width, dim.height );
			bufferGraphics = offscreen.getGraphics();


			addKeyListener(this);
			add(punkte);
			punkte.setVisible(false);
			add(gameover);
			gameover.setVisible(false);

			add(titel);
			titel.setBounds(220,10, 200, 20);
			add(normal);
			normal.setBounds(220,100, 200, 50);
			normal.addActionListener(this);
	}


public void actionPerformed (ActionEvent action) {

	if ( action.getSource().equals(normal) ) {

	normal.setVisible(false);
	titel.setVisible(false);
	punkte.setVisible(true);
	punkte.setBounds(220,0, 200, 20);

	Graphics g = getGraphics();

	for( int i=20; i<=572; i=i+8 ) {

		bufferGraphics.setColor(Color.gray);
		bufferGraphics.drawLine(20, i, 580, i);
	}

	for( int i=20; i<=580; i=i+8 ) {

		bufferGraphics.drawLine(i, 20, i, 572);
	}

		if ( direction1.equals("up") ) {

			coordinates[x1][y1][0] = 1;
			y1 = y1-8;
		}

		if ( direction1.equals("down") ) {

			coordinates[x1][y1][0] = 1;
			y1 = y1+8;
		}

		if ( direction1.equals("left") ) {

			coordinates[x1][y1][0] = 1;
			x1 = x1-8;
		}

		if ( direction1.equals("right") ) {

			coordinates[x1][y1][0] = 1;
			x1 = x1+8;
		}

			if ( direction2.equals("up") ) {

				coordinates[x2][y2][0] = 1;
				y2 = y2-8;
			}

			if ( direction2.equals("down") ) {

				coordinates[x2][y2][0] = 1;
				y2 = y2+8;
			}

			if ( direction2.equals("left") ) {

				coordinates[x2][y2][0] = 1;
				x2 = x2-8;
			}

			if ( direction2.equals("right") ) {

				coordinates[x2][y2][0] = 1;
				x2 = x2+8;
			}

		if ( direction1.equals("none") ) {

			x1 = 221;
			y1 = 293;
		}


		if ( direction2.equals("none") ) {

			x2 = 373;
			y2 = 293;
		}


	if ( x1 >= 21 && y1 >= 21 && x1 <= 573 && y1 <= 565 && x2 >= 21 && y2 >= 21 && x2 <= 573 && y2 <= 565 && coordinates[x1][y1][0] != 1 && coordinates[x2][y2][0] != 1 && ! ( x1 == x2 && y1 == y2 ) ) {

		try{

             Thread.sleep(40);

        } catch(InterruptedException e) {

             System.out.println("Sleep Interrupted");
        }

		if ( ! direction1.equals("none") && ! direction2.equals("none") ) {
		bufferGraphics.setColor(Color.red);
		bufferGraphics.fillRect(x1, y1, 7, 7);
		bufferGraphics.setColor(Color.green);
		bufferGraphics.fillRect(x2, y2, 7, 7);
		repaint();
		} else if ( direction1.equals("none") || direction2.equals("none") ) {

		x1 = 221;
		y1 = 293;
		x2 = 373;
		y2 = 293;

		bufferGraphics.setColor(Color.red);
		bufferGraphics.fillRect(x1, y1, 7, 7);
		bufferGraphics.setColor(Color.green);
		bufferGraphics.fillRect(x2, y2, 7, 7);
		repaint();

		} else {
		bufferGraphics.setColor(Color.red);
		bufferGraphics.fillRect(x1, y1, 7, 7);
		bufferGraphics.setColor(Color.green);
		bufferGraphics.fillRect(x2, y2, 7, 7);
		repaint();
		}

	} else {												// GAME OVER

		if ( (coordinates[x1][y1][0] == 1 && coordinates[x2][y2][0] == 1) || (x2 < 21 || y2 < 21 || x2 > 573 || y2 > 565) && (x1 < 21 || y1 < 21 || x1 > 573 || y1 > 565) || ( x1 == x2 && y1 == y2 ) ) {

			gameover.setText("Draw");

		} else if ( x2 < 21 || y2 < 21 || x2 > 573 || y2 > 565 ) {
			punkte1++;
			gameover.setText("Player 1 wins");
			punkte.setText("Score: Player1 " + punkte1 + " : " + punkte2 + " Player2");
		} else if ( x1 < 21 || y1 < 21 || x1 > 573 || y1 > 565 ) {
			punkte2++;
			gameover.setText("Player 2 wins");
			punkte.setText("Score: Player1 " + punkte1 + " : " + punkte2 + " Player2");
		} else if ( coordinates[x1][y1][0] == 1 ) {
			punkte2++;
			gameover.setText("Player 2 wins");
			punkte.setText("Score: Player1 " + punkte1 + " : " + punkte2 + " Player2");
		} else if ( coordinates[x2][y2][0] == 1 ) {
			punkte1++;
			gameover.setText("Player 1 wins");
			punkte.setText("Score: Player1 " + punkte1 + " : " + punkte2 + " Player2");
		}


		gameover.setVisible(true);
		gameover.setBounds(260,260, 100, 50);
		gameover.setFont(new Font("Comic Sans", 3, 16));
		x1 = 221;
		y1 = 293;
		x2 = 373;
		y2 = 293;

		direction1 = "none";
		direction2 = "none";

		try{

             Thread.sleep(1000);

        } catch(InterruptedException e) {

             System.out.println("Sleep Interrupted");
        }

		dim = getSize();
		offscreen = createImage( dim.width, dim.height );
		bufferGraphics = offscreen.getGraphics();

		for( int i=20; i<=572; i=i+8 ) {

			bufferGraphics.setColor(Color.gray);
			bufferGraphics.drawLine(20, i, 580, i);
		}

		for( int i=20; i<=580; i=i+8 ) {

			bufferGraphics.drawLine(i, 20, i, 572);
		}

		bufferGraphics.setColor(Color.red);
		bufferGraphics.fillRect(x1, y1, 7, 7);
		bufferGraphics.setColor(Color.green);
		bufferGraphics.fillRect(x2, y2, 7, 7);

		direction1 = "none";
		direction2 = "none";

		coordinates = new int[1000][1000][1];

		gameover.setVisible(false);

		repaint();

	}

	g.drawImage( offscreen, 0, 0, this );

}}

public void update(Graphics g) {

	paint(g);
}

 	 public void keyPressed(KeyEvent tastatur) {

    	if (tastatur.getKeyCode() == KeyEvent.VK_W) {

			if( ! direction1.equals("down") ) {
			direction1 = "up";
			}
			repaint();

		}

    	if (tastatur.getKeyCode() == KeyEvent.VK_A) {

			if( ! direction1.equals("right") ) {
			direction1 = "left";
			}
			repaint();
		}

    	if (tastatur.getKeyCode() == KeyEvent.VK_S) {

			if( ! direction1.equals("up") ) {
			direction1 = "down";
			}
			repaint();
		}

    	if (tastatur.getKeyCode() == KeyEvent.VK_D) {

			if( ! direction1.equals("left") ) {
			direction1 = "right";
			}
			repaint();
		}

			if (tastatur.getKeyCode() == KeyEvent.VK_UP) {

				if( ! direction2.equals("down") ) {
				direction2 = "up";
				}
				repaint();
			}

			if (tastatur.getKeyCode() == KeyEvent.VK_LEFT) {

				if( ! direction2.equals("right") ) {
				direction2 = "left";
				}
				repaint();
			}

			if (tastatur.getKeyCode() == KeyEvent.VK_DOWN) {

				if( ! direction2.equals("up") ) {
				direction2 = "down";
				}
				repaint();
			}

			if (tastatur.getKeyCode() == KeyEvent.VK_RIGHT) {

				if( ! direction2.equals("left") ) {
				direction2 = "right";
				}
				repaint();
			}
 	 }

	public void keyReleased ( KeyEvent tastatur ) { }
	public void keyTyped ( KeyEvent tastatur ) { }

}
```


----------



## Marco13 (21. Okt 2007)

Man sollte Swing und AWT nicht mischen. Man sollte NIE auf einer Component "getGraphics" aufrufen. Man sollte Debug-Ausgaben einbauen, wenn man einen Fehler sucht. Man sollte nicht Abfragen stricken wie if ( x1 >= 21 && y1 >= 21 && x1 <= 573 && y1 <= 565 && x2 >= 21 && y2 >= 21 && x2 <= 573 && y2 <= 565 && coordinates[x1][y1][0] != 1 && coordinates[x2][y2][0] != 1 && ! ( x1 == x2 && y1 == y2 ) ) {  :autsch:  Man sollte Variablen wie "direction1" nicht durch einen String repräsentierten. (Hat zwar nichts mit der eigentlichen Frage zu tun, sollte man aber mal erwähnen)


----------



## remax (21. Okt 2007)

Ja ok manches sagt mir jetzt zwar nischt, aber trotzdem danke.

Auf jeden fall habe ich mein Problem lösen können, ohne auch nur eines deiner genannten Dinge davon zu ändern. 

// Closed


----------



## Marco13 (21. Okt 2007)

Jo, man kann auch mit einer Zange eine Schraube in die Wand nageln. Aber was liegt mir dran  :roll:


----------

