import java.awt.*; import java.awt.event.*; enum ZeichenModus {LINIE, RECHTECK, ELLIPSE}; class GrafikFenster extends Frame{ private static final long serialVersionUID = 7526471155622776147L; private static Button stopButton = new Button("Stop Button"); private static Button linieButton = new Button("Linien malen"); private static Button RechteckButton = new Button("Rechtecke malen"); private static Button ellipsenButton = new Button("Ellipsen malen"); private static Button fillButton = new Button("Gefüllt ?"); private ZeichenModus zModus = ZeichenModus.LINIE; private boolean gefüllt = false; private int x1; private int y1; GrafikFenster(String title, int breite, int höhe){ super(title); setLayout(new FlowLayout()); stopButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { fensterBeenden(); } }); add(stopButton); linieButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { zModus = ZeichenModus.LINIE; } }); add(linieButton ); RechteckButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { zModus = ZeichenModus.RECHTECK; } }); add(RechteckButton ); ellipsenButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { zModus = ZeichenModus.ELLIPSE; } }); add(ellipsenButton ); fillButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { gefüllt = !gefüllt; } }); add(fillButton ); addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e){ x1 = e.getX(); y1 = e.getY(); } public void mouseReleased(MouseEvent e){ int x2 = e.getX(); int y2 = e.getY(); Graphics g = getGraphics(); g.setColor(Color.blue); if (zModus == ZeichenModus.LINIE) g.drawLine(x1, y1, x2, y2); else { int w= Math.abs(x1 - x2); int h= Math.abs(y1 - y2); int x= Math.min(x1, x2); int y= Math.min(y1, y2); if (zModus == ZeichenModus.RECHTECK) if (gefüllt)g.fillRect(x, y, w, h); else g.drawRect(x, y, w, h); else if (zModus == ZeichenModus.ELLIPSE) if (gefüllt)g.fillOval(x, y, w, h); else g.drawOval(x, y, w, h); } } }); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { fensterBeenden(); } }); setBackground(Color.lightGray); setSize(breite, höhe); setVisible(true); } void fensterBeenden() { dispose(); } } class FensterTester { public static void main(String args[]) { GrafikFenster mainFenster = new GrafikFenster("Fenstermaler",800,600); } }