Störe meine Kreise nicht


Hier kann man Kreise oder Ellipsen malen, indem man einen Anfangspunkt mit der Maus wählt, klickt und sich einen Endpunkt sucht.
 StoereKreisenicht.java 
1 import java.applet.*; 2 import java.awt.*; 3 import java.awt.event.*; 4 5 public class StoereKreisenicht extends Applet implements MouseListener, MouseMotionListener { 6 7 private int first_x, first_y, last_x, last_y; 8 private int x, y, hoehe, breite; 9 private boolean klick = false; 10 11 public void init() { 12 this.setBackground(Color.yellow); 13 this.addMouseListener(this); 14 this.addMouseMotionListener(this); 15 } 16 17 18 19 // Die anderen, nicht verwendeten Methoden des MouseListener-Interfaces 20 public void mouseClicked (MouseEvent e) { 21 if (klick == false) { 22 first_x = e.getX(); 23 first_y = e.getY(); 24 klick = true; 25 } 26 else { 27 last_x = e.getX(); 28 last_y = e.getY(); 29 Graphics g = this.getGraphics(); 30 31 if ((first_x < last_x) && (first_y < last_y)) { 32 /* es gibt vier Fälle, wo die Punkte sitzen können */ 33 x = first_x; 34 y = first_y; 35 breite = last_x-first_x; 36 hoehe = last_y-first_y; 37 } 38 39 if ((first_x > last_x) && (first_y < last_y)) { 40 x = last_x; 41 y = first_y; 42 breite = Math.abs(last_x-first_x); 43 hoehe = Math.abs(last_y-first_y); 44 } 45 46 if ((first_x > last_x) && (first_y > last_y)) { 47 x = last_x; 48 y = last_y; 49 breite = Math.abs(last_x-first_x); 50 hoehe = Math.abs(last_y-first_y); 51 } 52 53 if ((first_x < last_x) && (first_y > last_y)) { 54 x = first_x; 55 y = last_y; 56 breite = Math.abs(last_x-first_x); 57 hoehe = Math.abs(last_y-first_y); 58 } 59 g.setColor(Color.blue); 60 g.fillOval(x, y, breite, hoehe); 61 g.setColor(Color.red); 62 g.drawOval(x, y, breite, hoehe); 63 klick = false; 64 } 65 } 66 // die nicht verwendeten Methoden des MouseListeners 67 public void mouseEntered (MouseEvent e) { } 68 public void mouseExited (MouseEvent e) { } 69 public void mousePressed (MouseEvent e) { } 70 public void mouseDragged (MouseEvent e) { } 71 public void mouseReleased (MouseEvent e) { } 72 73 // Die andere, nicht verwendete Methode des MouseMotionListener-Interfaces 74 public void mouseMoved (MouseEvent e) { } 75 76 } 77 78

Letzte Änderung: 22.08.2009: 11:57:24 von X. Rendtel