public void mouseDragged(MouseEvent evt) { Graphics g = canvas.getGraphics(); g.setColor(color); if (type == 0) { g.setXORMode(canvas.getBackground()); g.drawRect(start.x, start.y, end.x - start.x, end.y - start.y); end = evt.getPoint(); g.drawRect(start.x, start.y, end.x - start.x, end.y - start.y); } else if (type == 1) { int radius; g.setXORMode(canvas.getBackground()); radius = (int) Math.sqrt(Math.pow(end.x - start.x, 2) + Math.pow(end.y - start.y, 2)); g.drawOval(start.x - radius, start.y - radius, 2 * radius, 2 * radius); end = evt.getPoint(); radius = (int) Math.sqrt(Math.pow(end.x - start.x, 2) + Math.pow(end.y - start.y, 2)); g.drawOval(start.x - radius, start.y - radius, 2 * radius, 2 * radius); } else if (type == 2) { g.setXORMode(canvas.getBackground()); g.drawOval(start.x, start.y, end.x - start.x, end.y - start.y); end = evt.getPoint(); g.drawOval(start.x, start.y, end.x - start.x, end.y - start.y); } }
public void mouseReleased(MouseEvent evt) { Graphics g = canvas.getGraphics(); g.setColor(color); g.setPaintMode(); end = evt.getPoint(); if (type == 0) { g.drawRect(start.x, start.y, end.x - start.x, end.y - start.y); if (filled.getState() == true) g.fillRect(start.x, start.y, end.x - start.x, end.y - start.y); status.setText("2. Ecke des Rechtecks festgelegt"); } else if (type == 1) { int radius; radius = (int) Math.sqrt(Math.pow(end.x - start.x, 2) + Math.pow(end.y - start.y, 2)); g.drawOval(start.x - radius, start.y - radius, 2 * radius, 2 * radius); if (filled.getState() == true) g.fillOval(start.x - radius, start.y - radius, 2 * radius, 2 * radius); status.setText("Radius des Kreises festgelegt"); } else if (type == 2) { g.drawOval(start.x, start.y, end.x - start.x, end.y - start.y); if (filled.getState() == true) g.fillOval(start.x, start.y, end.x - start.x, end.y - start.y); status.setText("Radius der Ellipse festgelegt"); } }
public void init() { status = new Label("", Label.CENTER); menu = new Panel(); canvas = new MyCanvas(); farbe = new Choice(); color = Color.blue; filled = new Checkbox("ausgefuellt"); namen = new String[] {"Rechteck", "Kreis", "Ellipse"}; farben = new String[] {"blau", "gelb", "gruen", "rot", "schwarz", "weiss"}; button = new Button[namen.length]; type = -1; for (int i = 0; i < namen.length; i++) { button[i] = new Button(namen[i]); button[i].addActionListener(this); menu.add(button[i]); } for (int i = 0; i < farben.length; i++) farbe.add(farben[i]); filled.addItemListener(this); farbe.addItemListener(this); canvas.addMouseListener(this); canvas.addMouseMotionListener(this); menu.add(filled); menu.add(farbe); setLayout(new BorderLayout()); setBackground(Color.lightGray); add(menu, "North"); add(canvas, "Center"); add(status, "South"); }