ERLEDIGT
NEIN
NEIN
ANTWORTEN
3
3
ZUGRIFFE
553
553
EMPFEHLEN
-
27.01.10 16:39 #1PapaNoah Tutorials.de Gastzugang
Hallo miteinander,
Ich arbeite gerade an einem "Schiffe versenken" für die Abschlussarbeit in der Berufsschule. Ich hänge gerade an einem ziemlich nervigen Problem fest, und zwar zeichne ich zwei praktisch identische Spielfelder in ein JFrame (Canvas => Paint() überschrieben). Sie unterscheiden sich jediglich von der Position im JFrame sowie dem Spielernamen, welcher auf der rechten Seite des Spielfelds angezeigt werden sollte. Leider funktioniert das aber irgendwie nicht, das heisst nur so halb. Wenn ich nur 1 Spielfeld zeichne klappts, zeichne ich zwei Spielfelder wird nur bei einem der Name angezeigt. Ich hoffe ihr könnt mir helfen, ich stehe leider etwas unter Zeitdruck, da ich für heute frei genommen habe um daran arbeiten zu können
Hier der betreffende Code:
Wenn eine Instanz von der Klasse "Player" erstellt wird, wird automatisch eine Instanz der Klasse "Playground"(in der die Paint()-Methode überschrieben wird) erstellt mit den mitgegebenen Parametern x, y, und dem Spielernamen.
Code java:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
import javax.swing.*; public class Gameplay { private int coordinateX; private int coordinateY; private String rawCoordinates; private Player currentPlayer; private Playground currentPlayground; public Gameplay() { initializeGame(); Player testPlayer = new Player(20, 20, "Tester1"); Player testPlayer2 = new Player(20, 313, "Tester2"); showGui(testPlayer, testPlayer2); testPlayer.getPlayground().repaint(); testPlayer2.getPlayground().repaint(); while(true) { rawCoordinates = Actions.getCoordinates(); coordinateX = Actions.getCoordinateX(rawCoordinates); coordinateY = Actions.getCoordinateY(rawCoordinates); Actions.attack(testPlayer.getPlayground(), coordinateX, coordinateY); testPlayer.getPlayground().positioningModeActivated(false); testPlayer.getPlayground().repaint(); } } public void showGui(Player playerA, Player playerB) { //JFrame wird erstellt JFrame mainFrame = new JFrame("Java - Schiffe versenken"); //Spielfelder(Canvas) werden dem Frame zugewiesen mainFrame.add(playerA.getPlayground()); mainFrame.add(playerB.getPlayground()); //Fenstereigenschaften werden angepasst mainFrame.setSize(348,615); mainFrame.setResizable(false); mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); mainFrame.setVisible(true); } public void initializeGame() { coordinateX = 0; coordinateY = 0; rawCoordinates = ""; } public void showMenu() { System.out.println("============================="); System.out.println("======SCHIFFE=VERSENKEN======"); System.out.println("============================="); System.out.println("MENU:"); System.out.println("1 EINZELSPIELER(GEGEN COMPUTER)"); System.out.println("2 2-SPIELER"); System.out.println("3 SPIEL BEENDEN"); } }
Der Name wird in dieser Klasse bei Zeile 44 gezeichnet :
Code java:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
import java.awt.*; public class Playground extends Canvas { private static int PLAYGROUND_SIZE = 12; private static int FIELD_SIZE = 20; private static int GAP = 2; private Field playground[][] = new Field[PLAYGROUND_SIZE][PLAYGROUND_SIZE]; private Boolean positioningMode; private int fieldPositionX; private int fieldPositionY; private String playerName; public Playground(int positionX, int positionY, String playerName) { int canvasSize = (PLAYGROUND_SIZE * FIELD_SIZE) + ((PLAYGROUND_SIZE - 1)*GAP) + 21; setSize(canvasSize, canvasSize); initializePlayground(); positioningMode = true; fieldPositionX = positionX; fieldPositionY = positionY; this.playerName = playerName; } public void paint(Graphics g) { super.paint(g); int x = fieldPositionX; int y = fieldPositionY; int fontPositionX = fieldPositionX; char yCoordinateSign = 'A'; g.setFont(new Font("Consolas", Font.PLAIN, 10)); for(int i = 0 ; i < PLAYGROUND_SIZE ; i++) { g.drawString(String.valueOf(yCoordinateSign), fontPositionX + 7, fieldPositionY - 5); g.setColor(Color.BLACK); fontPositionX += 22; yCoordinateSign++; } g.drawString(playerName, 294, y); System.out.println(playerName + " drawed at" + 294 + "/" + y); for(int i = 0 ; i < PLAYGROUND_SIZE ; i++) { g.setColor(Color.BLACK); g.drawString(String.valueOf(i + 1), (x - 15), (y + 13)); for(int j = 0 ; j < PLAYGROUND_SIZE ; j++) { if(positioningMode) { if(playground[i][j].containsBoat()) { g.setColor(Color.GREEN); } else { g.setColor(Color.GRAY); } } else { if(playground[i][j].isHit()) { if(playground[i][j].containsBoat()) { g.setColor(Color.RED); } else { g.setColor(Color.BLUE); } } else { g.setColor(Color.GRAY); } } g.fillRect(x, y, FIELD_SIZE, FIELD_SIZE); x += FIELD_SIZE + GAP; } x = fieldPositionX; y += FIELD_SIZE + GAP; } } public void paintNew() { repaint(); } private void initializePlayground() { for(int i = 0 ; i < 12 ; i++) { for(int j = 0 ; j < 12 ; j++) { playground[i][j] = new Field(); } } } public Field getField(int x, int y) { return playground[y][x]; } public void setField(Boat boat, int x, int y) { playground[y][x].setBoat(boat); } public void positioningModeActivated(Boolean activated) { positioningMode = activated; } public static int getPlaygroundSize() { return PLAYGROUND_SIZE; } }
Auf die Formatierung sowie auf Kommentare wurde bisher noch nicht geachtet, ich möchte zuerst dieses Problem lösen.
Danke im Voraus für die Hilfe!
PapaNoah
-
27.01.10 16:42 #2PapaNoah Tutorials.de Gastzugang
EDIT: Sorry der Titel müsste heissen CANVAS nicht JPanel, ich habs bereits über ein JPanel versucht (paintComponent), jedoch das gleiche Ergebnis bekommen: Der Name wird nur bei einem Spielfeld gezeichnet...
mfg PapaNoah
-
27.01.10 17:03 #3PapaNoah Tutorials.de Gastzugang
Hat sich erledigt!
Ich hatte vergessen ein Layout für das JFrame zu bestimmen:
Code java:1
mainFrame.setLayout(new GridLayout(2,1));
mfg PapaNoah
-
Hey,
Markiere den Thread doch bitte als erledigt...
Freundliche Grüsse
CKingZesiEine Frage braucht keinen Anlass, die Frage ist der Anlass selbst...
Ähnliche Themen
-
JPanel wird nicht neu gezeichnet
Von kuhlmaehn im Forum Swing, Java2D/3D, SWT, JFaceAntworten: 0Letzter Beitrag: 24.08.10, 17:41 -
Diagramm aus XML Datei wird nicht gezeichnet
Von filigrani im Forum Adobe Flex & AIRAntworten: 0Letzter Beitrag: 28.01.09, 12:06 -
JFrame wird nicht vollständig gezeichnet
Von karl_soost im Forum Swing, Java2D/3D, SWT, JFaceAntworten: 1Letzter Beitrag: 04.03.08, 20:25 -
JScrollPane wird nicht neu gezeichnet
Von donstefano im Forum Swing, Java2D/3D, SWT, JFaceAntworten: 3Letzter Beitrag: 21.07.06, 10:39 -
Wieso wird nicht alles gezeichnet
Von MrDude im Forum JavaAntworten: 1Letzter Beitrag: 16.07.06, 01:03





Zitieren
Login





