G
Gino Adomeit
Gast
Java:
import java.awt.*;
import java.awt.event.*;
class InputButton extends Button
{
// Attribute
protected ButtonPanel buttons;
protected int row, col;
protected boolean black;
public InputButton(int i,int j,ButtonPanel b)
{
buttons = b;
row = i;
col = j;
// Eventverarbeitung
setBackground(new Color(255,255,255));
}
public void changeColor()
{
if(getBackground().equals(new Color(0,0,0)))
setBackground(new Color(255,255,255));
else
setBackground(new Color(0,0,0));
}
public void reset()
{
setBackground(new Color(255,255,255));
}
}
class ButtonPanel extends Panel
{
protected int size;
protected InputButton[][] button;
public ButtonPanel(int n)
{
size = n;
setLayout(new GridLayout(n,n,2,2));
button = new InputButton[n][n];
for(int k=0;k<n;k++)
{
for(int l=0;l<n;l++)
{
button[k][l] = new InputButton(l,k,this);
button[k][l].addActionListener (new ActionListener() {
public void actionPerformed(ActionEvent e){
changeNeighborsOf(((InputButton)e.getSource()).col,
((InputButton)e.getSource()).row);
}
});
add(button[k][l]);
}
}
}
public void changeNeighborsOf(int x,int y)
{
for(int k=-1;k<2;k++)
{
for(int l=-1;l<2;l++)
{
if(x+k>=0 && x+k<size && y+l>=0 && y+l<size &&(k!=0 || l!=0))
{
button[x+k][y+l].changeColor();
}
}
}
}
public void reset()
{
for(int k=0;k<size;k++)
{
for(int l=0;l<size;l++)
{
button[k][l].reset();
}
}
}
}
public class Trisentis extends Frame
{
// Attribute:
protected ButtonPanel buttons; // Eingabefeld
protected Button resetButton; // Reset-Knopf
// Konstruktor:
public Trisentis(int size)
{
super("Trisentis");
// Bestandteile konstruieren:
buttons = new ButtonPanel(size);
resetButton = new Button("Reset");
add("Center", buttons);
add("South", resetButton);
resetButton.addActionListener (new ActionListener() {
public void actionPerformed(ActionEvent e) { buttons.reset(); }
});
this.addWindowListener
(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
System.exit(0);
}
});
// Spiel sichtbar machen:
setSize(400, 440);
show();
}
// Methode:
// Fuer den Aufruf von einer Kommandozeile:
public static void main(String[] args) {
if (args.length != 1)
{
System.out.println("Trisentis benoetigt genau ein int-Argument");
} else
{
int size = Integer.parseInt(args[0]);
new Trisentis(size);
}
}
}
Hallo,
das ist der Quelltext von dem Spiel Trisentis. Allerdings erscheinen ein Paar Meldungen (siehe unten).
Beim Kompilieren
Compiliere C:\Dokumente und Einstellungen\Benutzername\Desktop\InputButton.java mit Jikes-Compiler
C:/Dokumente und Einstellungen/Benutzername/Desktop/InputButton.java:100:14:100:22: Semantic Warning:
The public type "Trisentis" does not match the name of its containing file "C:/Dokumente und Einstellungen/Benutzername/Desktop/InputButton.java".
C:/Dokumente und Einstellungen/Benutzername/Desktop/InputButton.java:134:5:134:10: Semantic Warning:
The method "void show();" declared in type "java.awt.Window" has been deprecated.
Beim Starten
Starte C:\Dokumente und Einstellungen\Benutzername\Desktop\InputButton.java
java.lang.NoSuchMethodError: main
Exception in thread "main"
Die zweite Meldung beim Kompilieren kann man ja ignorieren, aber was ist mit den anderen?
Danke im Voraus!