Etch-a-Sketch

To produce a Etch-A-Sketch applet, rename the Hello World class and change the message.

Etch-a-Sketch

To produce a Etch-A-Sketch applet, rename the Hello World class and change the message.

Etch-a-Sketch

To produce a Etch-A-Sketch applet, rename the Hello World class and change the message.

Etch-a-Sketch

To produce a Etch-A-Sketch applet, rename the Hello World class and change the message.

Etch-a-Sketch

To produce a Etch-A-Sketch applet, rename the Hello World class and change the message.

Etch-a-Sketch

Add some state for storing the current x and y positions

Etch-a-Sketch

Add some state for storing the current x and y positions

Etch-a-Sketch

Add some state for storing the current x and y positions

Etch-a-Sketch

We want to monitor the user's mouse clicks and drags. So lets do it the easy (old-fashioned, Java 1.0, deprecated) way by adding a handler for the mouseDown event...

Etch-a-Sketch

We want to monitor the user's mouse clicks and drags. So lets do it the easy (old-fashioned, Java 1.0, deprecated) way by adding a handler for the mouseDown event...

Etch-a-Sketch

We want to monitor the user's mouse clicks and drags. So lets do it the easy (old-fashioned, Java 1.0, deprecated) way by adding a handler for the mouseDown event...

Etch-a-Sketch

...and one for the mouseDrag event...

Etch-a-Sketch

...and one for the mouseDrag event...

Etch-a-Sketch

...and one for the mouseDrag event...

Etch-a-Sketch

Both these event handlers are boolean functions which have to return a value indicating whether the event was successfully handled.

Etch-a-Sketch

Both these event handlers are boolean functions which have to return a value indicating whether the event was successfully handled.

Etch-a-Sketch

Both these event handlers are boolean functions which have to return a value indicating whether the event was successfully handled.

Etch-a-Sketch

Every time a mouse event occurs, update the current position

Etch-a-Sketch

Every time a mouse event occurs, update the current position

Etch-a-Sketch

Every time a mouse event occurs, update the current position

Etch-a-Sketch

But only while the mouse is being dragged, draw a line between the new position and the last position

Etch-a-Sketch

But only while the mouse is being dragged, draw a line between the new position and the last position

Etch-a-Sketch

But only while the mouse is being dragged, draw a line between the new position and the last position

Etch-a-Sketch

But drawLine is an instance method for a graphics object. The paint() handler gets a Graphics object passed as a parameter. Here we must obtain one (using the getGraphics method of this applet)...

Etch-a-Sketch

But drawLine is an instance method for a graphics object. The paint() handler gets a Graphics object passed as a parameter. Here we must obtain one (using the getGraphics method of this applet)...

Etch-a-Sketch

But drawLine is an instance method for a graphics object. The paint() handler gets a Graphics object passed as a parameter. Here we must obtain one (using the getGraphics method of this applet)...

Etch-a-Sketch

...before we can use it.

Etch-a-Sketch

...before we can use it.

Etch-a-Sketch

...before we can use it.

Etch-a-Sketch

Finished code.

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

public class HelloWorldApplet extends Applet {

	public void paint(Graphics g){
		g.drawString("Hello World", 50, 50);
		}
}
import java.applet.Applet;
import java.awt.*;

public class HelloWorldApplet extends Applet {

	public void paint(Graphics g){
		g.drawString("Hello World", 50, 50);
		}
}
import java.applet.Applet;
import java.awt.*;

public class Sketch extends Applet {

	public void paint(Graphics g){
		g.drawString("Hello World", 50, 50);
		}
}
import java.applet.Applet;
import java.awt.*;

public class Sketch extends Applet {

	public void paint(Graphics g){
		g.drawString("Hello World", 50, 50);
		}
}
import java.applet.Applet;
import java.awt.*;

public class Sketch extends Applet {

	public void paint(Graphics g){
		g.drawString("Click-and-drag to draw", 50, 50);
		}
}
import java.applet.Applet;
import java.awt.*;

public class Sketch extends Applet {

	public void paint(Graphics g){
		g.drawString("Click-and-drag to draw", 50, 50);
		}
}
import java.applet.Applet;
import java.awt.*;

public class Sketch extends Applet {

	public void paint(Graphics g){
		g.drawString("Click-and-drag to draw", 50, 50);
		}
}
import java.applet.Applet;
import java.awt.*;

public class Sketch extends Applet {
	private int xpos, ypos;

	public void paint(Graphics g){
		g.drawString("Click-and-drag to draw", 50, 50);
		}
}
import java.applet.Applet;
import java.awt.*;

public class Sketch extends Applet {
	private int xpos, ypos;

	public void paint(Graphics g){
		g.drawString("Click-and-drag to draw", 50, 50);
		}
}
import java.applet.Applet;
import java.awt.*;

public class Sketch extends Applet {
	private int xpos, ypos;

	public void paint(Graphics g){
		g.drawString("Click-and-drag to draw", 50, 50);
		}
}
import java.applet.Applet;
import java.awt.*;

public class Sketch extends Applet {
	private int xpos, ypos;

	public boolean mouseDown(Event e, int x, int y){
		...
		}

	public void paint(Graphics g){
		g.drawString("Click-and-drag to draw", 50, 50);
		}
}
import java.applet.Applet;
import java.awt.*;

public class Sketch extends Applet {
	private int xpos, ypos;

	public boolean mouseDown(Event e, int x, int y){
		...
		}

	public void paint(Graphics g){
		g.drawString("Click-and-drag to draw", 50, 50);
		}
}
import java.applet.Applet;
import java.awt.*;

public class Sketch extends Applet {
	private int xpos, ypos;

	public boolean mouseDown(Event e, int x, int y){
		...
		}

	public void paint(Graphics g){
		g.drawString("Click-and-drag to draw", 50, 50);
		}
}
import java.applet.Applet;
import java.awt.*;

public class Sketch extends Applet {
	private int xpos, ypos;

	public boolean mouseDown(Event e, int x, int y){
		...
		}

	public boolean mouseDrag(Event e, int x, int y){
		...
		}

	public void paint(Graphics g){
		g.drawString("Click-and-drag to draw", 50, 50);
		}
}
import java.applet.Applet;
import java.awt.*;

public class Sketch extends Applet {
	private int xpos, ypos;

	public boolean mouseDown(Event e, int x, int y){
		...
		}

	public boolean mouseDrag(Event e, int x, int y){
		...
		}

	public void paint(Graphics g){
		g.drawString("Click-and-drag to draw", 50, 50);
		}
}
import java.applet.Applet;
import java.awt.*;

public class Sketch extends Applet {
	private int xpos, ypos;

	public boolean mouseDown(Event e, int x, int y){
		...
		}

	public boolean mouseDrag(Event e, int x, int y){
		...
		}

	public void paint(Graphics g){
		g.drawString("Click-and-drag to draw", 50, 50);
		}
}
import java.applet.Applet;
import java.awt.*;

public class Sketch extends Applet {
	private int xpos, ypos;

	public boolean mouseDown(Event e, int x, int y){
		...
		return true;
		}

	public boolean mouseDrag(Event e, int x, int y){
		...
		return true;
		}

	public void paint(Graphics g){
		g.drawString("Click-and-drag to draw", 50, 50);
		}
}
import java.applet.Applet;
import java.awt.*;

public class Sketch extends Applet {
	private int xpos, ypos;

	public boolean mouseDown(Event e, int x, int y){
		...
		return true;
		}

	public boolean mouseDrag(Event e, int x, int y){
		...
		return true;
		}

	public void paint(Graphics g){
		g.drawString("Click-and-drag to draw", 50, 50);
		}
}
import java.applet.Applet;
import java.awt.*;

public class Sketch extends Applet {
	private int xpos, ypos;

	public boolean mouseDown(Event e, int x, int y){
		...
		return true;
		}

	public boolean mouseDrag(Event e, int x, int y){
		...
		return true;
		}

	public void paint(Graphics g){
		g.drawString("Click-and-drag to draw", 50, 50);
		}
}
import java.applet.Applet;
import java.awt.*;

public class Sketch extends Applet {
	private int xpos, ypos;

	public boolean mouseDown(Event e, int x, int y){
		xpos=x;
		ypos=y;
		return true;
		}

	public boolean mouseDrag(Event e, int x, int y){
		xpos=x;
		ypos=y;
		return true;
		}

	public void paint(Graphics g){
		g.drawString("Click-and-drag to draw", 50, 50);
		}
}
import java.applet.Applet;
import java.awt.*;

public class Sketch extends Applet {
	private int xpos, ypos;

	public boolean mouseDown(Event e, int x, int y){
		xpos=x;
		ypos=y;
		return true;
		}

	public boolean mouseDrag(Event e, int x, int y){
		xpos=x;
		ypos=y;
		return true;
		}

	public void paint(Graphics g){
		g.drawString("Click-and-drag to draw", 50, 50);
		}
}
import java.applet.Applet;
import java.awt.*;

public class Sketch extends Applet {
	private int xpos, ypos;

	public boolean mouseDown(Event e, int x, int y){
		xpos=x;
		ypos=y;
		return true;
		}

	public boolean mouseDrag(Event e, int x, int y){
		xpos=x;
		ypos=y;
		return true;
		}

	public void paint(Graphics g){
		g.drawString("Click-and-drag to draw", 50, 50);
		}
}
import java.applet.Applet;
import java.awt.*;

public class Sketch extends Applet {
	private int xpos, ypos;

	public boolean mouseDown(Event e, int x, int y){
		xpos=x;
		ypos=y;
		return true;
		}

	public boolean mouseDrag(Event e, int x, int y){
		drawLine(xpos, ypos, x, y);
		xpos=x;
		ypos=y;
		return true;
		}

	public void paint(Graphics g){
		g.drawString("Click-and-drag to draw", 50, 50);
		}
}
import java.applet.Applet;
import java.awt.*;

public class Sketch extends Applet {
	private int xpos, ypos;

	public boolean mouseDown(Event e, int x, int y){
		xpos=x;
		ypos=y;
		return true;
		}

	public boolean mouseDrag(Event e, int x, int y){
		drawLine(xpos, ypos, x, y);
		xpos=x;
		ypos=y;
		return true;
		}

	public void paint(Graphics g){
		g.drawString("Click-and-drag to draw", 50, 50);
		}
}
import java.applet.Applet;
import java.awt.*;

public class Sketch extends Applet {
	private int xpos, ypos;

	public boolean mouseDown(Event e, int x, int y){
		xpos=x;
		ypos=y;
		return true;
		}

	public boolean mouseDrag(Event e, int x, int y){
		drawLine(xpos, ypos, x, y);
		xpos=x;
		ypos=y;
		return true;
		}

	public void paint(Graphics g){
		g.drawString("Click-and-drag to draw", 50, 50);
		}
}
import java.applet.Applet;
import java.awt.*;

public class Sketch extends Applet {
	private int xpos, ypos;

	public boolean mouseDown(Event e, int x, int y){
		xpos=x;
		ypos=y;
		return true;
		}

	public boolean mouseDrag(Event e, int x, int y){
		Graphics g=this.getGraphics();
		drawLine(xpos, ypos, x, y);
		xpos=x;
		ypos=y;
		return true;
		}

	public void paint(Graphics g){
		g.drawString("Click-and-drag to draw", 50, 50);
		}
}
import java.applet.Applet;
import java.awt.*;

public class Sketch extends Applet {
	private int xpos, ypos;

	public boolean mouseDown(Event e, int x, int y){
		xpos=x;
		ypos=y;
		return true;
		}

	public boolean mouseDrag(Event e, int x, int y){
		Graphics g=this.getGraphics();
		drawLine(xpos, ypos, x, y);
		xpos=x;
		ypos=y;
		return true;
		}

	public void paint(Graphics g){
		g.drawString("Click-and-drag to draw", 50, 50);
		}
}
import java.applet.Applet;
import java.awt.*;

public class Sketch extends Applet {
	private int xpos, ypos;

	public boolean mouseDown(Event e, int x, int y){
		xpos=x;
		ypos=y;
		return true;
		}

	public boolean mouseDrag(Event e, int x, int y){
		Graphics g=this.getGraphics();
		drawLine(xpos, ypos, x, y);
		xpos=x;
		ypos=y;
		return true;
		}

	public void paint(Graphics g){
		g.drawString("Click-and-drag to draw", 50, 50);
		}
}
import java.applet.Applet;
import java.awt.*;

public class Sketch extends Applet {
	private int xpos, ypos;

	public boolean mouseDown(Event e, int x, int y){
		xpos=x;
		ypos=y;
		return true;
		}

	public boolean mouseDrag(Event e, int x, int y){
		Graphics g=this.getGraphics();
		g.drawLine(xpos, ypos, x, y);
		xpos=x;
		ypos=y;
		return true;
		}

	public void paint(Graphics g){
		g.drawString("Click-and-drag to draw", 50, 50);
		}
}
import java.applet.Applet;
import java.awt.*;

public class Sketch extends Applet {
	private int xpos, ypos;

	public boolean mouseDown(Event e, int x, int y){
		xpos=x;
		ypos=y;
		return true;
		}

	public boolean mouseDrag(Event e, int x, int y){
		Graphics g=this.getGraphics();
		g.drawLine(xpos, ypos, x, y);
		xpos=x;
		ypos=y;
		return true;
		}

	public void paint(Graphics g){
		g.drawString("Click-and-drag to draw", 50, 50);
		}
}