-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscribble.java
More file actions
66 lines (62 loc) · 2.06 KB
/
Copy pathscribble.java
File metadata and controls
66 lines (62 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import java.applet.*;
import java.awt.*;
public class scribble extends Applet{
private int last_x,last_y;
private Color current_color = Color.black;
private Button clear_button;
private Choice color_choices;
//this method is called to initillize the applet
public void init(){
this.setBackground(Color.white);
clear_button = new Button("clear");
clear_button.setForeground(Color.black);
clear_button.setBackground(Color.lightGray);
this.add(clear_button);
//creat a name of color and add it to tge applet
//also set the menue color and add label
color_choices = new Choice();
color_choices.addItem("black");
color_choices.addItem("yellow");
color_choices.addItem("blue");
color_choices.addItem("red");
color_choices.addItem("pink");
color_choices.setForeground(Color.black);
color_choices.setBackground(Color.lightGray);
this.add(new Label("Color"));
this.add(color_choices);
}
//this method is 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;
}
//this method is called when the user drags the mouse
public boolean mouseDrag(Event e,int x,int y){
Graphics g=this.getGraphics();
g.setColor(current_color);
g.drawLine(last_x,last_y,x,y);
last_x=x;
last_y=y;
return true;
}
//this method is called when the user click the button or choose a color
public boolean action(Event event,Object arg){
if(event.target == clear_button){
Graphics g=this.getGraphics();
Rectangle r=this.bounds();
g.setColor(this.getBackground());
g.fillRect(r.x,r.y,r.width,r.height);
return true;
}
else if(event.target == color_choices){
if(arg.equals("black")) current_color=Color.black;
else if(arg.equals("yellow")) current_color=Color.yellow;
else if(arg.equals("blue")) current_color=Color.blue;
else if(arg.equals("red")) current_color=Color.red;
else if(arg.equals("pink")) current_color=Color.pink;
return true;
}
//otherwise,let the superclass handle it.
else return super.action(event,arg);
}
}