Follow me


Hier verfolgt der Kreis die Maus.

Programmcode

 Followme.java 
1 import java.applet.*; 2 import java.awt.*; 3 import java.awt.event.*; 4 5 public class Followme extends Applet implements MouseListener, MouseMotionListener { 6 7 private int last_x = 20, last_y = 20; 8 private int x, y; 9 10 public void init() { 11 this.setBackground(Color.red); //Hintergrund wird gemalt 12 this.addMouseListener(this); 13 this.addMouseMotionListener(this); 14 } 15 16 public void paint(Graphics g) { // Startkreis wird gemalt 17 g.setColor(Color.yellow); 18 g.fillOval(last_x, last_y, 40, 40); 19 } 20 21 22 public void mouseClicked (MouseEvent e) { // Kreis hüpf! 23 Graphics g = this.getGraphics(); 24 g.setColor(Color.red); 25 g.fillOval(last_x, last_y, 40, 40); 26 g.setColor(Color.yellow); 27 last_x = e.getX() - 20; 28 last_y = e.getY() - 20; 29 g.fillOval(last_x, last_y, 40, 40); 30 } 31 32 public void mouseDragged (MouseEvent e) { // kennt einer noch Zini? 33 Graphics g = this.getGraphics(); 34 g.setColor(Color.red); 35 g.fillOval(last_x, last_y, 40, 40); 36 g.setColor(Color.yellow); 37 last_x = e.getX() - 20; 38 last_y = e.getY() - 20; 39 g.fillOval(last_x, last_y, 40, 40); 40 41 } 42 43 // die nicht verwendeten Methoden des MouseListeners 44 public void mouseReleased (MouseEvent e) { } 45 public void mouseEntered (MouseEvent e) { } 46 public void mouseExited (MouseEvent e) { } 47 public void mousePressed (MouseEvent e) { } 48 49 50 51 // Die andere, nicht verwendete Methode des MouseMotionListener-Interfaces 52 public void mouseMoved (MouseEvent e) { } 53 }

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