interface Paintable{
public void paint( Graphics g );
}
class Rect implements Paintable{
private int x, y, w, h;
private Color color;
public Rect( Color color, int x, int y, int w, int h ){
this.color = color;
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
public void paint( Graphics g ){
g.setColor( color );
g.drawRect( x, y, w, h );
}
}
class PaLiMa extends Frame implements ActionListener
{
private Vector paints = new Vector(); //######### das ist neu
Color farbe = new Color(0,0,0);
int gitterstatus = 0;
public void paint( Graphics g ){ // ######## das verändert
g.setColor( Color.BLACK );
g.fillRect( 0, 0, getWidth(), getHeight() );
for( int i = 0; i < paints.size(); i++ )
((Paintable)paints.get(i)).paint( g );
}
public void test(Color farbe, int x, int y, int breite, int hoehe) // ####### das verändert
{
paints.add( new Rect( farbe, x, y, breite, hoehe ) );
repaint();
}
public PaLiMa()
{
super("PaLiMa A0.1");
//setBackground(Color.lightGray);
setLayout(new FlowLayout());
setSize(800,600);
//Positioniert das Fenster in der Mitte des Bildschirms
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
setLocation( (d.width - getSize().width ) / 2,
(d.height- getSize().height) / 2 );
setMenuBar(new MainMenu(this));
setResizable(false);
//Dialog-Button
Button button = new Button("Dialog");
button.addActionListener(this);
add(button);
//Gitternetz-Button
button = new Button("Gitternetz");
button.addActionListener(this);
add(button);
//Loeschen-Button
button = new Button("Loeschen");
button.addActionListener(this);
add(button);
//Test-Button
button = new Button("Test");
button.addActionListener(this);
add(button);
//Window-Listener
addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent event)
{
setVisible(false);
dispose();
System.exit(0);
}
}
);
}
public void actionPerformed(ActionEvent event)
{
String cmd = event.getActionCommand();
if (cmd.equals("Dialog")) {
InfoWnd dlg = new InfoWnd(this);
dlg.setVisible(true);
} else if (cmd.equals("Loeschen")) {
repaint();
} else if (cmd.equals("Test")) {
test(new Color(100,0,0), 50, 50, 100, 200);
}
}
public static void main(String[] args)
{
PaLiMa wnd = new PaLiMa();
wnd.setVisible(true);
}
}