Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: schiffeversenken/NeuesSpiel.java


1   package schiffeversenken;
2   
3   
4   /*
5     JavaBattle
6     ==========
7     
8     published under GNU General Public License (GPL)
9   
10    schoetz@users.sourceforge.net
11    
12  */
13  
14  /*
15    NeuesSpiel
16    ==========
17    
18    Dialog des Guis um ein neues Spiel zu erstellen
19  */
20  
21  import java.awt.*;
22  import java.awt.event.*;
23  import javax.swing.*;
24  
25  public class NeuesSpiel extends JDialog implements ActionListener, ItemListener, KeyListener
26  {
27      JButton btnOK, btnAb;
28      ButtonGroup boxModus;
29      JRadioButton boxPvsCPU, boxPvsPS, boxPvsPC;
30      JTextField txtName, txtGroesse, txtIP, txtPort;
31      GUI gui;
32      View v;
33      
34      public NeuesSpiel(GUI gui1){
35    
36    super(gui1,"Neues Spiel",true);
37    
38    gui = gui1;
39    v = new View();
40    this.getContentPane().add(v);
41    this.setSize(390,290);
42    this.setLocation(200,200);
43    this.getContentPane().setLayout(null);
44    this.setResizable(false);
45    this.toFront();
46    
47    
48    boxModus = new ButtonGroup();
49    
50    boxPvsCPU = new JRadioButton("gegen den Computer",true);
51    boxPvsCPU.setSize(150,20);
52    boxPvsCPU.setLocation(40,40);
53    boxPvsCPU.setForeground(Color.white);
54    boxPvsCPU.setBackground(Color.black);
55    boxPvsCPU.addKeyListener(this);
56    boxModus.add(boxPvsCPU);
57    
58    boxPvsPS = new JRadioButton("im Netzwerk, als Server",false);
59    boxPvsPS.setSize(150,20);
60    boxPvsPS.setLocation(40,70);
61    boxPvsPS.setForeground(Color.white);
62    boxPvsPS.setBackground(Color.black);
63    boxPvsPS.addKeyListener(this);
64    boxModus.add(boxPvsPS);
65    
66    boxPvsPC = new JRadioButton("im Netzwerk, als Client",false);
67    boxPvsPC.setSize(150,20);
68    boxPvsPC.setLocation(40,100);
69    boxPvsPC.setForeground(Color.white);
70    boxPvsPC.setBackground(Color.black);
71    boxPvsPC.addKeyListener(this);
72    boxModus.add(boxPvsPC);
73    
74    boxPvsCPU.addItemListener(this);
75    boxPvsPS.addItemListener(this);
76    boxPvsPC.addItemListener(this);
77    
78    v.add(boxPvsCPU);
79    v.add(boxPvsPS);
80    v.add(boxPvsPC);
81    
82    txtName = new JTextField("Player 1");
83    txtGroesse = new JTextField("10");
84    txtIP = new JTextField();
85    txtPort = new JTextField();
86    
87    txtName.setSize(130,20);
88    txtGroesse.setSize(130,20);
89    txtIP.setSize(130,20);
90    txtPort.setSize(95,20);
91    
92    txtName.setLocation(215,40);
93    txtGroesse.setLocation(215,110);
94    txtIP.setLocation(60,155);
95    txtPort.setLocation(250,155);
96    
97    txtIP.setEditable(false);
98    txtGroesse.setEditable(true);
99    txtPort.setEditable(false);
100   
101   txtName.addKeyListener(this);
102   txtGroesse.addKeyListener(this);
103   txtPort.addKeyListener(this);
104   txtIP.addKeyListener(this);
105   
106   v.add(txtName);
107   v.add(txtGroesse);
108   v.add(txtIP);
109   v.add(txtPort);
110   
111   btnOK = new JButton("OK");
112   btnOK.addActionListener(this);
113   btnOK.setSize(100,20);
114   btnOK.setLocation(30,210);
115   v.add(btnOK);
116   
117   btnAb = new JButton("Abbrechen");
118   btnAb.addActionListener(this);
119   btnAb.setSize(100,20);
120   btnAb.setLocation(190,210);
121   v.add(btnAb);
122   
123   this.setVisible(true);
124     } 
125     
126     public void actionPerformed(ActionEvent ae)
127     {
128   Object object = ae.getSource();
129   if(object.equals(btnOK)){
130       this.setVisible(false);
131       String n = new String();
132       n = txtGroesse.getText();
133       int i = Integer.parseInt(n);
134       if((i<5)||(i>15)){
135     if(i<5){i=5;}
136     if(i>15){i=15;}}
137       gui.reset(i);
138       if(boxPvsCPU.isSelected()){gui.initialisiere(i,gui);}
139       if(boxPvsPS.isSelected()){gui.initialisiere(i,Integer.parseInt(txtPort.getText()),gui);}
140       if(boxPvsPC.isSelected()){gui.initialisiere(i,Integer.parseInt(txtPort.getText()),txtIP.getText(),gui);}
141       
142   }
143   if(object.equals(btnAb)){this.setVisible(false);}
144   
145     }
146     
147     public void itemStateChanged(ItemEvent ie)
148     {
149   Object objitem = ie.getSource();
150   if(objitem.equals(boxPvsCPU)){
151       txtIP.setEditable(false);
152       txtPort.setEditable(false);
153       txtGroesse.setEditable(true);}
154   if(objitem.equals(boxPvsPS)){
155       txtIP.setEditable(false);
156       txtPort.setEditable(true);
157       txtGroesse.setEditable(true);}
158   if(objitem.equals(boxPvsPC)){
159       txtIP.setEditable(true);
160       txtPort.setEditable(true);
161       txtGroesse.setEditable(false);}
162     }
163     
164     public void keyPressed(KeyEvent ke){
165   if(ke.getKeyCode()==KeyEvent.VK_ENTER){
166       this.setVisible(false);
167       String n = new String();
168       n = txtGroesse.getText();
169       int i = Integer.parseInt(n);
170       if((i<5)||(i>15)){
171     if(i<5){i=5;}
172     if(i>15){i=15;}}
173       gui.reset(i);
174       if(boxPvsCPU.isSelected()){gui.initialisiere(i,gui);}
175       if(boxPvsPS.isSelected()){gui.initialisiere(i,Integer.parseInt(txtPort.getText()),gui);}
176       if(boxPvsPC.isSelected()){gui.initialisiere(i,Integer.parseInt(txtPort.getText()),txtIP.getText(),gui);}
177       
178   }}
179     public void keyReleased(KeyEvent ke){}
180     public void keyTyped(KeyEvent ke){}
181     class View extends Container{
182   public View() {
183       this.setSize(1000,1000);
184       this.setLocation(0,0);
185       this.setLayout(null);
186   }
187   public void paint(Graphics g) {
188       paintCommponents(g);
189   }
190   public void paintCommponents(Graphics newGame)
191   {
192       newGame.setColor(Color.black);
193       newGame.fillRect(0,0,1000,1000);
194       newGame.setColor(Color.white);
195       newGame.drawString("Modus:",35,30);
196       newGame.drawString("Spielername:",215,30);
197       newGame.drawString("Spielfeldgrösse (5-15):",215,100);
198       newGame.drawString("IP:",35,170);
199       newGame.drawString("Port:",215,170);
200       newGame.drawRect(30,10,170,130);  //Modus
201       newGame.drawRect(210,10,150,60);  //Spielername
202       newGame.drawRect(210,80,150,60); //Spielfeldgrösse
203       newGame.drawRect(30,150,170,30);  //IP
204       newGame.drawRect(210,150,150,30); //Port
205       super.paintComponents(newGame);
206   }
207     }   
208 }