Draw Lines Applet (Without Flicker)
// this applet allows the user to draw up to 10 lines by dragging the mouse
// drawing is done on an offscreen Image to reduce flicker
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Color;
import java.awt.Point;
import java.applet.AudioClip;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
public class LinesNew11 extends java.applet.Applet
implements MouseListener, MouseMotionListener {
private final int MAXLINES = 10;
private Point[] starts;
private Point[] ends;
private Point anchor;
private Point currentpoint;
private int currlines;
private AudioClip tooMany;
private Image drawing;
private Graphics drawGraph;
private int width;
private int height;
// allocate the array of Points, set the background color, load the error sound
// save the size of the applet, and register to listen for mouse events
public void init() {
width = getSize().width;
height = getSize().height;
starts = new Point[MAXLINES];
ends = new Point[MAXLINES];
setBackground(Color.white);
currlines = 0;
tooMany = getAudioClip(getCodeBase(),"bong.au");
this.addMouseListener(this);
this.addMouseMotionListener(this);
}
// start a new line when the user depresses the mouse
public void mousePressed(MouseEvent e)
{
anchor = new Point(e.getX(),e.getY());
repaint();
}
// draw the potential line as the user drags the mouse around
public void mouseDragged(MouseEvent e)
{
currentpoint = new Point(e.getX(),e.getY());
repaint();
}
public void mouseMoved(MouseEvent e) { }
// add the new line when the user releases the mouse button
public void mouseReleased(MouseEvent e)
{
if (currlines < MAXLINES)
addline(e.getX(),e.getY());
else {
tooMany.play();
currentpoint = null;
repaint();
}
}
public void mouseClicked(MouseEvent e) { }
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
// add new line to array of permanent lines
void addline(int x, int y) {
starts[currlines] = anchor;
ends[currlines] = currentpoint;
currlines++;
currentpoint = null;
repaint();
}
// override update to remove call to clear applet
public void update(Graphics g)
{
paint(g);
}
// create the offscreen drawing image, draw rectangle around it,
// and draw the lines (with potential line in blue)
// draw the offscreen image on the screen
public void paint(Graphics g)
{
if (drawing == null)
{
drawing = createImage(width,height);
drawGraph = drawing.getGraphics();
}
// clear the Image, then draw the border
drawGraph.setColor(Color.black);
drawGraph.clearRect(0,0,width,height);
drawGraph.drawRect(0,0,width-1,height-1);
drawGraph.drawRect(1,1,width-3,height-3);
// draw the permanent lines, then draw the potential line in blue
for (int i = 0 ; i < currlines ; i++)
drawGraph.drawLine(starts[i].x,starts[i].y,ends[i].x,ends[i].y);
drawGraph.setColor(Color.blue);
if (currentpoint != null)
drawGraph.drawLine(anchor.x,anchor.y,currentpoint.x,currentpoint.y);
// transfer the completed Image onto the Graphics object for the applet
g.drawImage(drawing,0,0,null);
}
}