To produce a Etch-A-Sketch applet, rename the Hello World class and change the message.
To produce a Etch-A-Sketch applet, rename the Hello World class and change the message.
To produce a Etch-A-Sketch applet, rename the Hello World class and change the message.
To produce a Etch-A-Sketch applet, rename the Hello World class and change the message.
To produce a Etch-A-Sketch applet, rename the Hello World class and change the message.
Add some state for storing the current x and y positions
Add some state for storing the current x and y positions
Add some state for storing the current x and y positions
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...
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...
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...
...and one for the mouseDrag event...
...and one for the mouseDrag event...
...and one for the mouseDrag event...
Both these event handlers are boolean functions which have to return a value indicating whether the event was successfully handled.
Both these event handlers are boolean functions which have to return a value indicating whether the event was successfully handled.
Both these event handlers are boolean functions which have to return a value indicating whether the event was successfully handled.
Every time a mouse event occurs, update the current position
Every time a mouse event occurs, update the current position
Every time a mouse event occurs, update the current position
But only while the mouse is being dragged, draw a line between the new position and the last position
But only while the mouse is being dragged, draw a line between the new position and the last position
But only while the mouse is being dragged, draw a line between the new position and the last position
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)...
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)...
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)...
...before we can use it.
...before we can use it.
...before we can use it.
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);
}
}