Rubber Banding


[Up]

Please click inside applet window.

Source Code

import java.awt.Graphics;
import java.awt.Event;
import java.awt.Color;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseEvent;
import java.applet.Applet;

public class Rubber extends Applet 
             implements MouseListener, MouseMotionListener {
  int sx, sy, ex, ey;
  boolean first = false;

  public void init() {
    addMouseListener(this);
    addMouseMotionListener(this);
  }
  public void paint(Graphics g) {
    g.setColor(Color.blue);
    g.drawLine(sx, sy, ex, ey);
  }
  public void mouseEntered(MouseEvent evt) {}
  public void mouseExited(MouseEvent evt) {}
  public void mousePressed(MouseEvent evt) {}
  public void mouseReleased(MouseEvent evt) {}
  public void mouseClicked(MouseEvent evt) {
    int x, y;
    x = evt.getX(); y = evt.getY();
    if (first == true) {
      first = false;
    }
    else {
      sx = x; sy = y;
      first = true;
    }
  }
  public void mouseDragged(MouseEvent evt) {}
  public void mouseMoved(MouseEvent evt) {
    int x, y;
    x = evt.getX(); y = evt.getY();
    if (first == true) {
      ex = x; ey = y;
      repaint();
    }
  }
}

naniwa@rbt.his.fukui-u.ac.jp