Rubber Bunding with Memory


[Up]

Please click inside applet window.

Source Code

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

public class MRubber extends Applet 
             implements MouseListener, MouseMotionListener {
  int sx, sy, ex, ey;
  boolean first = false;
  Image buffer;
  Graphics GC;

  public void init() {
    buffer = createImage(400, 400);
    GC = buffer.getGraphics();
    GC.setColor(Color.white);
    GC.fillRect(0, 0, 400, 400);
    GC.setColor(Color.blue);
    addMouseListener(this);
    addMouseMotionListener(this);
    setBackground(Color.white);
  }
  public void paint(Graphics g) {
    g.drawImage(buffer, 0, 0, this);
    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) {
      GC.drawLine(sx, sy, x, y);
      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