FileDocCategorySizeDatePackage
DrawCanvas.javaAPI DocExample767Thu Jul 29 15:21:22 BST 1999None

DrawCanvas.java

import java.awt.*;
import java.awt.event.*;

public class DrawCanvas extends MouseableCanvas
{
	int mx[];
	int my[];
	int counter = 0;
	static final int ARRAYSIZE = 100;
	
	public DrawCanvas()
	{
		mx = new int[ARRAYSIZE ];
		my = new int[ARRAYSIZE ];
	}
	
	public void mousePressed(MouseEvent e)
	{
		mx[counter] = e.getX();
		my[counter] = e.getY();
		System.out.println("x: " + mx[counter] + "  y: " + my[counter]);
		Graphics g = this.getGraphics();
		if(counter > 0)
			g.drawLine(mx[counter-1], my[counter-1], mx[counter], my[counter]);
		counter++;
		if(counter > ARRAYSIZE-1) counter = 0;
	}
	
	public void paint(Graphics g)
	{
		for(int i = 0; i < counter-1; i++)
		{
			g.drawLine(mx[i], my[i], mx[i+1], my[i+1]);
		}
	}
}