Example #1
0
  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);
    }
  }