
Signature
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology
>>That's the gist of Andrew's
>>SSCCE (if I got the acronym right).
[quoted text clipped - 14 lines]
>
> [ But the shorter the better.. ]
OK, here's the code (4 classes, error is with the 1st and 2nd). I hope
I'm not trying to do something impossible.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics.*;
import java.awt.Color.*;
public class Spel extends JFrame implements ActionListener
{
boolean gestart;
boolean beeindigd;
int kleur;
private JMenuItem jmiExit, jmiNew, jmiSettingsHelpSubMenu,
jmiColourHelpSubMenu, jmiColour, jmiColourForeground, jmiBlue;
private JRadioButtonMenuItem jrbmiBlue, jrbmiRed, jrbmiGreen;
char speler;
private char beurt = 'X';
private JButton[] knoppen = new JButton[10];
private JPanel paneel = new JPanel(); // maak paneel om het menu en
de knoppen in te plaatsen
private JPanel paneel2 = new JPanel();
Container container = getContentPane(); // get het contentpane van
het frame
JMenuBar jmb = new JMenuBar(); // maak een menu aan
Speelveld speelveld = new Speelveld();
int vak[][] = new int[3][3];
/** Creates a new instance of Spel */
public Spel()
{
container.setLayout(new BorderLayout()); // set de borderlayout
voor het frame
paneel.setLayout(new GridLayout(3,3)); // definieer een
GridLayout van 3 bij 3
for (int i = 1; i <= 9; i++) // voeg de vakjes toe aan paneel
knoppen[i] = new JButton("");
for (int i = 1; i <= 9; i++) // voeg de vakjes toe aan paneel
paneel.add(knoppen[i]);
paneel2.setLayout(new BorderLayout()); // hierin komen knoppen
en het menu
JMenu fileMenu = new JMenu("File"); // maak menu-item File aan
JMenu settingsMenu = new JMenu("Settings"); // maak menu-item
Settings aam
jmb.add(fileMenu); // voeg File em Settings toe aan het Menu
jmb.add(settingsMenu);
fileMenu.add(jmiNew = new JMenuItem("New")); // voeg Filemenu
items toe
fileMenu.addSeparator();
fileMenu.add(jmiExit = new JMenuItem("Exit"));
fileMenu.setMnemonic('F'); // voeg Filemenu mnemonics toe
jmiNew.setMnemonic('N');
jmiExit.setMnemonic('x');
JMenu colourHelpSubMenu = new JMenu("Colour"); // voeg
Settingsmenu items toe
settingsMenu.add(jmiColour = colourHelpSubMenu);
colourHelpSubMenu.add(jrbmiBlue = new
JRadioButtonMenuItem("Blue")); // voeg kleur radiobuttons toe
colourHelpSubMenu.add(jrbmiRed = new JRadioButtonMenuItem("Red"));
colourHelpSubMenu.add(jrbmiGreen = new
JRadioButtonMenuItem("Green"));
settingsMenu.setMnemonic('S'); // voeg Settingsmenu mnemonics toe
jmiColour.setMnemonic('C');
jrbmiBlue.setMnemonic('B');
jrbmiRed.setMnemonic('R');
jrbmiGreen.setMnemonic('G');
paneel2.add(jmb, BorderLayout.NORTH); // voeg het Menu toe aan
het borderlayout
paneel2.add(paneel, BorderLayout.CENTER);
container.add(paneel2, BorderLayout.CENTER); // voeg paneel toe
aan het frame
jmiExit.addActionListener(this); // registreer listeners
jrbmiBlue.addActionListener(this);
jrbmiRed.addActionListener(this);
jrbmiGreen.addActionListener(this);
ButtonGroup btg = new ButtonGroup(); // voeg buttongroup toe
btg.add(jrbmiBlue);
btg.add(jrbmiRed);
btg.add(jrbmiGreen);
knoppen[1].addActionListener(
new ButtonListener() {
public void actionPerformed(ActionEvent e)
{
knoppen[1] = new JButton("");
System.out.println("1");
// zet vak[0][0] op 'gevuld' met beurt;
speelveld.plaatsPion(vak[0][0]);
wisselSpeler();
System.out.println(beurt + " heeft de beurt");
}
}
);
knoppen[2].addActionListener(
new ButtonListener() {
public void actionPerformed(ActionEvent e)
{
knoppen[2] = new JButton("x");
System.out.println("2");
// zet vak[1][0] op 'gevuld' met beurt;
wisselSpeler();
System.out.println(beurt + " heeft de beurt");
}
}
);
knoppen[3].addActionListener(
new ButtonListener() {
public void actionPerformed(ActionEvent e)
{
knoppen[3] = new JButton("3");
System.out.println("3");
// zet vak[2[[0] op 'gevuld' met beurt;
wisselSpeler();
System.out.println(beurt + " heeft de beurt");
}
}
);
knoppen[4].addActionListener(
new ButtonListener() {
public void actionPerformed(ActionEvent e)
{
knoppen[4] = new JButton("4");
System.out.println("4");
// zet vak[0][1] op 'gevuld' met beurt;
wisselSpeler();
System.out.println(beurt + " heeft de beurt");
}
}
);
knoppen[5].addActionListener(
new ButtonListener() {
public void actionPerformed(ActionEvent e)
{
knoppen[5] = new JButton("x");
System.out.println("5");
// zet vak[1][1] op 'gevuld' met beurt;
wisselSpeler();
System.out.println(beurt + " heeft de beurt");
}
}
);
knoppen[6].addActionListener(
new ButtonListener() {
public void actionPerformed(ActionEvent e)
{
knoppen[6] = new JButton("x");
System.out.println("6");
// zet vak[2][1] op 'gevuld' met beurt;
wisselSpeler();
System.out.println(beurt + " heeft de beurt");
}
}
);
knoppen[7].addActionListener(
new ButtonListener() {
public void actionPerformed(ActionEvent e)
{
knoppen[7] = new JButton("x");
System.out.println("7");
// zet vak[0][2] op 'gevuld' met beurt;
wisselSpeler();
System.out.println(beurt + " heeft de beurt");
}
}
);
knoppen[8].addActionListener(
new ButtonListener() {
public void actionPerformed(ActionEvent e)
{
knoppen[8] = new JButton("x");
System.out.println("8");
// zet vak[1][2] op 'gevuld' met beurt;
wisselSpeler();
System.out.println(beurt + " heeft de beurt");
}
}
);
knoppen[9].addActionListener(
new ButtonListener() {
public void actionPerformed(ActionEvent e)
{
knoppen[9] = new JButton("x");
System.out.println("9");
// zet vak[2][2] op 'gevuld' met beurt;
wisselSpeler();
System.out.println(beurt + " heeft de beurt");
}
}
);
}
public void startSpel()
{
}
public void eindeSpel()
{
}
public void eindeProgramma()
{
System.exit(0);
}
public char wisselSpeler()
{
if (beurt == 'X')
{
beurt = 'O';
System.out.println("De beurt is gegaan naar " + beurt);
}
else if (beurt == 'O')
{
beurt = 'X';
System.out.println("De beurt is gegaan naar " + beurt);
}
System.out.println("Beurt = " + beurt);
return beurt;
}
public static void main(String[] args)
{
Spel spel = new Spel();
spel.setTitle("Boter, Kaas en Eieren");
spel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
spel.setSize(200, 200);
spel.setVisible(true);
}
class Vakjes extends JPanel
{
public void paintComponent(Graphics g)
{
if (speler == 'x')
g.drawOval(10, 10, getSize().width-20,
getSize().height-20);
}
}
public void actionPerformed(ActionEvent e)
{
String actionCommand = e.getActionCommand();
if (e.getSource() instanceof JMenuItem)
if ("Exit".equals(actionCommand))
{
eindeProgramma();
}
if (e.getSource() instanceof JRadioButtonMenuItem)
if ("Blue".equals(actionCommand))
{
paneel.setForeground(Color.blue);
System.out.println("Blue");
}
if ("Red".equals(actionCommand))
{
paneel.setForeground(Color.red);
System.out.println("Red");
}
if ("Green".equals(actionCommand))
{
paneel.setForeground(Color.green);
System.out.println("Green");
}
if (e.getSource() instanceof JButton)
System.out.println("Button clicked");
}
class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
}
}
}
public class Speelveld
{
int vakafmeting;
int getal;
String vakBezetDoor;
boolean speelveldVol;
boolean speelveldLeeg;
final static int max_aantal_horizontaal = 3;
final static int max_aantal_vertikaal = 3;
int aantalPionnenOpSpeelveld;
private int vak[][] = new int[3][3];
/** Creates a new instance of Speelveld */
public Speelveld()
{
}
public int controleerBezetting(int vak[][])
{
System.out.println(vak);
return getal;
}
public int plaatsPion(int vak[][])
{
System.out.println("vak["+"]["+"]");
return getal;
}
public void verwijderPion()
{
}
}
public class Speler
{
String naam;
String symbool;
/** Creates a new instance of Speler */
public Speler()
{
}
}
public class Pion
{
int vorm;
/** Creates a new instance of Pion */
public Pion()
{
}
}
Andrew Thompson - 31 May 2004 09:24 GMT
>> Short, Self contained, Complileable, Example.
>>
[quoted text clipped - 9 lines]
>>
>> [ But the shorter the better.. ]
( hinting at shorter than 173 lines.. )
> OK, here's the code (4 classes,
The example only requires two. If you can
pass a 2D arary between one class and another,
you can do it between 6, 10 or 50..
>..error is with the 1st and 2nd). I hope
> I'm not trying to do something impossible.
You are attempting to do something fairly
easy (from your description) but the
'penny has not dropped' yet. You will
kick yourself when it finally makes
sense, you will _then_ wonder how you
ever misunderstood it. ;-)
> import javax.swing.*;
...
> knoppen[9].addActionListener(
OK. I am gonna break in here.
Somebody else may wade through your code,
but I do not have the time to wade through
your 4 classes, spread over a 357 line post,
especially when the source needs to be broken
into separate files to compile, and includes
other compile errors in addition to that..
C:\Spel.java:19: ';' expected.
de knoppen in te plaatsen
^
C:\Spel.java:19: Duplicate variable declaration: de knoppen was JButton
knoppen[]
de knoppen in te plaatsen
^
C:\Spel.java:22: ';' expected.
het frame
^
C:\Spel.java:31: ';' expected.
voor het frame
^
C:\Spel.java:39: ';' expected.
en het menu
^
C:\Spel.java:274: Public class Speelveld must be defined in a file called
"Speelveld.java".
public class Speelveld
^
C:\Spel.java:308: Public class Speler must be defined in a file called
"Speler.java".
public class Speler
^
C:\Spel.java:320: Public class Pion must be defined in a file called
"Pion.java".
public class Pion
^
8 errors
....
Your SSCCE should be trimmed to under 100 lines.
You can make the 'public' classes 'default' so
that they compile in a single file.
Please have a _careful_ read of the SSCCE document
and you will note that you do not need a UI for this..
Especially not an UI with 9(!) buttons.
As I recall, I provided a self contained
example earlier.. Yeah.. there it is..
<http://groups.google.com/groups?th=7995d61159b0265c#link2>
less than 40 lines of code that can be copy/pasted/compiled/run
that demonstrate passing a 2D array between classes.
Also check Woebegone's even shorter example,
immediately below that post.
It is important to understand how you can
trim code down to the *simplest* code that
demonstrates a problem or solution.

Signature
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology
Huub - 31 May 2004 09:59 GMT
>>>Short, Self contained, Complileable, Example.
>>>
[quoted text clipped - 95 lines]
> trim code down to the *simplest* code that
> demonstrates a problem or solution.
Alright. I'll have a thorough look at it once again. Then see if I
should make another post or reply. 8 Errors....I really got only 1. But
maybe this 1 holds back the others as yet.
Thanks so far.