Probleme mit repaint() bzw. der Programstruktur

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • Probleme mit repaint() bzw. der Programstruktur

    Ich habe Probleme mit der Programmstruktur eines Othello-Programmes.
    Es existiert eine Applikation, ein Board und ein Frame.
    Dies sind einzelne Dateien bzw. getrennte Klassen.
    Nun muß das Board das Frame, das Frame das Board und die Applikation beide kennen. Eventuell benötige ich noch Klassen für die Spieler. Wie verwalte ich die Kommunikation der Klassen untereinander ?
    Soll ich die Instanzen der einzelnen Klassen zentral in der Applikation verwalten ?
    Ich habe bereits versucht die einzelnen Instanzen jeweils als Parameter zu übergeben. Hier ergaben sich aber Probleme mit paint() bzw. repaint().
    Ich kann das Board, das Frame usw. in der Applikation verwalten wenn ich die entsprechenden Instanzen als static deklariere (Siehe unten).
    Nachfolgend ein kleiner Aussschnitt aus dem Programm.

    Quellcode

    1. package classes;
    2. import java.awt.*;
    3. import java.awt.event.*;
    4. import javax.swing.*;
    5. import info.clearthought.layout.TableLayout;
    6. import classes.OthelloFrame;
    7. import classes.OthelloBoard;
    8. import classes.OthelloPlayer;
    9. public class OthelloApplication
    10. {
    11. static public OthelloFrame OthelloFrame;
    12. static public OthelloBoard OthelloBoard;
    13. static public OthelloPlayer firstPlayer, secondPlayer;
    14. public OthelloApplication ()
    15. {
    16. initComponents();
    17. }
    18. // -----------------------------------------------------
    19. public static void main(String[] args)
    20. {
    21. new OthelloApplication();
    22. } // main
    23. private void initComponents ()
    24. {
    25. OthelloBoard = new OthelloBoard(8);
    26. OthelloFrame = new OthelloFrame("Othello");
    27. // The Board as a Parameter ???
    28. firstPlayer = new OthelloPlayer (OthelloBoard);
    29. secondPlayer = new OthelloPlayer (OthelloBoard);
    30. }
    31. }
    Alles anzeigen


    Quellcode

    1. package classes;
    2. import java.awt.*;
    3. import java.awt.event.*;
    4. import java.awt.image.*;
    5. import java.awt.geom.*;
    6. import java.awt.Window.*;
    7. import java.awt.Component.*;
    8. import java.applet.*;
    9. import java.net.*;
    10. import java.io.*;
    11. import javax.imageio.*;
    12. import java.lang.String;
    13. import java.lang.Enum;
    14. import java.net.URL;
    15. import javax.swing.*;
    16. import java.lang.Object;
    17. import java.lang.reflect.*;
    18. import java.util.Observable;
    19. import java.util.Observer;
    20. import java.util.ArrayList;
    21. import classes.OthelloFrame;
    22. import classes.OthelloPlayer;
    23. import interfaces.ConstInterface;
    24. import classes.GameBoard;
    25. public class OthelloBoard implements ConstInterface
    26. {
    27. private GameBoard Board;
    28. private OthelloFrame thisFrame;
    29. private void initComponents (int Size)
    30. {
    31. // The Board must know the Frame to call it for repaint()
    32. // This works only if OthelloFrame is declared as static
    33. thisFrame = OthelloApplication.OthelloFrame;
    34. Board = new GameBoard (Size);
    35. // Initialize the Board
    36. Board.setColor(Size/2-1,Size/2-1,EnumColor.WHITE);
    37. Board.setColor(Size/2,Size/2-1,EnumColor.BLACK);
    38. Board.setColor(Size/2-1,Size/2,EnumColor.BLACK);
    39. Board.setColor(Size/2,Size/2,EnumColor.WHITE);
    40. } // initComponents
    41. // Is called by the MouseAdapter in BoardPanel
    42. public void XYmousePressed (int row, int column)
    43. {
    44. System.out.println("mousePressed at OthelloBoard");
    45. // This is a test
    46. Board.setColor(row,column,EnumColor.WHITE);
    47. System.out.println("setColor at OthelloBoard");
    48. // Repaint the Frame
    49. thisFrame.repaint();
    50. }
    51. // Konstruktor 1
    52. public OthelloBoard (int Size)
    53. {
    54. initComponents (Size);
    55. }
    56. /* To transmit the Frame as a Parameter does not work
    57. public OthelloBoard (int Size, OthelloFrame OthelloFrame)
    58. {
    59. thisFrame = OthelloFrame;
    60. initComponents (Size);
    61. }
    62. */
    63. // ------------------ Get und Set-Methoden -----------------
    64. // getColor() wird von BoardPanel zum Neuzeichnen des Boards
    65. // abgefragt und ruft seinerseita getColor() in GameBoard auf
    66. public EnumColor getColor (int row, int column)
    67. {
    68. return (Board.getColor(row,column));
    69. }
    70. public void setColor (int row, int column, EnumColor color)
    71. {
    72. Board.setColor (row,column,color);
    73. }
    74. public void changeColor (int row, int column)
    75. {
    76. Board.changeColor (row,column);
    77. }
    78. }
    Alles anzeigen


    BoardPanel gibt den Mouseklick an das Frame weiter, das Frame an das Board.
    Dieses setzt den Spielstein und ruft das Neuzeichnens des Frames bzw. des Panels auf.
  • Re: Probleme mit repaint() bzw. der Programstruktur

    "HansWerner" schrieb:



    Quellcode

    1. package classes;
    2. ...
    3. public class OthelloApplication
    4. {
    5. static public OthelloFrame OthelloFrame;
    6. static public OthelloBoard OthelloBoard;
    7. static public OthelloPlayer firstPlayer, secondPlayer;
    8. public OthelloApplication ()
    9. {
    10. initComponents();
    11. }
    12. // -----------------------------------------------------
    13. public static void main(String[] args)
    14. {
    15. new OthelloApplication();
    16. } // main
    17. private void initComponents ()
    18. {
    19. OthelloBoard = new OthelloBoard(8);
    20. OthelloFrame = new OthelloFrame("Othello");
    21. // The Board as a Parameter ???
    22. firstPlayer = new OthelloPlayer (OthelloBoard);
    23. secondPlayer = new OthelloPlayer (OthelloBoard);
    24. }
    25. }
    Alles anzeigen


    Das sollte dir mindestens eine Warnung ausgeben. SEHR unsauber statische Variablen von den Instanzen setzen zu lassen. Die Variablennamen sind auch verwirrend.

    Das mit dem repaint hängt (glaube ich) damit zusammen, dass die GUI nicht sequentiell bezüglich der Aufrufe aufgebaut wird. Ich hatte das Problem auch mal, aber es recht gräußlich gelöst. Im Internet hab ich das gefunden:

    Quellcode

    1. SwingUtilities.invokeLater(new Runnable(){
    2. public void run(){
    3. doGuiTasks(); // accessing Swing GUI components
    4. doAnotherGuiTasks();
    5. }
    6. });


    SwingUtilities:
    http://www.dpunkt.de/java/Referenz/Das_Paket_javax.swing/170.html
    Runnable:
    http://www.dpunkt.de/java/Referenz/Das_Paket_java.lang/58.html

    Das Problem gibt es häufiger, google mal danach.

    PS: Ich habe vorrausgesetzt, dass du Swing verwendest und nicht AWT, SWT, etc.
  • Ja, ja, sehr unsauber. Stimmt ! Sonst hätte ich wahrscheinlich nicht so viele Probleme.
    Aber wie macht man es besser ?
    Die einzelnen Klassen sollen nach Möglichkeit unabhängig voneinander sein und miteinander kommunizieren. Also nicht "class Board extends Frame".
    Ich verwende Swing. Stimmt !
    Jetzt bitte nicht zusätzlich auch noch Threads.
    Was meinst du mit "dass die GUI nicht sequentiell bezüglich der Aufrufe aufgebaut wird" ? Es gibt eine Klasse OthelloFrame und von dieser habe ich, zwecks Übersichtsgründen (Die einzelnen Dateien sollten nicht so groß werden) zwei Klassen für die beiden Panels abgespalten.
  • Also:
    erstmal solltest du entscheiden ob du von OthelloApplication Instanzen brauchst. Wenn nein, dann brauchst du keinen Konstruktor und kannst alle Funktionen und attribute statisch machen. Wenn ja, dann mach bei den Attributen das static weg.

    class Board extends Frame
    ist AWT.
    class Board extends JFrame
    sollte Swing sein.

    Die Variablennamen beginnt man mit einem kleinen Buchstaben. Es ist OK, wenn man den Typ im Bezeichner unterbringt. z.B.:
    leftFrame
    rightFrame
    othelloBoard
    othelloFrame
    Das sorgt dafür, dass man die Variablen gut von den Klassen unterscheiden kann. Hat man einen Methodenaufruf
    OthelloApplication.abc(); dann kann man nun auf einen Blick sagen, ob es sich um eine statische Methode der Klasse oder eine Methode der Instanz handelt (in diesem Fall eine statische Methode der Klasse).

    Jetzt bitte nicht zusätzlich auch noch Threads.

    Threads sind nicht wirklich der Brüller in sachen Schwierigkeit. Wenn du nicht magst, dann musst du eben mal nach einer anderen Lösung googeln.
    Ersetze doch testweise einfach mal dein:

    Quellcode

    1. thisFrame.repaint();

    mit

    Quellcode

    1. SwingUtilities.invokeLater(new Runnable(){
    2. public void run(){
    3. thisFrame.repaint();
    4. }
    5. });

    (ungetestet, keine Garantie, funktioniert wohl eh nur bei Swing)

    Du solltest dir vielleicht Gedanken machen, wie du die grafische Darstellung von der Programmlogik trennst. Das bringt enorm mehr Übersicht.
  • Vereinfachung

    Also ich habe es mal ein bißchen vereinfacht.
    Nach dem 0815-Schema in den meisten Büchern bzw. Beispielen ist jetzt:
    OthelloBoard extends JPanel
    Also Programmlogik und graphische Darstellung erstmal gemischt.
    Probleme gibt es noch anscheinend noch beim GraphicContext mit Graphics bzw. getGraphics. Der Inhalt des JFrames wird im Panel noch einmal dargestellt. Und zwar dann wenn ein Mouseklick erfolgt daraufhin ein Stein gesetzt wird und repaint aufgerufen wird.
    Diesmal einfach etwas mehr. Der gerösste Teil des Codes ist irrelevant.

    OthelloApplication generiert diesmal nur ein OthelloFrame.

    Quellcode

    1. package classes;
    2. import java.awt.*;
    3. import java.awt.event.*;
    4. import javax.swing.*;
    5. import info.clearthought.layout.TableLayout;
    6. import classes.OthelloFrame; // OthelloFrame extends JFrame
    7. import classes.OthelloPlayer;
    8. public class OthelloApplication
    9. {
    10. public OthelloFrame OthelloFrame;
    11. public OthelloPlayer firstPlayer, secondPlayer;
    12. public OthelloApplication ()
    13. {
    14. initComponents();
    15. }
    16. // -----------------------------------------------------
    17. public static void main(String[] args)
    18. {
    19. System.out.println("new OthelloApplication");
    20. new OthelloApplication();
    21. } // main
    22. private void initComponents ()
    23. {
    24. System.out.println("new OthelloFrame");
    25. OthelloFrame = new OthelloFrame("Othello");
    26. // The Board as a Parameter ???
    27. // firstPlayer = new OthelloPlayer ();
    28. // secondPlayer = new OthelloPlayer ();
    29. }
    30. }
    Alles anzeigen



    Hier das entsprechende JFrame. Alles uninteressant bis auf den Konstruktor und initComponents und die Zeile BoardPanel = new OthelloBoard (8). Hier wird wie bereits oben angegeben ein neues JPanel erzeugt.

    Quellcode

    1. package classes;
    2. // Allgemeines
    3. import java.awt.*;
    4. import java.awt.image.*;
    5. import java.awt.geom.*;
    6. import java.awt.event.*;
    7. import java.applet.*;
    8. import java.net.*;
    9. import java.io.*;
    10. import javax.imageio.*;
    11. import java.lang.reflect.*;
    12. // Swing
    13. import javax.swing.*;
    14. import javax.swing.border.*;
    15. import javax.swing.table.*;
    16. // Layouts
    17. import com.jgoodies.uif_lite.panel.*;
    18. import info.clearthought.layout.*;
    19. // Interfaces
    20. import interfaces.ConstInterface;
    21. // Observer
    22. import java.util.Observer;
    23. import java.util.Observable;
    24. // Panels
    25. // import classes.BoardPanel;
    26. // import classes.PlayerPanel;
    27. // import classes.BoardSettingsInternalFrame;
    28. import classes.OthelloSound;
    29. // import classes.OthelloBoard;
    30. // import classes.GameBoard;
    31. // import classes.OthelloApplication;
    32. public class OthelloFrame extends JFrame
    33. {
    34. /* ----------------- BoardSettingsDialog ------------------------ */
    35. public class BoardSettingsDialog extends JDialog
    36. {
    37. public BoardSettingsDialog(Frame owner)
    38. {
    39. super(owner);
    40. initComponents();
    41. }
    42. public BoardSettingsDialog(Dialog owner)
    43. {
    44. super(owner);
    45. initComponents();
    46. }
    47. private JPanel dialogPane;
    48. private JPanel contentPanel;
    49. private JLabel label4;
    50. private JSpinner spinner1;
    51. private JLabel label2;
    52. private JSpinner spinner2;
    53. private JLabel label3;
    54. private JSpinner spinner3;
    55. private JPanel buttonBar;
    56. private JButton okButton;
    57. private JButton cancelButton;
    58. private void initComponents()
    59. {
    60. dialogPane = new JPanel();
    61. contentPanel = new JPanel();
    62. label4 = new JLabel();
    63. spinner1 = new JSpinner();
    64. label2 = new JLabel();
    65. spinner2 = new JSpinner();
    66. label3 = new JLabel();
    67. spinner3 = new JSpinner();
    68. buttonBar = new JPanel();
    69. okButton = new JButton();
    70. cancelButton = new JButton();
    71. //======== this ========
    72. Container contentPane = getContentPane();
    73. contentPane.setLayout(new BorderLayout());
    74. //======== dialogPane ========
    75. {
    76. dialogPane.setBorder(new EmptyBorder(12, 12, 12, 12));
    77. dialogPane.setLayout(new TableLayout(new double[][] {
    78. {TableLayout.FILL},
    79. {TableLayout.FILL, TableLayout.PREFERRED}}));
    80. //======== contentPanel ========
    81. {
    82. contentPanel.setLayout(new TableLayout(new double[][] {
    83. {TableLayout.PREFERRED, 70},
    84. {TableLayout.PREFERRED, TableLayout.PREFERRED, TableLayout.PREFERRED}}));
    85. ((TableLayout)contentPanel.getLayout()).setHGap(5);
    86. ((TableLayout)contentPanel.getLayout()).setVGap(5);
    87. //---- label4 ----
    88. label4.setText("Size of the Board");
    89. contentPanel.add(label4, new TableLayoutConstraints(0, 0, 0, 0, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
    90. //---- spinner1 ----
    91. spinner1.setModel(new SpinnerListModel(new String[] {"10x10", "8x8", "6x6"}) {
    92. { setValue("8x8"); }
    93. });
    94. contentPanel.add(spinner1, new TableLayoutConstraints(1, 0, 1, 0, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
    95. //---- label2 ----
    96. label2.setText("Color for Player 1");
    97. contentPanel.add(label2, new TableLayoutConstraints(0, 1, 0, 1, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
    98. //---- spinner2 ----
    99. spinner2.setModel(new SpinnerListModel(new String[] {"Yellow", "White", "Red", "Green", "Blue", "Black", "Aquamarin"}) {
    100. { setValue("White"); }
    101. });
    102. contentPanel.add(spinner2, new TableLayoutConstraints(1, 1, 1, 1, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
    103. //---- label3 ----
    104. label3.setText("Color for Player 2");
    105. contentPanel.add(label3, new TableLayoutConstraints(0, 2, 0, 2, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
    106. //---- spinner3 ----
    107. spinner3.setModel(new SpinnerListModel(new String[] {"Yellow", "White", "Red", "Green", "Blue", "Black", "Aquamarin"}) {
    108. { setValue("Black"); }
    109. });
    110. contentPanel.add(spinner3, new TableLayoutConstraints(1, 2, 1, 2, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
    111. }
    112. dialogPane.add(contentPanel, new TableLayoutConstraints(0, 0, 0, 0, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
    113. //======== buttonBar ========
    114. {
    115. buttonBar.setBorder(new EmptyBorder(12, 0, 0, 0));
    116. buttonBar.setLayout(new TableLayout(new double[][] {
    117. {TableLayout.FILL, TableLayout.PREFERRED, TableLayout.PREFERRED},
    118. {TableLayout.PREFERRED}}));
    119. ((TableLayout)buttonBar.getLayout()).setHGap(5);
    120. ((TableLayout)buttonBar.getLayout()).setVGap(5);
    121. //---- okButton ----
    122. okButton.setText("OK");
    123. buttonBar.add(okButton, new TableLayoutConstraints(1, 0, 1, 0, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
    124. //---- cancelButton ----
    125. cancelButton.setText("Cancel");
    126. buttonBar.add(cancelButton, new TableLayoutConstraints(2, 0, 2, 0, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
    127. }
    128. dialogPane.add(buttonBar, new TableLayoutConstraints(0, 1, 0, 1, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
    129. }
    130. contentPane.add(dialogPane, BorderLayout.CENTER);
    131. pack();
    132. setLocationRelativeTo(getOwner());
    133. }// initComponents
    134. }// BoardSettingsDialog
    135. /* ----------------------- BoardSettingsInternalFrame ------------- */
    136. public class BoardSettingsInternalFrame extends SimpleInternalFrame
    137. {
    138. private SimpleInternalFrame BoardSettings;
    139. private JPanel dialogPane;
    140. private JPanel contentPanel;
    141. private JLabel label4;
    142. private JSpinner spinner1;
    143. private JLabel label2;
    144. private JSpinner spinner2;
    145. private JLabel label3;
    146. private JSpinner spinner3;
    147. private JPanel buttonBar;
    148. private JButton okButton;
    149. private JButton cancelButton;
    150. private void initComponents()
    151. {
    152. BoardSettings = new SimpleInternalFrame();
    153. dialogPane = new JPanel();
    154. contentPanel = new JPanel();
    155. label4 = new JLabel();
    156. spinner1 = new JSpinner();
    157. label2 = new JLabel();
    158. spinner2 = new JSpinner();
    159. label3 = new JLabel();
    160. spinner3 = new JSpinner();
    161. buttonBar = new JPanel();
    162. okButton = new JButton();
    163. cancelButton = new JButton();
    164. //======== BoardSettings ========
    165. {
    166. Container BoardSettingsContentPane = BoardSettings.getContentPane();
    167. BoardSettingsContentPane.setLayout(new TableLayout(new double[][] {
    168. {TableLayout.FILL},
    169. {TableLayout.FILL}}));
    170. //======== dialogPane ========
    171. {
    172. dialogPane.setBorder(new EmptyBorder(12, 12, 12, 12));
    173. dialogPane.setLayout(new TableLayout(new double[][] {
    174. {TableLayout.FILL},
    175. {TableLayout.FILL, TableLayout.PREFERRED}}));
    176. //======== contentPanel ========
    177. {
    178. contentPanel.setLayout(new TableLayout(new double[][] {
    179. {TableLayout.PREFERRED, 70},
    180. {TableLayout.PREFERRED, TableLayout.PREFERRED, TableLayout.PREFERRED, TableLayout.PREFERRED}}));
    181. ((TableLayout)contentPanel.getLayout()).setHGap(5);
    182. ((TableLayout)contentPanel.getLayout()).setVGap(5);
    183. //---- label4 ----
    184. label4.setText("Size of the Board");
    185. contentPanel.add(label4, new TableLayoutConstraints(0, 0, 0, 0, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
    186. //---- spinner1 ----
    187. spinner1.setModel(new SpinnerListModel(new String[] {"10x10", "8x8", "6x6"}) {
    188. { setValue("8x8"); }
    189. });
    190. contentPanel.add(spinner1, new TableLayoutConstraints(1, 0, 1, 0, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
    191. //---- label2 ----
    192. label2.setText("Color for Player 1");
    193. contentPanel.add(label2, new TableLayoutConstraints(0, 1, 0, 1, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
    194. //---- spinner2 ----
    195. spinner2.setModel(new SpinnerListModel(new String[] {"Yellow", "White", "Red", "Green", "Blue", "Black", "Aquamarin"}) {
    196. { setValue("White"); }
    197. });
    198. contentPanel.add(spinner2, new TableLayoutConstraints(1, 1, 1, 1, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
    199. //---- label3 ----
    200. label3.setText("Color for Player 2");
    201. contentPanel.add(label3, new TableLayoutConstraints(0, 2, 0, 2, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
    202. //---- spinner3 ----
    203. spinner3.setModel(new SpinnerListModel(new String[] {"Yellow", "White", "Red", "Green", "Blue", "Black", "Aquamarin"}) {
    204. { setValue("Black"); }
    205. });
    206. contentPanel.add(spinner3, new TableLayoutConstraints(1, 2, 1, 2, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
    207. }
    208. dialogPane.add(contentPanel, new TableLayoutConstraints(0, 0, 0, 0, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
    209. //======== buttonBar ========
    210. {
    211. buttonBar.setBorder(new EmptyBorder(12, 0, 0, 0));
    212. buttonBar.setLayout(new TableLayout(new double[][] {
    213. {TableLayout.FILL, TableLayout.PREFERRED, TableLayout.PREFERRED},
    214. {TableLayout.PREFERRED}}));
    215. ((TableLayout)buttonBar.getLayout()).setHGap(5);
    216. ((TableLayout)buttonBar.getLayout()).setVGap(5);
    217. //---- okButton ----
    218. okButton.setText("OK");
    219. buttonBar.add(okButton, new TableLayoutConstraints(1, 0, 1, 0, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
    220. //---- cancelButton ----
    221. cancelButton.setText("Cancel");
    222. buttonBar.add(cancelButton, new TableLayoutConstraints(2, 0, 2, 0, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
    223. }
    224. dialogPane.add(buttonBar, new TableLayoutConstraints(0, 1, 0, 1, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
    225. }
    226. BoardSettingsContentPane.add(dialogPane, new TableLayoutConstraints(0, 0, 0, 0, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
    227. }
    228. } // initComponents
    229. // Konstruktor
    230. public BoardSettingsInternalFrame()
    231. {
    232. initComponents();
    233. }
    234. } // BoardSettingsInternalFrame
    235. /* ------------------------------------------------------------ */
    236. private OthelloFrame thisFrame;
    237. private GameBoard thisGameBoard;
    238. private OthelloBoard BoardPanel;
    239. private PlayerPanel PlayerPanel;
    240. private HelpAboutOptionPane HelpAboutOptionPane;
    241. private OthelloSound OthelloSound;
    242. private MenuBar MenuBar;
    243. private Container OthelloFrameContentPane;
    244. // --------------------- public -----------------------------
    245. public JMenu getOptionsMenu ()
    246. {
    247. return MenuBar.getOptionsMenu ();
    248. }
    249. // --------------------- Sounds --------------------------------
    250. private void playJRadioButton ()
    251. {
    252. OthelloSound.play (2);
    253. }
    254. private void playJCheckBox ()
    255. {
    256. OthelloSound.play (1);
    257. }
    258. private void playJWindow ()
    259. {
    260. OthelloSound.play (3);
    261. }
    262. private void playSetStone ()
    263. {
    264. OthelloSound.play (2);
    265. }
    266. // ----------------------------------------------------------
    267. // Konstruktor
    268. public OthelloFrame (String Title)
    269. {
    270. super (Title);
    271. System.out.println("Konstruktor OthelloFrame");
    272. initComponents ();
    273. setVisible (true);
    274. setResizable (false);
    275. OthelloFrameContentPane = getContentPane();
    276. OthelloFrameContentPane.setLayout(new TableLayout(new double[][]
    277. {
    278. {12, 500, 10, 150, 12},
    279. {12, 500, 12, TableLayout.PREFERRED, 12}
    280. }));
    281. System.out.println("add BoardPanel");
    282. OthelloFrameContentPane.add(BoardPanel, new TableLayoutConstraints(1, 1, 1, 1, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
    283. System.out.println("add PlayerPanel");
    284. OthelloFrameContentPane.add(PlayerPanel, new TableLayoutConstraints(3, 1, 3, 1, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
    285. pack();
    286. }
    287. private void initComponents ()
    288. {
    289. System.out.println("new MenuBar");
    290. MenuBar = new MenuBar ();
    291. setJMenuBar (MenuBar);
    292. System.out.println("new PlayerPanel");
    293. PlayerPanel = new PlayerPanel (this);
    294. System.out.println("new OthelloBoard");
    295. BoardPanel = new OthelloBoard();
    296. HelpAboutOptionPane = new HelpAboutOptionPane ();
    297. // SoundManager = new SoundManager (); // Lade die AudioClips
    298. // SoundManager.play(1); // Test !!!
    299. }
    300. public void printMethods (Object o)
    301. {
    302. // Gib alle Methoden dieser Klasse aus
    303. Method[] Methoden = o.getClass().getMethods();
    304. for (int i=0; i < Methoden.length; i++)
    305. {
    306. System.out.println("Methode: "+Methoden[i]);
    307. }
    308. }
    309. // Fragt das Board ab
    310. // OthelloBoard extends JPanel
    311. // OthelloBoard fragt GameBoard ab
    312. public ConstInterface.EnumColor getColor (int row, int column)
    313. {
    314. return (BoardPanel.getColor(row,column));
    315. }
    316. public class HelpAboutOptionPane extends JOptionPane
    317. {
    318. public HelpAboutOptionPane()
    319. {
    320. initComponents();
    321. }
    322. private void initComponents()
    323. {
    324. }
    325. private void showDialog (Component parent)
    326. {
    327. JOptionPane.showMessageDialog(parent,"Othello Version 0.1");
    328. }
    329. }
    330. /* ----------------------- FileMenu ------------------ */
    331. private class FileMenu extends JMenu
    332. {
    333. private LoadMenuItem LoadMenuItem;
    334. private SaveMenuItem SaveMenuItem;
    335. private SaveAsMenuItem SaveAsMenuItem;
    336. private ExitMenuItem ExitMenuItem;
    337. private class LoadMenuItem extends JMenuItem
    338. {
    339. private LoadMenuItem()
    340. {
    341. initComponents();
    342. }
    343. private void initComponents()
    344. {
    345. setText("Load ...");
    346. setIcon(null);
    347. setName("LoadMenuItem");
    348. }
    349. private void LoadMenuItemActionPerformed(ActionEvent e)
    350. {
    351. // SoundManager.play(1);
    352. }
    353. } // LoadMenuItem
    354. private class SaveMenuItem extends JMenuItem
    355. {
    356. private SaveMenuItem()
    357. {
    358. initComponents();
    359. }
    360. private void initComponents()
    361. {
    362. setText("Save");
    363. setName("SaveMenuItem");
    364. }
    365. private void SaveMenuItemActionPerformed(ActionEvent e)
    366. {
    367. // SoundManager.play(1);
    368. }
    369. } // SaveMenuItem
    370. private class SaveAsMenuItem extends JMenuItem
    371. {
    372. private SaveAsMenuItem()
    373. {
    374. initComponents();
    375. }
    376. private void initComponents()
    377. {
    378. setText("Save As ...");
    379. setName("SaveAsMenuItem");
    380. }
    381. private void SaveAsMenuItemActionPerformed(ActionEvent e)
    382. {
    383. // SoundManager.play(1);
    384. }
    385. } // SaveAsMenuItem
    386. private class ExitMenuItem extends JMenuItem
    387. {
    388. private ExitMenuItem()
    389. {
    390. initComponents();
    391. }
    392. private void initComponents()
    393. {
    394. setText("Exit");
    395. setName("ExitMenuItem");
    396. }
    397. private void ExitMenuItemActionPerformed(ActionEvent e)
    398. {
    399. // SoundManager.play(1);
    400. }
    401. } // ExitMenuItem
    402. // Konstruktor
    403. private FileMenu()
    404. {
    405. initComponents();
    406. } // FileMenu
    407. private void initComponents()
    408. {
    409. LoadMenuItem = new LoadMenuItem();
    410. SaveMenuItem = new SaveMenuItem();
    411. SaveAsMenuItem = new SaveAsMenuItem();
    412. ExitMenuItem = new ExitMenuItem();
    413. //======== this ========
    414. setText("File");
    415. setName("FileMenu");
    416. add(LoadMenuItem);
    417. addSeparator();
    418. add(SaveMenuItem);
    419. add(SaveAsMenuItem);
    420. addSeparator();
    421. add(ExitMenuItem);
    422. }
    423. } // FileMenu
    424. /* ----------------------- GameMenu ------------------ */
    425. private class GameMenu extends JMenu
    426. {
    427. private NewMenuItem NewMenuItem;
    428. private StepForwardMenuItem StepForwardMenuItem;
    429. private StepBackwardMenuItem StepBackwardMenuItem;
    430. private GotoMenuItem GotoMenuItem;
    431. private class NewMenuItem extends JMenuItem
    432. {
    433. private NewMenuItem()
    434. {
    435. initComponents();
    436. }
    437. private void initComponents()
    438. {
    439. setText("New");
    440. setIcon(null);
    441. setName("NewMenuItem");
    442. }
    443. } // NewMenuItem
    444. private class StepForwardMenuItem extends JMenuItem implements ActionListener
    445. {
    446. private StepForwardMenuItem()
    447. {
    448. initComponents();
    449. }
    450. private void initComponents()
    451. {
    452. setText("Step Forward");
    453. setIcon(null);
    454. setName("StepForwardMenuItem");
    455. addActionListener(this);
    456. }
    457. public void actionPerformed (ActionEvent e)
    458. {
    459. }
    460. } // StepForwardMenuItem
    461. private class StepBackwardMenuItem extends JMenuItem implements ActionListener
    462. {
    463. private StepBackwardMenuItem()
    464. {
    465. initComponents();
    466. }
    467. private void initComponents()
    468. {
    469. setText("Step Backward");
    470. setIcon(null);
    471. setName("StepBackwardMenuItem");
    472. addActionListener(this);
    473. }
    474. public void actionPerformed(ActionEvent e)
    475. {
    476. }
    477. } // StepBackwardMenuItem
    478. private class GotoMenuItem extends JMenuItem implements ActionListener
    479. {
    480. private GotoMenuItem()
    481. {
    482. initComponents();
    483. }
    484. private void initComponents()
    485. {
    486. setText("Goto ...");
    487. setIcon(null);
    488. setName("GotoMenuItem");
    489. addActionListener(this);
    490. }
    491. public void actionPerformed(ActionEvent e)
    492. {
    493. }
    494. } // GotoMenuItem
    495. // Konstruktor
    496. private GameMenu()
    497. {
    498. initComponents();
    499. } // GameMenu
    500. private void initComponents()
    501. {
    502. NewMenuItem = new NewMenuItem();
    503. StepForwardMenuItem = new StepForwardMenuItem();
    504. StepBackwardMenuItem = new StepBackwardMenuItem();
    505. GotoMenuItem = new GotoMenuItem();
    506. //======== this ========
    507. setText("Game");
    508. setName("GameMenu");
    509. add(NewMenuItem);
    510. addSeparator();
    511. add(StepForwardMenuItem);
    512. add(StepBackwardMenuItem);
    513. addSeparator();
    514. add(GotoMenuItem);
    515. }
    516. } // GameMenu
    517. /* ----------------------- Player1Menu ---------------- */
    518. public class Player1Menu extends JMenu
    519. {
    520. private Player1UserMenuItem Player1UserMenuItem;
    521. private Player1ComputerMenuItem Player1ComputerMenuItem;
    522. private Player1NetworkMenuItem Player1NetworkMenuItem;
    523. private Player1PlaysFirstMenuItem Player1PlaysFirstMenuItem;
    524. private Player1PlaysSecondMenuItem Player1PlaysSecondMenuItem;
    525. private ButtonGroup Player1PlayerButtonGroup;
    526. private ButtonGroup Player1SequenceButtonGroup;
    527. // Konstruktor
    528. public Player1Menu()
    529. {
    530. initComponents();
    531. }
    532. private void initComponents()
    533. {
    534. Player1UserMenuItem = new Player1UserMenuItem();
    535. Player1ComputerMenuItem = new Player1ComputerMenuItem();
    536. Player1NetworkMenuItem = new Player1NetworkMenuItem();
    537. Player1PlaysFirstMenuItem = new Player1PlaysFirstMenuItem(false);
    538. Player1PlaysSecondMenuItem = new Player1PlaysSecondMenuItem(true);
    539. //======== this ========
    540. setText("Player 1");
    541. setName("Player1Menu");
    542. add(Player1UserMenuItem);
    543. add(Player1ComputerMenuItem);
    544. add(Player1NetworkMenuItem);
    545. addSeparator();
    546. add(Player1PlaysFirstMenuItem);
    547. add(Player1PlaysSecondMenuItem);
    548. //---- Player1PlayerButtonGroup ----
    549. Player1PlayerButtonGroup = new ButtonGroup();
    550. Player1PlayerButtonGroup.add(Player1UserMenuItem);
    551. Player1PlayerButtonGroup.add(Player1ComputerMenuItem);
    552. Player1PlayerButtonGroup.add(Player1NetworkMenuItem);
    553. //---- Player1SequenceButtonGroup ----
    554. Player1SequenceButtonGroup = new ButtonGroup();
    555. Player1SequenceButtonGroup.add(Player1PlaysFirstMenuItem);
    556. Player1SequenceButtonGroup.add(Player1PlaysSecondMenuItem);
    557. } // initComponents
    558. private class Player1UserMenuItem extends JRadioButtonMenuItem implements ActionListener
    559. {
    560. private Player1UserMenuItem()
    561. {
    562. initComponents();
    563. }
    564. private void initComponents()
    565. {
    566. setText("User");
    567. setSelected(true);
    568. setName("Player1UserMenuItem");
    569. addActionListener(this);
    570. }
    571. // Both Players are of Type User
    572. // This is not allowed
    573. public void actionPerformed(ActionEvent e)
    574. {
    575. playJRadioButton ();
    576. if (MenuBar.Player2Menu.Player2UserMenuItem.isSelected())
    577. {
    578. System.out.println("Player1 and Player2 selected as User");
    579. }
    580. }
    581. } // Player1User
    582. private class Player1ComputerMenuItem extends JRadioButtonMenuItem implements ActionListener
    583. {
    584. private Player1ComputerMenuItem()
    585. {
    586. initComponents();
    587. }
    588. private void initComponents()
    589. {
    590. setText("Computer");
    591. setName("Player1ComputerMenuItem");
    592. addActionListener(this);
    593. }
    594. public void actionPerformed(ActionEvent e)
    595. {
    596. playJRadioButton ();
    597. }
    598. } // Player1Computer
    599. private class Player1NetworkMenuItem extends JRadioButtonMenuItem implements ActionListener
    600. {
    601. private Player1NetworkMenuItem()
    602. {
    603. initComponents();
    604. }
    605. private void initComponents()
    606. {
    607. setText("Network");
    608. setName("Player1NetworkMenuItem");
    609. addActionListener(this);
    610. }
    611. // Both Players are of Type Network
    612. // This is not allowed
    613. public void actionPerformed(ActionEvent e)
    614. {
    615. playJRadioButton ();
    616. if (MenuBar.Player2Menu.Player2NetworkMenuItem.isSelected())
    617. {
    618. System.out.println("Player1 and Player2 selected as Network");
    619. }
    620. }
    621. } // Player1Network
    622. private class Player1PlaysFirstMenuItem extends JRadioButtonMenuItem implements ActionListener
    623. {
    624. private Player1PlaysFirstMenuItem(Boolean Selected)
    625. {
    626. initComponents(Selected);
    627. }
    628. private void initComponents(Boolean Selected)
    629. {
    630. setText("Play´s First");
    631. setSelected(Selected);
    632. setName("Player1PlaysFirstMenuItem");
    633. addActionListener (this);
    634. }
    635. // ----------------- public -----------------
    636. public void setAsSelected (Boolean Selected)
    637. {
    638. setSelected(Selected);
    639. }
    640. public void actionPerformed(ActionEvent e)
    641. {
    642. playJRadioButton ();
    643. MenuBar.Player2Menu.Player2PlaysSecondMenuItem.setAsSelected (true);
    644. }
    645. } // Player1PlaysFirstMenuItem
    646. private class Player1PlaysSecondMenuItem extends JRadioButtonMenuItem implements ActionListener
    647. {
    648. /* -------------- Konstruktor -------------------------- */
    649. private Player1PlaysSecondMenuItem(Boolean Selected)
    650. {
    651. initComponents(Selected);
    652. }
    653. private void initComponents(Boolean Selected)
    654. {
    655. setText("Play´s Second");
    656. setSelected(Selected);
    657. setName("Player1PlaysSecondMenuItem");
    658. addActionListener (this);
    659. }
    660. // ---------------------- public --------------------------------
    661. public void setAsSelected (Boolean Selected)
    662. {
    663. setSelected(Selected);
    664. }
    665. public void actionPerformed(ActionEvent e)
    666. {
    667. playJRadioButton ();
    668. MenuBar.Player2Menu.Player2PlaysFirstMenuItem.setAsSelected(true);
    669. }
    670. } // Player1PlaysSecondMenuItem
    671. // ------------------------- public ----------------------------------
    672. public ButtonModel getSelectionPlayerButtonGroup ()
    673. {
    674. return (Player1PlayerButtonGroup.getSelection());
    675. }
    676. public ButtonModel getSelectionSequenceButtonGroup ()
    677. {
    678. return (Player1SequenceButtonGroup.getSelection());
    679. }
    680. } // Player1Menu
    681. /* ---------------------- Player2Menu ------------------- */
    682. public class Player2Menu extends JMenu
    683. {
    684. private Player2UserMenuItem Player2UserMenuItem;
    685. private Player2ComputerMenuItem Player2ComputerMenuItem;
    686. private Player2NetworkMenuItem Player2NetworkMenuItem;
    687. private Player2PlaysFirstMenuItem Player2PlaysFirstMenuItem;
    688. private Player2PlaysSecondMenuItem Player2PlaysSecondMenuItem;
    689. private ButtonGroup Player2PlayerButtonGroup;
    690. private ButtonGroup Player2SequenceButtonGroup;
    691. private Player2Menu()
    692. {
    693. initComponents();
    694. }
    695. private void initComponents()
    696. {
    697. Player2UserMenuItem = new Player2UserMenuItem();
    698. Player2ComputerMenuItem = new Player2ComputerMenuItem();
    699. Player2NetworkMenuItem = new Player2NetworkMenuItem();
    700. Player2PlaysFirstMenuItem =new Player2PlaysFirstMenuItem(true);
    701. Player2PlaysSecondMenuItem =new Player2PlaysSecondMenuItem(false);
    702. //======== this ========
    703. setText("Player 2");
    704. setName("Player2Menu");
    705. add(Player2UserMenuItem);
    706. add(Player2ComputerMenuItem);
    707. add(Player2NetworkMenuItem);
    708. addSeparator();
    709. add(Player2PlaysFirstMenuItem);
    710. add(Player2PlaysSecondMenuItem);
    711. //---- Player2PlayerButtonGroup ----
    712. Player2PlayerButtonGroup = new ButtonGroup();
    713. Player2PlayerButtonGroup.add(Player2UserMenuItem);
    714. Player2PlayerButtonGroup.add(Player2ComputerMenuItem);
    715. Player2PlayerButtonGroup.add(Player2NetworkMenuItem);
    716. //---- Player2SequenceButtonGroup ----
    717. Player2SequenceButtonGroup = new ButtonGroup();
    718. Player2SequenceButtonGroup.add(Player2PlaysFirstMenuItem);
    719. Player2SequenceButtonGroup.add(Player2PlaysSecondMenuItem);
    720. }
    721. private class Player2UserMenuItem extends JRadioButtonMenuItem implements ActionListener
    722. {
    723. private Player2UserMenuItem()
    724. {
    725. initComponents();
    726. }
    727. private void initComponents()
    728. {
    729. setText("User");
    730. setName("Player2UserMenuItem");
    731. addActionListener (this);
    732. }
    733. // Both Players are of Type User
    734. // This is not allowed
    735. public void actionPerformed(ActionEvent e)
    736. {
    737. playJRadioButton ();
    738. if (MenuBar.Player1Menu.Player1UserMenuItem.isSelected())
    739. {
    740. System.out.println("Player1 and Player2 selected as User");
    741. }
    742. }
    743. }// Player2User
    744. private class Player2ComputerMenuItem extends JRadioButtonMenuItem implements ActionListener
    745. {
    746. private Player2ComputerMenuItem()
    747. {
    748. initComponents();
    749. }
    750. private void initComponents()
    751. {
    752. setText("Computer");
    753. setSelected(true);
    754. setName("Player2ComputerMenuItem");
    755. addActionListener (this);
    756. }
    757. public void actionPerformed(ActionEvent e)
    758. {
    759. playJRadioButton ();
    760. }
    761. }// Player2Computer
    762. private class Player2NetworkMenuItem extends JRadioButtonMenuItem implements ActionListener
    763. {
    764. private Player2NetworkMenuItem()
    765. {
    766. initComponents();
    767. }
    768. private void initComponents()
    769. {
    770. setText("Network");
    771. setName("Player2NetworkMenuItem");
    772. addActionListener (this);
    773. }
    774. // Both Players are of Type Network
    775. // This is not allowed
    776. public void actionPerformed(ActionEvent e)
    777. {
    778. playJRadioButton ();
    779. if (MenuBar.Player1Menu.Player1NetworkMenuItem.isSelected())
    780. {
    781. System.out.println("Player1 and Player2 selected as Network");
    782. }
    783. }
    784. }// Player2Network
    785. private class Player2PlaysFirstMenuItem extends JRadioButtonMenuItem implements ActionListener
    786. {
    787. private Player2PlaysFirstMenuItem(Boolean Selected)
    788. {
    789. initComponents(Selected);
    790. }
    791. private void initComponents(Boolean Selected)
    792. {
    793. setText("Play´s First");
    794. setSelected(Selected);
    795. setName("Player2PlaysFirstMenuItem");
    796. addActionListener (this);
    797. }
    798. // -------------------- public --------------------------
    799. public void setAsSelected (Boolean Selected)
    800. {
    801. setSelected(Selected);
    802. }
    803. public void actionPerformed(ActionEvent e)
    804. {
    805. playJRadioButton ();
    806. MenuBar.Player1Menu.Player1PlaysSecondMenuItem.setAsSelected(true);
    807. }
    808. } // Player2PlaysFirstMenuItem
    809. private class Player2PlaysSecondMenuItem extends JRadioButtonMenuItem implements ActionListener
    810. {
    811. /* -------------- Konstruktor -------------------------- */
    812. private Player2PlaysSecondMenuItem(Boolean Selected)
    813. {
    814. initComponents(Selected);
    815. }
    816. private void initComponents(Boolean Selected)
    817. {
    818. setText("Play´s Second");
    819. setSelected(Selected);
    820. setName("Player2PlaysSecondMenuItem");
    821. addActionListener (this);
    822. }
    823. public void setAsSelected (Boolean Selected)
    824. {
    825. setSelected(Selected);
    826. }
    827. public void actionPerformed(ActionEvent e)
    828. {
    829. playJRadioButton ();
    830. MenuBar.Player1Menu.Player1PlaysFirstMenuItem.setAsSelected(true);
    831. }
    832. } // Player2PlaysSecondMenuItem
    833. // ------------------------------ public -----------------------
    834. public ButtonModel getSelectionPlayerButtonGroup ()
    835. {
    836. return (Player2PlayerButtonGroup.getSelection());
    837. }
    838. public ButtonModel getSelectionSequenceButtonGroup ()
    839. {
    840. return (Player2SequenceButtonGroup.getSelection());
    841. }
    842. } // Player2Menu
    843. /* --------------------- HelpMenu ------------------------- */
    844. private class HelpMenu extends JMenu
    845. {
    846. private HelpMenu()
    847. {
    848. initComponents();
    849. }
    850. private void initComponents()
    851. {
    852. HelpContentsMeniItem = new HelpContentsMenuItem();
    853. HelpAboutMenuItem = new HelpAboutMenuItem();
    854. //======== this ========
    855. setText("Help");
    856. setName("HelpMenu");
    857. add(HelpContentsMeniItem);
    858. add(HelpAboutMenuItem);
    859. }
    860. private HelpContentsMenuItem HelpContentsMeniItem;
    861. private HelpAboutMenuItem HelpAboutMenuItem;
    862. private class HelpContentsMenuItem extends JMenuItem
    863. {
    864. private HelpContentsMenuItem()
    865. {
    866. initComponents();
    867. }
    868. private void initComponents()
    869. {
    870. setText("Help Contents");
    871. setName("HelpContentsMeniItem");
    872. }
    873. } // HelpContentsMenuItem
    874. private class HelpAboutMenuItem extends JMenuItem
    875. {
    876. private HelpAboutMenuItem()
    877. {
    878. initComponents();
    879. }
    880. private void initComponents()
    881. {
    882. setText("About ...");
    883. setName("HelpAboutMenuItem");
    884. }
    885. private void HelpAboutMenuItemActionPerformed(ActionEvent e)
    886. {
    887. // HelpAboutOptionPane = new HelpAboutOptionPane (super);
    888. HelpAboutOptionPane.showDialog (this);
    889. }
    890. } // HelpAboutMenuItem
    891. } // HelpMenu
    892. /* ---------------------- OptionsMenu -------------------- */
    893. private class OptionsMenu extends JMenu
    894. {
    895. private OptionsMenu()
    896. {
    897. initComponents();
    898. }
    899. private void initComponents()
    900. {
    901. ShowGridMenuItem = new ShowGridMenuItem();
    902. ShowMovesMenuItem = new ShowMovesMenuItem();
    903. ShowStableFieldsMenuItem = new ShowStableFieldsMenuItem();
    904. //======== this ========
    905. setText("Options");
    906. setName("OptionsMenu");
    907. add(ShowGridMenuItem);
    908. add(ShowMovesMenuItem);
    909. add(ShowStableFieldsMenuItem);
    910. }
    911. private ShowGridMenuItem ShowGridMenuItem;
    912. private ShowMovesMenuItem ShowMovesMenuItem;
    913. private ShowStableFieldsMenuItem ShowStableFieldsMenuItem;
    914. // ------------------- public ------------------------------
    915. public ShowGridMenuItem getShowGridMenuItem()
    916. {
    917. return ShowGridMenuItem;
    918. }
    919. // -----------------------------------------------------------
    920. private class ShowGridMenuItem extends JCheckBoxMenuItem implements ActionListener
    921. {
    922. private boolean selected;
    923. public ShowGridMenuItem getShowGridMenuItem()
    924. {
    925. return ShowGridMenuItem;
    926. }
    927. private ShowGridMenuItem()
    928. {
    929. initComponents();
    930. }
    931. private void initComponents()
    932. {
    933. setText("Show Grid");
    934. setName("ShowGridMenuItem");
    935. setSelected(true);
    936. selected = true;
    937. addActionListener(this);
    938. }
    939. public void actionPerformed(ActionEvent e)
    940. {
    941. playJCheckBox ();
    942. if (selected == false)
    943. {
    944. // Nur wenn vorher nicht selektiert
    945. if (MenuBar.OptionsMenu.ShowGridMenuItem.isSelected())
    946. {
    947. // Zeichnen des Rasters, Neuzeichnen des Boards
    948. System.out.println("Grid zeichnen");
    949. BoardPanel.drawGrid (getGraphics());
    950. selected = true;
    951. }
    952. }
    953. if (selected == true)
    954. {
    955. // Nur wenn vorher selektiert
    956. if (!MenuBar.OptionsMenu.ShowGridMenuItem.isSelected())
    957. {
    958. // Löschen des Rasters, Neuzeichnen des Boards
    959. System.out.println("Grid löschen");
    960. BoardPanel.eraseGrid (getGraphics());
    961. selected = false;
    962. }
    963. }
    964. }
    965. } // ShowGridMenuItem
    966. private class ShowMovesMenuItem extends JCheckBoxMenuItem implements ActionListener
    967. {
    968. private ShowMovesMenuItem()
    969. {
    970. initComponents();
    971. }
    972. private void initComponents()
    973. {
    974. setText("Show Moves");
    975. setName("ShowMovesMenuItem");
    976. addActionListener(this);
    977. }
    978. public void actionPerformed(ActionEvent e)
    979. {
    980. playJCheckBox ();
    981. }
    982. } // ShowMovesMenuItem
    983. private class ShowStableFieldsMenuItem extends JCheckBoxMenuItem implements ActionListener
    984. {
    985. private ShowStableFieldsMenuItem()
    986. {
    987. initComponents();
    988. }
    989. private void initComponents()
    990. {
    991. setText("Show Stable Fields");
    992. setName("ShowStableFieldsMenuItem");
    993. addActionListener(this);
    994. }
    995. public void actionPerformed(ActionEvent e)
    996. {
    997. playJCheckBox ();
    998. }
    999. } // ShowStableFieldsMenuItem
    1000. } // OptionsMenu
    1001. /* --------------------- SettingsMenu ------------------ */
    1002. private class SettingsMenu extends JMenu
    1003. {
    1004. private SettingsMenu()
    1005. {
    1006. initComponents();
    1007. }
    1008. private void initComponents()
    1009. {
    1010. SettingsMenuBoardMenuItem = new SettingsMenuBoardMenuItem();
    1011. SettingsMenuEngineMenuItem = new SettingsMenuEngineMenuItem();
    1012. SettingsMenuNetworkMenuItem = new SettingsMenuNetworkMenuItem();
    1013. //======== this ========
    1014. setText("Settings");
    1015. setName("SettingsMenu");
    1016. add(SettingsMenuBoardMenuItem);
    1017. add(SettingsMenuEngineMenuItem);
    1018. add(SettingsMenuNetworkMenuItem);
    1019. }
    1020. private SettingsMenuBoardMenuItem SettingsMenuBoardMenuItem;
    1021. private SettingsMenuEngineMenuItem SettingsMenuEngineMenuItem;
    1022. private SettingsMenuNetworkMenuItem SettingsMenuNetworkMenuItem;
    1023. private class SettingsMenuBoardMenuItem extends JMenuItem implements ActionListener
    1024. {
    1025. private SettingsMenuBoardMenuItem()
    1026. {
    1027. initComponents();
    1028. }
    1029. private void initComponents()
    1030. {
    1031. setText("Board ...");
    1032. setName("SettingsMenuBoardMenuItem");
    1033. addActionListener (this);
    1034. }
    1035. // Must be public
    1036. public void actionPerformed(ActionEvent e)
    1037. {
    1038. JDialog Dialog = new BoardSettingsDialog(thisFrame);
    1039. Dialog.setVisible(true);
    1040. // Erzeugt ein neues JInternalFrame
    1041. /*
    1042. System.out.println("New JInternalFrame");
    1043. SimpleInternalFrame newFrame = new BoardSettingsInternalFrame();
    1044. OthelloFrameContentPane.add(newFrame);
    1045. newFrame.setVisible (true);
    1046. */
    1047. }
    1048. } // SettingsMenuBoardMenuItem
    1049. private class SettingsMenuEngineMenuItem extends JMenuItem
    1050. {
    1051. private SettingsMenuEngineMenuItem()
    1052. {
    1053. initComponents();
    1054. }
    1055. private void initComponents()
    1056. {
    1057. setText("Engine ...");
    1058. setName("SettingsMenuEngineMenuItem");
    1059. }
    1060. private void SettingsMenuEngineMenuItemActionPerformed(ActionEvent e)
    1061. {
    1062. // TODO add your code here
    1063. }
    1064. } // SettingsMenuEngineMenuItem
    1065. private class SettingsMenuNetworkMenuItem extends JMenuItem
    1066. {
    1067. private SettingsMenuNetworkMenuItem()
    1068. {
    1069. initComponents();
    1070. }
    1071. private void initComponents()
    1072. {
    1073. setText("Network ,,,");
    1074. setName("SettingsMenuNetworkMenuItem");
    1075. }
    1076. private void SettingsMenuNetworkMenuItemActionPerformed(ActionEvent e)
    1077. {
    1078. // TODO add your code here
    1079. }
    1080. } // SettingMenuNetworkMenuItem
    1081. } // SettingsMenu
    1082. /* --------------------- MenuBar ------------------------ */
    1083. private class MenuBar extends JMenuBar
    1084. {
    1085. private FileMenu FileMenu;
    1086. private GameMenu GameMenu;
    1087. private Player1Menu Player1Menu;
    1088. private Player2Menu Player2Menu;
    1089. private SettingsMenu SettingsMenu;
    1090. private OptionsMenu OptionsMenu;
    1091. private HelpMenu HelpMenu;
    1092. // ----------------- public ------------------------
    1093. public JMenu getOptionsMenu()
    1094. {
    1095. return OptionsMenu;
    1096. }
    1097. public JMenu getHelpMenu()
    1098. {
    1099. return HelpMenu;
    1100. }
    1101. // -------------------------------------------------
    1102. private MenuBar()
    1103. {
    1104. initComponents();
    1105. }
    1106. private void initComponents()
    1107. {
    1108. FileMenu = new FileMenu();
    1109. GameMenu = new GameMenu();
    1110. Player1Menu = new Player1Menu();
    1111. Player2Menu = new Player2Menu();
    1112. SettingsMenu = new SettingsMenu();
    1113. OptionsMenu = new OptionsMenu();
    1114. HelpMenu = new HelpMenu();
    1115. //======== this ========
    1116. setName("MenuBar");
    1117. add(FileMenu);
    1118. add(GameMenu);
    1119. add(Player1Menu);
    1120. add(Player2Menu);
    1121. add(SettingsMenu);
    1122. add(OptionsMenu);
    1123. add(HelpMenu);
    1124. }
    1125. } // MenuBar
    1126. } // OthelloFrame
    Alles anzeigen


    Hier jetzt das Board als Erweiterung von JPanel. Das Setzen, Wegnehmen eines Steines etc. wird in GameBoard ausgeführt. Diese Klasse verwaltet das Array. OthelloBoard reagiert mittels MouseAdapter auf einen Mouseklick. Dieses führt (Ist ein Test) unmittelbar zum Setzen eines Spielsteines mittels des Aufrufes von setStone in GameBoard. Nun soll das JPanel bzw. das Board mittels repaint() neu gezeichnet werden. Dummerweise zeichnet nun das Programm den Inhalt des JFrames im JPanel.
    Die Frage ist warum ? repaint() wird ja im JPanel aufgerufen.
    Der Code ist nicht vollständig und nicht ausführbar.

    Quellcode

    1. package classes;
    2. import java.awt.*;
    3. import java.awt.event.*;
    4. import java.awt.image.*;
    5. import java.awt.geom.*;
    6. import java.awt.Window.*;
    7. import java.awt.Component.*;
    8. import java.applet.*;
    9. import java.net.*;
    10. import java.io.*;
    11. import javax.imageio.*;
    12. import java.lang.String;
    13. import java.lang.Enum;
    14. import java.net.URL;
    15. import javax.swing.*;
    16. import javax.swing.border.*;
    17. import java.lang.Object;
    18. import java.lang.reflect.*;
    19. // import classes.OthelloFrame;
    20. // import classes.OthelloPlayer;
    21. import interfaces.ConstInterface;
    22. import classes.GameBoard;
    23. public class OthelloBoard extends JPanel implements ConstInterface
    24. {
    25. String Stil = "Plastic";
    26. String AbsoluteGroesse = "60";
    27. String RelativeGroesse = "70%";
    28. String Aussehen = "Sphere";
    29. String ImagePfad = "d:/Programmieren/Java/JAVA IDE´s/JCreator V 3.5/MyProjects/Othello/images";
    30. String GesamterPfad = ImagePfad+Aussehen+AbsoluteGroesse+"x"+AbsoluteGroesse;
    31. String FilePlasticGelb = GesamterPfad+"/PlasticGelb60x60_70%.jpg";
    32. String FilePlasticBlau = GesamterPfad+"/PlasticBlau60x60_70%.jpg";
    33. String FilePlasticWeiss = GesamterPfad+"/PlasticWeiss60x60_70%.jpg";
    34. String FilePlasticSchwarz = GesamterPfad+"/PlasticSchwarz60x60_70%.jpg";
    35. private Image PlasticBlau;
    36. private Image PlasticWeiss;
    37. private Image PlasticSchwarz;
    38. private Image PlasticRot;
    39. private Image PlasticGelb;
    40. private OthelloBoard thisBoard;
    41. private OthelloFrame thisFrame;
    42. private MouseAdapter MyMouseAdapter;
    43. private SoftBevelBorder SoftBorder;
    44. private GameBoard Board;
    45. private Graphics g4711;
    46. public Image BitmapComponent(String fname)
    47. {
    48. Image img;
    49. img = getToolkit().getImage(fname);
    50. MediaTracker mt = new MediaTracker(this);
    51. mt.addImage(img, 0);
    52. try
    53. {
    54. //Warten, bis das Image vollständig geladen ist,
    55. //damit getWidth() und getHeight() funktionieren
    56. mt.waitForAll();
    57. } catch (InterruptedException e)
    58. {
    59. //nothing
    60. }
    61. /*
    62. int width = img.getWidth(null);
    63. int height = img.getHeight(null);
    64. BufferedImage bimg = new BufferedImage(width,height,
    65. BufferedImage.TYPE_INT_RGB);
    66. */
    67. return (img);
    68. // return (bimg);
    69. } // BitmapComponent
    70. // OthelloFrame ist von JFrame abgeleitet
    71. public OthelloBoard ()
    72. {
    73. System.out.println("Konstruktor OthelloBoard");
    74. initComponents ();
    75. addMouseListener (MyMouseAdapter);
    76. }
    77. private void initComponents ()
    78. {
    79. System.out.println("initComponents in OthelloBoard");
    80. // Initialisierung der Image-Dateien
    81. PlasticGelb = BitmapComponent (FilePlasticGelb);
    82. PlasticBlau = BitmapComponent (FilePlasticBlau);
    83. PlasticWeiss = BitmapComponent (FilePlasticWeiss);
    84. PlasticSchwarz = BitmapComponent (FilePlasticSchwarz);
    85. MyMouseAdapter = new MyMouseAdapter();
    86. SoftBorder = new SoftBevelBorder(SoftBevelBorder.LOWERED);
    87. System.out.println("new GameBoard");
    88. Board = new GameBoard(8);
    89. }// initComponents
    90. private class MyMouseAdapter extends MouseAdapter
    91. {
    92. public void mouseClicked (MouseEvent e)
    93. {
    94. }
    95. public void mousePressed (MouseEvent e)
    96. {
    97. int x = e.getX();
    98. int y = e.getY();
    99. int column = x / (80);
    100. int row = y / (80);
    101. System.out.println("mousePressed at OthelloBoard");
    102. // Setze einen Stein
    103. // Grosser Mist
    104. // Anscheinen wird ein neuer GraphicContext erzeugt
    105. Board.setColor (row, column, EnumColor.WHITE);
    106. repaint();
    107. }
    108. // Hier nicht erforderlich, da Adapterklasse !
    109. public void mouseReleased (MouseEvent e)
    110. {
    111. }
    112. public void mouseEntered (MouseEvent e)
    113. {
    114. }
    115. public void mouseExited (MouseEvent e)
    116. {
    117. }
    118. } // MyMouseAdapter
    119. public void drawGrid (Graphics g)
    120. {
    121. Graphics2D g2d = (Graphics2D)g;
    122. g2d.setStroke(new BasicStroke(1.5f));
    123. int x = 10;
    124. int y = 10;
    125. int BoardSize = 480;
    126. int FieldSize = 480/8;
    127. g.setColor(Color.black);
    128. for (int i = 0; i <= 8; i++)
    129. {
    130. g.drawLine (x, y+i*FieldSize, x+BoardSize, y+i*FieldSize);
    131. g.drawLine (x+i*FieldSize, y, x+i*FieldSize, y+BoardSize);
    132. }
    133. }
    134. public void eraseGrid (Graphics g)
    135. {
    136. Graphics2D g2d = (Graphics2D)g;
    137. g2d.setStroke(new BasicStroke(1.5f));
    138. int x = 10;
    139. int y = 10;
    140. int BoardSize = 480;
    141. int FieldSize = 480/8;
    142. g.setColor(Color.white);
    143. for (int i = 0; i <= 8; i++)
    144. {
    145. g.drawLine (x, y+i*FieldSize, x+BoardSize, y+i*FieldSize);
    146. g.drawLine (x+i*FieldSize, y, x+i*FieldSize, y+BoardSize);
    147. }
    148. }
    149. public void paint (Graphics g)
    150. {
    151. int row, column;
    152. System.out.println("paint in OthelloBoard");
    153. // Arrayindex von 0..n
    154. for (row = 0; row <= 7; row++)
    155. {
    156. for (column = 0; column <= 7; column++)
    157. {
    158. // Fragt das Board
    159. ConstInterface.EnumColor a = Board.getColor (row,column);
    160. if (a == ConstInterface.EnumColor.WHITE)
    161. {
    162. g.drawImage (PlasticWeiss,10+row*60, 10+column*60, this);
    163. }
    164. if (a == ConstInterface.EnumColor.BLACK)
    165. {
    166. g.drawImage (PlasticSchwarz,10+row*60, 10+column*60, this);
    167. }
    168. }
    169. }
    170. } // paint
    171. // ------------------ Get und Set-Methoden -----------------
    172. public EnumColor getColor (int row, int column)
    173. {
    174. return (Board.getColor(row,column));
    175. }
    176. public void setColor (int row, int column, EnumColor color)
    177. {
    178. Board.setColor (row,column,color);
    179. }
    180. public void changeColor (int row, int column)
    181. {
    182. Board.changeColor (row,column);
    183. }
    184. }
    Alles anzeigen