Source code: schiffeversenken/GUI.java
1 package schiffeversenken;
2
3 /*
4 JavaBattle
5 ==========
6
7 published under GNU General Public License (GPL)
8
9 schoetz@users.sourceforge.net
10
11 */
12
13 /*
14 GUI
15 ===
16 Hauptfenster. Es erstellt ein neues Spiel indem es ein neues Objekt der Klasse Spiel anlegt.
17 Es führt Befehle des menschlichen Spielers aus, die es über das Callback Interface empfängt.
18 */
19
20 import java.awt.*;
21 import java.awt.event.*;
22 import javax.swing.*;
23
24 public class GUI extends JFrame implements ActionListener, MouseListener, Callback, KeyListener
25 {
26 public final static int xStart = 60;
27 public final static int yStart = 50;
28 JMenuBar mnuBar;
29 JMenu mnuDatei, mnuHilfe;
30 JMenuItem mnitNeuesSpiel, mnitBeenden, mnitAbout, mnitHilfe;
31
32 JTextField txtChat;
33 JButton btnSubmit;
34 JLabel sunken;
35 JLabel lblCommand;
36
37 PrintField printField;
38
39 Spiel spiel;
40 schiffeversenken.spieler.Spieler spieler;
41
42 private int length = 15;
43 private int[] shipArray = new int[4];
44 private C_Punkte qKoordinaten[][];
45 private int xGitter=50, yGitter=100, sQuadrat=20;
46 private int mouseX, mouseY;
47 private int todo=-1;
48 private int cursorAusrichtung;
49 private int[] shipArray2 = new int[4];
50 private int aktuellesSchiff;
51
52 public GUI()
53 {
54 super("Schiffe Versenken");
55 this.setLocation(100,100);
56 this.setResizable(false);
57 countShips();
58 cursorAusrichtung = 0;
59 this.addKeyListener(this);
60 shipArray2[0] = 0;
61 shipArray2[1] = 0;
62 shipArray2[2] = 0;
63 shipArray2[3] = 0;
64 printField = new PrintField(this);
65 this.getContentPane().add(printField);
66
67
68
69 // Menuleiste
70
71 txtChat = new JTextField();
72 // this.getContentPane().add(txtChat);
73 txtChat.addKeyListener(this);
74
75 btnSubmit = new JButton("Senden");
76 // this.getContentPane().add(btnSubmit);
77
78 lblCommand = new JLabel("Klicken Sie auf Datei Neues Spiel um ein Spiel zu starten");
79 lblCommand.setSize(400,18);
80 lblCommand.setForeground(Color.white);
81 setAllSizes();
82 printField.add(lblCommand);
83 //this.getContentPane().add(lblCommand);
84
85 mnuBar = new JMenuBar();
86 mnuDatei = new JMenu("Datei");
87 mnuHilfe = new JMenu("Hilfe");
88
89 mnitNeuesSpiel = new JMenuItem("Neues Spiel");
90 mnitBeenden = new JMenuItem("Beenden");
91 mnitAbout = new JMenuItem("Info...");
92 mnitHilfe = new JMenuItem("Regeln...");
93
94 mnitNeuesSpiel.addActionListener(this);
95 mnitBeenden.addActionListener(this);
96 mnitAbout.addActionListener(this);
97
98 mnitHilfe.addActionListener(this);
99 mnuDatei.add(mnitNeuesSpiel);
100 mnuDatei.add(mnitBeenden);
101 mnuHilfe.add(mnitAbout);
102 mnuHilfe.add(mnitHilfe);
103 mnuBar.add(mnuDatei);
104 mnuBar.add(mnuHilfe);
105
106 this.setJMenuBar(mnuBar);
107 this.setVisible(true);
108
109 this.addWindowListener(new WindowAdapter()
110 {
111 public void windowClosing(WindowEvent we)
112 {
113 System.exit(0);
114 }
115 });
116 } //end Konstruktor
117 public void initialisiere(int groesse, Callback c) {
118 spiel = new Spiel(groesse,c);
119 }
120 public void initialisiere(int groesse, int port, Callback c) {
121 // spiel = new Spiel(groesse,port,c);
122 }
123 public void initialisiere(int groesse, int port, String ip, Callback c) {
124 // spiel = new Spiel(groesse,port,ip,c);
125 }
126
127
128 public void actionPerformed(ActionEvent ae)
129 {
130 Object object = ae.getSource();
131 if(object.equals(mnitNeuesSpiel))
132 {NeuesSpiel n = new NeuesSpiel(this);}
133 if(object.equals(mnitAbout))
134 {About n = new About(this);}
135 if(object.equals(mnitHilfe))
136 {Help n = new Help(this);}
137 if(object.equals(mnitBeenden))
138 {System.exit(0);}
139 }
140
141 // Fenstergrösse anhand von length berechnen
142 public void setAllSizes(){
143 int a;
144 int b;
145 int x;
146 int y;
147 int q;
148 int z;
149
150 //Fenstergrösse setzen
151 a= 320+(2*this.length*20);
152 b= 250+(this.length*20);
153 this.setSize(a,b);
154
155 /* //Comandozeile setzen
156 x= 2*this.length*20;
157 y= 210+(this.length*20);
158 txtChat.setSize(x,20);
159 txtChat.setLocation(50,y);*/
160
161 lblCommand.setLocation(55,(length*20+yStart+97));
162
163 /* //Chatbutton setzen
164 q= 50+(2*this.length*20);
165 z= 210+(this.length*20);
166 btnSubmit.setSize(50,20);
167 btnSubmit.setLocation(q,z);*/
168 } //end setAllSizes
169
170 public void reset(int length) {
171 shipArray = new int[4];
172 shipArray2 = new int[4];
173 setLength(length);
174 countShips();
175 setAllSizes();
176 printField.setCursor(PrintField.CURSOR_NORMAL);
177 }
178
179 public void countShips()
180 {
181 int ship2=2, ship3=1, ship4=1, ship8=0;
182 for(int i =1;i<=length-5;i++){
183 if(i%2==0)
184 {
185 if(ship2==2){ship2=1;}
186 else{ship2=2;}
187 ship3=1;
188 ship4++;
189 }
190 else{ship3=2;}
191 if((i-2)%4==0)
192 {
193 ship8++;
194 }
195 }
196 shipArray[0] = ship2;
197 shipArray[1] = ship3;
198 shipArray[2] = ship4;
199 shipArray[3] = ship8;
200 } //end countShips
201
202 public boolean setLength(int l){
203 if((l>=5)&&(l<=15)){
204 length = l;
205 return true;}
206 else return false;
207 }
208
209 public boolean command(int x,schiffeversenken.spieler.Spieler o){
210 // System.out.println("Command: " + x);
211 spieler = o;
212 switch(x){
213 case Callback.SCHIESSE:
214 todo=Callback.SCHIESSE;
215 printField.setCursor(PrintField.CURSOR_SHOOT);
216 printField.repaint();
217 setCommandText("Bitte schiessen Sie");
218 break;
219 case Callback.CONNECTION_UNTERBROCHEN:
220 todo=Callback.CONNECTION_UNTERBROCHEN;
221 break;
222 case Callback.SCHIFFE_SETZEN:todo=Callback.SCHIFFE_SETZEN;
223 setCommandText("Bitte setzen Sei ihre Schiffe");
224 if(shipArray2 == null)
225 System.out.println("shipArray2 = null");
226 if(spiel == null)
227 System.out.println("spiel = null");
228 shipArray2 = spiel.getShipArray();
229 printField.repaint();
230 break;
231 case Callback.SPIEL_BEENDET_GEWONNEN:
232 setCommandText("Sie haben gewonnen! Klicken Sie auf Start um ein neues Spiel zu starten");
233 break;
234 case Callback.SPIEL_BEENDET_VERLOREN:
235 setCommandText("Sie haben leider verloren! Klicken Sie auf Start um ein neues Spiel zu starten");
236 break;
237 }
238 return true;
239 }
240
241 public void mouseClicked(MouseEvent e){
242 if(todo==Callback.SCHIESSE) {
243 int x = e.getX();
244 int y = e.getY();
245 x -= xStart + 50 + 20 * length;
246 y -= yStart;
247 if(x>=0 && x<length*20 && y>=0 && y<length*20) {
248 x = x/20;
249 y = y/20;
250 if(spieler.schuss(x,y)) {
251 printField.setCursor(PrintField.CURSOR_NORMAL);
252 todo = -1;
253 printField.repaint();
254 setCommandText("Bitte warten Sie bis der Gegner geschossen hat");
255 synchronized(spieler) {
256 spieler.notify();
257 }
258 }
259 }
260 }
261 }
262 public void mouseReleased(MouseEvent e){
263 if(todo==Callback.SCHIFFE_SETZEN){
264 if(cursorAusrichtung!=0){
265 printField.setCursor(PrintField.CURSOR_NORMAL);
266 // System.out.println("X: " + e.getX() + " Y: "+e.getY());
267 if(e.getX()>=xStart && e.getY()>=yStart && e.getX() <= xStart+length*20 && e.getY() <= yStart+length*20) {
268 int x = (e.getX()-xStart) / 20;
269 int y = (e.getY()-yStart) / 20;
270 int breit = aktuellesSchiff+1;
271 int lang = 1;
272 if(breit == 5) {
273 breit = 4;
274 lang = 2;
275 }
276 if(cursorAusrichtung == 2) {
277 int buffer = breit;
278 breit = lang;
279 lang = buffer;
280 }
281 // System.out.println("Versuche zu setzen lang: "+lang+" breit: "+breit);
282 if(spieler.addShip(new schiffeversenken.spieler.Schiffe(x,y,breit,lang))) {
283 shipArray2[aktuellesSchiff-1] = shipArray2[aktuellesSchiff-1]-1;
284 //System.out.println("The ShipArray: \n"+
285 // "0: " + shipArray2[0]+"\n"+
286 // "1: " + shipArray2[1]+"\n"+
287 // "2: " + shipArray2[2]+"\n"+
288 // "3: " + shipArray2[3]);
289 if(shipArray2[0]==0 && shipArray2[1]==0 && shipArray2[2] == 0 && shipArray2[3] == 0) {
290 // System.out.println("Schiffe gesetzt");
291 todo = -1;
292 synchronized(spieler) {
293 spieler.notify();
294 }
295 }
296 // System.out.println("Schiff erfolgreich gesetzt");
297 }
298 printField.repaint();
299 aktuellesSchiff = 0;
300 cursorAusrichtung = 0;
301 // System.out.println("X: " +x+" Y: "+ y);
302 }
303 }
304 }
305 }
306 public void mousePressed(MouseEvent e){
307 if(todo==Callback.SCHIFFE_SETZEN){
308 int x = e.getX();
309 int y = e.getY();
310 // System.out.println("Cursor Position: x: "+x+" y: "+y);
311 aktuellesSchiff = 0;
312 if((y>=(length*20+yStart+15))&&(y<=length*20+yStart+15+70)){
313 // System.out.println("y is ok");
314 if(x>49) {
315 int feld = (x-50)/40+1;
316 // System.out.println("Feld: " + feld);
317 if(shipArray2[0]>=feld)
318 aktuellesSchiff = 1;
319 else if(shipArray2[0]+shipArray2[1] >=feld)
320 aktuellesSchiff = 2;
321 else if(shipArray2[0]+shipArray2[1]+shipArray2[2] >= feld)
322 aktuellesSchiff = 3;
323 else if(shipArray2[0]+shipArray2[1]+shipArray2[2]+shipArray2[3] >= feld)
324 aktuellesSchiff = 4;
325 if(aktuellesSchiff!=0) {
326 // System.out.println("Aktuelles Schiff: " + aktuellesSchiff);
327 printField.setCursor(PrintField.CURSOR_HORIZONTAL);
328 cursorAusrichtung = 1;
329 }
330 }
331 /*else {
332 System.out.println("Zu weit links");
333 }*/
334 }
335 }
336 }
337 public void mouseEntered(MouseEvent e){}
338 public void mouseExited(MouseEvent e){}
339
340 public void keyPressed(KeyEvent ke){}
341 public void keyReleased(KeyEvent ke) {}
342 public void keyTyped(KeyEvent ke) {
343 if(todo==Callback.SCHIFFE_SETZEN) {
344 if(ke.getKeyChar() == ' ')
345 if(cursorAusrichtung == 1) {
346 printField.setCursor(PrintField.CURSOR_VERTICAL);
347 cursorAusrichtung = 2;
348 }
349 else if(cursorAusrichtung == 2) {
350 printField.setCursor(PrintField.CURSOR_HORIZONTAL);
351 cursorAusrichtung = 1;
352 }
353 }
354 }
355
356 public int getLength(){return length;}
357 public void setCommandText(String s) {
358 lblCommand.setText(s);
359 }
360 public void chat(String s) {
361 }
362
363
364
365
366 class PrintField extends Container {
367 Cursor c_h, c_v, c_n, c_s;
368 Toolkit myKit;
369
370 public final static int CURSOR_NORMAL = 0;
371 public final static int CURSOR_HORIZONTAL = 1;
372 public final static int CURSOR_VERTICAL = 2;
373 public final static int CURSOR_SHOOT = 3;
374
375 public PrintField(GUI g) {
376 this.setSize(2000,2000);
377 this.setLocation(0,0);
378 this.setBackground(Color.black);
379 this.addMouseListener(g);
380 c_n = new Cursor(Cursor.DEFAULT_CURSOR);
381 c_s = new Cursor(Cursor.CROSSHAIR_CURSOR);
382 myKit = myKit.getDefaultToolkit();
383 java.io.File f = new java.io.File("cursor_h.gif");
384 if(!f.exists()) {
385 System.out.println("cursor_h.gif not found");
386 System.out.println("Program will terminate");
387 System.exit(-1);
388 }
389 f = new java.io.File("cursor_v.gif");
390 if(!f.exists()) {
391 System.out.println("cursor_v.gif not found");
392 System.out.println("Program will terminate");
393 System.exit(-1);
394 }
395 c_h = myKit.createCustomCursor(myKit.getImage("cursor_h.gif"),new Point(16,16),"Cursor_H");
396 c_v = myKit.createCustomCursor(myKit.getImage("cursor_v.gif"),new Point(16,16),"Cursor_V");
397 }
398 public void setCursor(int cursor) {
399 switch(cursor) {
400 case CURSOR_NORMAL:
401 this.setCursor(c_n);
402 break;
403 case CURSOR_HORIZONTAL:
404 this.setCursor(c_h);
405 break;
406 case CURSOR_VERTICAL:
407 this.setCursor(c_v);
408 break;
409 case CURSOR_SHOOT:
410 this.setCursor(c_s);
411 break;
412 }
413 // System.out.println("Cursor: " + cursor);
414 }
415 public void paint(Graphics g) {
416 paintCommponents(g);
417 }
418 public void paintCommponents(Graphics field)
419 {
420 int x=GUI.this.xStart;
421 int y=GUI.this.yStart;
422 field.setColor(Color.black);
423 field.fillRect(0,0,2000,2000);
424
425 for(int k=1;k<=GUI.this.length;k++){
426 for(int i=1;i<=GUI.this.length;i++){
427 field.setColor(Color.blue);
428 field.fillRect(x,y,20,20);
429 field.setColor(Color.white);
430 field.drawRect(x,y,20,20);
431 x = x + 20;}
432 y = y+20;
433 x = x-(GUI.this.length*20);
434 }
435
436 x = x+50+(GUI.this.length*20);
437 y = GUI.this.yStart;
438
439 for(int j=1;j<=GUI.this.length;j++){
440 for(int l=1;l<=GUI.this.length;l++){
441 field.setColor(Color.blue);
442 field.fillRect(x,y,20,20);
443 field.setColor(Color.white);
444 field.drawRect(x,y,20,20);
445 x = x + 20;}
446 y = y+20;
447 x = x-(GUI.this.length*20);}
448
449 int p =GUI.this.xStart-20;
450 int q =GUI.this.yStart+15;
451
452 for(int m=0;m<GUI.this.length;m++){
453 field.setFont(Font.getFont("Arial"));
454 field.setColor(Color.white);
455 field.drawString(String.valueOf((char)('A'+m)),p,q);
456
457 q = q+20;}
458
459 p = p+50+(GUI.this.length*20);
460 q = GUI.this.yStart+15;
461
462 for(int m=0;m<GUI.this.length;m++){
463 field.setFont(Font.getFont("Arial"));
464 field.setColor(Color.white);
465 field.drawString(String.valueOf((char)('A'+m)),p,q);
466 q = q+20;}
467
468 p=GUI.this.xStart+5;
469 q=GUI.this.yStart-10;
470
471 for(int n=1;n<=GUI.this.length;n++){
472 field.drawString(String.valueOf(n),p,q);
473 p = p+20;
474 }
475
476 p = p+50;
477 q = GUI.this.yStart-10;
478
479 for(int o=1;o<=GUI.this.length;o++){
480 field.drawString(String.valueOf(o),p,q);
481 p = p+20;}
482
483
484 field.drawString("Enemy ships",GUI.this.xStart+90+(2*length*20),GUI.this.yStart-10);
485
486 int rectx = GUI.this.xStart+90+(2*length*20);
487 int recty = GUI.this.yStart;
488 int heigth = (length*20);
489
490 field.drawRect(rectx,recty,100,heigth);
491
492 int xachse= GUI.this.xStart+100+(2*length*20);
493 int yachse= GUI.this.yStart+10;
494
495 for(int i=1;i<=GUI.this.shipArray[0];i++){
496 paintShips(2,xachse,yachse,field);
497 yachse = yachse+15;
498 }
499
500 for(int i=1;i<=GUI.this.shipArray[1];i++){
501 paintShips(3,xachse,yachse,field);
502 yachse = yachse+15;
503 }
504
505 for(int i=1;i<=GUI.this.shipArray[2];i++){
506 paintShips(4,xachse,yachse,field);
507 yachse = yachse+15;
508 }
509
510 for(int i=1;i<=GUI.this.shipArray[3];i++){
511 paintShips(8,xachse,yachse,field);
512 yachse = yachse+25;
513 }
514
515 y = length*20+GUI.this.yStart+15;
516 int width = 2*20*length+50;
517 field.drawRect(50,y,width,70);
518
519 y = length*20+GUI.this.yStart+15+80;
520 field.drawRect(50,y,width,20);
521
522 zeichneSchiffe(field);
523 paintFieldShips(field);
524 super.paintComponents(field);
525 } //end paint
526
527 public void zeichneSchiffe(Graphics g){
528 int x,y;
529 x = 50;
530 y = 80+(GUI.this.length*20);
531
532 for(int i=0;i<GUI.this.shipArray2[0];i++){
533 zeichneSchiff(g,x+15,y,10,20,Color.red);
534 x = x+40;
535 }
536 for(int j=0;j<GUI.this.shipArray2[1];j++){
537 zeichneSchiff(g,x+15,y,10,30,Color.red);
538 x = x+40;
539 }
540 for(int k=0;k<GUI.this.shipArray2[2];k++){
541 zeichneSchiff(g,x+15,y,10,40,Color.red);
542 x = x+40;
543 }
544 for(int l=0;l<GUI.this.shipArray2[3];l++){
545 zeichneSchiff(g,x+10,y,20,40,Color.red);
546 x = x+40;
547 }
548 } //end zeichneSchiffe
549
550 public void paintShips(int shipKind, int xachse, int yachse, Graphics field){
551 if(shipKind != 8)
552 {
553 for(int i=1; i<=shipKind; i++)
554 {
555 field.drawRect(xachse,yachse,10,10);
556 xachse= xachse+10;
557 }
558 }
559 else
560 {
561 for(int k=1; k<=2; k++)
562 {
563 for(int i=1; i<=4; i++)
564 {
565 field.drawRect(xachse,yachse,10,10);
566 xachse= xachse+10;
567 }
568 xachse = xachse -40;
569 yachse = yachse +10;
570 }
571 }
572 }//end paintShips$
573
574 public void zeichneSchiff(Graphics ship,int x, int y, int width, int height, Color c){
575 ship.setColor(c);
576 ship.fillRect(x,y,width,height);
577 }
578
579 public void paintFieldShips(Graphics g) {
580 if(spieler!= null) {
581 schiffeversenken.spieler.Spielfeld sp = spieler.getEigenesSpielfeld();
582 schiffeversenken.spieler.Spielfeld sp2 = spieler.getGegnerSpielfeld();
583 for(int i=0; i<GUI.this.getLength();i++) {
584 for(int j=0; j<GUI.this.getLength();j++) {
585 // System.out.println("Zustand x: "+i +" y: "+j +" z: "+sp.getZustand(i,j));
586 if(sp.getZustand(i,j)== 1) {
587 schiffeversenken.spieler.Schiffe schiff = sp.schiff(i,j);
588 int x = xStart + schiff.getX()*20+5;
589 int y = yStart + schiff.getY()*20+5;
590 int width = schiff.getSchiffBreite()*20 -10;
591 int height = schiff.getSchiffLaenge()*20 -10;
592 // System.out.println("Zeichne Schiff: x: " +x+" y: "+y+" width :"+width+" height: "+height);
593 zeichneSchiff(g,x,y,width,height,Color.green);
594 }
595 }
596 }
597 for(int i=0; i<GUI.this.getLength();i++) {
598 for(int j=0;j<GUI.this.getLength();j++) {
599 // Eigenes Spielfeld zeichnen
600 if(sp.getZustand(i,j)==sp.SCHIFF_VORHANDEN) {
601 schiffeversenken.spieler.Schiffe schiff = sp.schiff(i,j);
602 int x = xStart + schiff.getX()*20+5;
603 int y = yStart + schiff.getY()*20+5;
604 int width = schiff.getSchiffBreite()*20 -10;
605 int height = schiff.getSchiffLaenge()*20 -10;
606 zeichneSchiff(g,x,y,width,height,Color.green);
607 }
608 else if(sp.getZustand(i,j)==sp.SCHIFF_GETROFFEN) {
609 int x = xStart + i *20+5;
610 int y = yStart + j *20+5;
611 zeichneSchiff(g,x,y,10,10,Color.red);
612 }
613 else if(sp.getZustand(i,j)==sp.INS_WASSER) {
614 int x = xStart + i *20 +5;
615 int y = yStart + j *20 +5;
616 zeichneSchiff(g,x,y,10,10,Color.black);
617 }
618 else if(sp.getZustand(i,j)==sp.SCHIFF_VERSENKT) {
619 schiffeversenken.spieler.Schiffe schiff = sp.schiff(i,j);
620 int x = xStart + schiff.getX()*20 +5;
621 int y = yStart + schiff.getY()*20 +5;
622 int width = schiff.getSchiffBreite()*20 -10;
623 int height = schiff.getSchiffLaenge()*20 -10;
624 zeichneSchiff(g,x,y,width,height,Color.red);
625 }
626 // Gegnerisches Spielfeld zeichnen
627 /* if(sp2.getZustand(i,j)==sp.SCHIFF_VORHANDEN) {
628 schiffeversenken.spieler.Schiffe schiff = sp2.schiff(i,j);
629 if(schiff!=null) {
630 int x = xStart + schiff.getX()*20+55+GUI.this.length*20;
631 int y = yStart + schiff.getY()*20+5;
632 int width = schiff.getSchiffBreite()*20 -10;
633 int height = schiff.getSchiffLaenge()*20 -10;
634 zeichneSchiff(g,x,y,width,height,Color.green);
635 }
636 }*/
637 if(sp2.getZustand(i,j)==sp.SCHIFF_GETROFFEN) {
638 int x = xStart + i*20+55+ GUI.this.length *20;
639 int y = yStart + j*20+5;
640 zeichneSchiff(g,x,y,10,10,Color.red);
641 }
642 else if(sp2.getZustand(i,j)==sp.INS_WASSER) {
643 int x = xStart + i *20 + 55+ GUI.this.length *20;
644 int y = yStart + j *20 + 5;
645 zeichneSchiff(g,x,y,10,10,Color.black);
646 }
647 else if(sp2.getZustand(i,j)==sp.SCHIFF_VERSENKT) {
648 schiffeversenken.spieler.Schiffe schiff = sp2.schiff(i,j);
649 if(schiff!=null) {
650 int x = xStart + schiff.getX() *20 +55+GUI.this.length*20;
651 int y = yStart + schiff.getY() *20+5;
652 int width = schiff.getSchiffBreite()*20-10;
653 int height = schiff.getSchiffLaenge()*20 -10;
654 zeichneSchiff(g,x,y,width,height,Color.red);
655 }
656 }
657 }
658 }
659 }
660 }
661 } //end PrintField
662 }