PaintApp Listing
/* PaintApp.java - Paint Applet.
Java Resources
http://geocities.datacellar.net/SiliconValley/Bay/4050/
Color Parameters:
bgcolor = background
fgcolor = foreground (drawing color)
bordercolor = border
Each color value is a hexadecimal number of the form
"rrggbb"
where rr, gg, bb specify the amount of red, green, and blue,
respectively, and can range from 00 to FF.
Example:
<PARAM NAME="bgcolor" VALUE="FFFFFF">
*/
import java.awt.*;
import java.applet.*;
//
// PaintApp
//
public class PaintApp extends Applet {
//
// Attributes
//
private Dimension size;
private Color backColor, foreColor, borderColor;
private Image drawImage;
private Graphics gDraw;
private Point prevPoint;
private boolean isDrawing = false;
//
// Operations
//
public void init() {
size = size();
backColor = initColor("bgcolor", getBackground());
foreColor = initColor("fgcolor", Color.black);
borderColor = initColor("bordercolor", Color.black);
clearImage();
}
// Get the color from the applet parameters
private Color initColor(String param, Color defColor) {
try {
String s;
if((s = getParameter(param)) != null)
defColor = new Color(Integer.parseInt(s, 16));
}
catch(Exception e) { }
return defColor;
}
public void paint(Graphics g) {
g.drawImage(drawImage, 0, 0, null);
}
public void update(Graphics g) { // Override-don't erase background
paint(g);
}
public boolean mouseDown(Event evt, int x, int y) {
if(evt.modifiers == Event.META_MASK) {
clearImage();
repaint();
} else {
prevPoint = new Point(x, y);
isDrawing = true;
}
return true;
}
public boolean mouseDrag(Event evt, int x, int y) {
if(isDrawing) {
gDraw.drawLine(prevPoint.x, prevPoint.y, x, y);
prevPoint.move(x, y);
repaint();
}
return true;
}
public boolean mouseUp(Event evt, int x, int y) {
isDrawing = false;
return true;
}
private void clearImage() {
drawImage = createImage(size.width, size.height);
gDraw = drawImage.getGraphics();
gDraw.setColor(backColor);
gDraw.fillRect(0, 0, size.width, size.height);
gDraw.setColor(borderColor);
gDraw.drawRect(0, 0, size.width - 1, size.height - 1);
gDraw.setColor(foreColor);
}
//
// Information functions
//
public String getAppletInfo() {
return "Paint Applet. Copyright © 1998 Java Resources.";
}
public String[][] getParameterInfo() {
String info[][] = {
{ "bgcolor", "rrggbb", "Background" },
{ "fgcolor", "rrggbb", "Foreground" },
{ "bordercolor", "rrggbb", "Border" }
};
return info;
}
}
<title>PaintApp Java Applet</title>
<div align=center>
<h1>PaintApp</h1>
Scribble in the box below.<br>
Right-click to erase.<p>
<applet code=PaintApp width=200 height=200>
<param name="bgcolor" value="ffffff">
<param name="fgcolor" value="ff">
<param name="bordercolor" value="808080">
This scribble applet requires a Java-enabled browser.
</applet>
</div>