Scribble on screen

This applet allows the user to scribble on the screen with the mouse1

import java.applet.*;
import java.awt.*;

public class Scribble extends Applet {

private int last_x = 0;
private int last_y = 0;
private Color current_color =;
private Button clear_button;
private Choice color_choices;

// Called to initialize the applet.
public void init() {

// Set the background color
// Create a button and add it to the applet.
// Also, set the button's colors
clear_button = new Button("Clear");
// Create a menu of colors and add it to the applet.
// Also set the menus's colors and add a label.
color_choices = new Choice();
this.add(new Label("Color: "));


// Called when the user clicks the mouse to start a scribble
public boolean mouseDown(Event e, int x, int y) {

last_x = x; last_y = y;
return true;


// Called when the user scribbles with the mouse button down
public boolean mouseDrag(Event e, int x, int y) {

Graphics g = this.getGraphics();
g.drawLine(last_x, last_y, x, y);
last_x = x;
last_y = y;
return true;


// Called when the user clicks the button or chooses a color
public boolean action(Event event, Object arg) {

// If the Clear button was clicked on, handle it.
if ( == clear_button) {

Graphics g = this.getGraphics();
Rectangle r = this.bounds();
g.fillRect(r.x, r.y, r.width, r.height);
return true;

// Otherwise if a color was chosen, handle that
else if ( == color_choices) {

String colorname = (String) arg;
if (arg.equals("black")) current_color =;
else if (arg.equals("red")) current_color =;
else if (arg.equals("yellow")) current_color = Color.yellow;
else if (arg.equals("green")) current_color =;
return true;

// Otherwise, let the superclass handle it.
else return super.action(event, arg);



This is what it does:

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License