Multiplayer Shooter- Wie geht das mit Canvas & Graphics

antonbracke

Mitglied
Hallo,

ich schreibe seit kurzem an einem multiplayer shooter, bei dem ich anfangs Weise einfach nur über ein 20 * 20 Feld laufen kann! Alles soll multiplayer gehen mit einem einfachen Echo-Server (das hat mein Lehrer vorgegeben)! Ich habe jeetzt aber das große Problem, dass ich das mit dem Canvas und den Graphics nicht ordentlich hinbekomme (beim auführen kommt eine null pointer execption)!

Vielleicht kann mir ja jemand helfen!
Würde mich auch über andere Tipps freuen, da ich in Java noch keinen wirklich eleganten Stil habe.

JAVA-CODE (GUI-CLASS):
Java:
...
Game game = new Game(gui);
JPanel pgame = new JPanel();
pgame.add(game);
game.addKeyListener(this);
...

JAVA-CODE (GAME-CLASS):
Java:
package Game;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;

import Main.GUI;

public class Game extends Canvas {
	private static final long serialVersionUID = 1L;
	static GUI gui = null;
	static int[][] ls_game = new int[20][20];
	static int[] ls_myposi = new int[2];
	static Graphics gr;

	public Game(GUI _gui) {
		if (_gui != null) {
			gui = _gui;
			gr = this.getGraphics();
			setBounds(10, 10, 500, 500);
			setBackground(Color.LIGHT_GRAY);	
			setGameField();
		}
	}

	public void paint(Graphics _g) {
		gr = _g;
		gr.setColor(Color.GREEN);
		gr.fillRect(10, 10, 20, 20);
		this.repaint();
	}

	public void setGameField() {
		init();
		Random rnd = new Random();
		int x = rnd.nextInt(20);
		int y = rnd.nextInt(20);
		ls_myposi[0] = x;
		ls_myposi[1] = y;
		updateGUI(3, x, y);
	}

	public void init() {
		for (int i = 0; i < ls_game.length; i++) {
			for (int j = 0; j < ls_game.length; j++) {
				ls_game[i][j] = 0;
			}
		}
		for (int i = 0; i < ls_myposi.length; i++) {
			ls_myposi[i] = 0;
		}
	}

	public void updateGUI(int type, int x, int y) {
		System.out.println(type + " " + x + " " + y);
		switch (type) {
		case 0:
			gr.setColor(Color.magenta);
			gr.fillRect(x, y, 20, 20);
			break;
		case 1:
			gr.setColor(Color.BLUE);
			gr.fillRect(x, y, 20, 20);
			break;
		case 2:
			gr.setColor(Color.CYAN);
			gr.fillRect(x, y, 20, 20);
			break;
		case 3:
			gr.setColor(Color.GREEN);
			gr.fillRect(x, y, 20, 20);
			break;
		}
	}

	public void setnewPosi(int type, int x, int y) {
		ls_game[x][y] = type;
		updateGUI(type, x, y);
	}

	public void go(int go) {
		int x = ls_myposi[0];
		int y = ls_myposi[1];

		switch (go) {
		case 0: // up
			y++;
			break;
		case 1: // down
			y--;
			break;
		case 2: // left
			x--;
			break;
		case 3: // right
			x--;
			break;
		}
		System.out.println(cango(x, y));
		if (cango(x, y)) {
			ls_myposi[0] = x;
			ls_myposi[1] = y;
			setnewPosi(30, x, y);
		}
	}

	public boolean cango(int x, int y) {
		if (x < 0) {
			return false;
		}
		if (y < 0) {
			return false;
		}
		if (x > 20) {
			return false;
		}
		if (y > 20) {
			return false;
		}
		return true;
	}

	// types::::
	// 0:leeres Feld
	// 1:blockade
	// 2:
	// 30:ich
	// 31:Player 1
	// 32:Player 2
	// 33:Player 3
}


Danke schon mal im Voraus

Anton
 
Der Fehler aus der GAME-CLASS:
Java:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at Game.Game.updateGUI(Game.java:71)
	at Game.Game.setGameField(Game.java:41)
	at Game.Game.<init>(Game.java:23)
	at Main.GUI.start(GUI.java:98)
	at Main.GUI.access$1(GUI.java:95)
	at Main.GUI$2.actionPerformed(GUI.java:72)
	at java.awt.Button.processActionEvent(Unknown Source)
	at java.awt.Button.processEvent(Unknown Source)
	at java.awt.Component.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
	at java.awt.EventQueue.access$000(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue$4.run(Unknown Source)
	at java.awt.EventQueue$4.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)
 
Wie du siehst steht da:
Code:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Game.Game.updateGUI(Game.java:71)
Das heisst in Zeile 71 fliegt er raus. Du hältst in deiner Klasse ein Graphics Objekt, das du die u.a. sogar mit getGraphics() beschaffst. Das ist ein verbreiteter Anfängerfehler, siehe Zeichnen in Swing Tutorial. Ausserdem darf dein Graphics Objekt nur innerhalb der paint Methode angesprochen/verändert werden. Dein Fehler fliegt, weil der Prozess, der die paint Methode aufruft das Graphics Objekt zerstört nachdem die paint fertig ist - wenn du dann was dran ändern willst ist nix mehr da.
Bzgl. Stile ist mir noch aufgefallen dass du repaint in der paint aufrufst. Das ist nicht schön. Setzt dich lieber mal mit Threads auseinander. Ich bin sicher hier kursieren einige Tutorials für Spiele von denen du dir was abschauen kannst, siehe Forum Spiele- und Multimedia-Programmierung
 
Hallo bERt0r,

wie genau sollte ich das dann dort ändern.
Ich versteh das mit dem getGraphics(); und dem Graphics Objekt noch nicht so ganz!

Anton
 

Zurück
Oben