Java Programming Code Examples
Java > Applets Code Examples
EventDemo
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
EventDemo
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class EventDemo extends Applet implements ActionListener, ItemListener, AdjustmentListener {
// An applet that displays a shape and some text. Color of
// shape and text is controlled by a vertical scroll bar.
// Color of background is controlled by a horizontal scroll bar.
// Text to be displayed can be entered in a TextField.
// Shape to be displayed can be selected from a Choice componnet.
// Bright or dim colors can be selected using a Chackbox.
// The display area is implemented as a ColorCanvas; the
// ColorCanvas class is defined later in this file.
ColorCanvas display; // display area
Choice shapeChoice; // for selecting which shape to display
Checkbox brightColors;// for selecting bright or dim colors
TextField text; // for entering the text to be displayed
Scrollbar hScroll; // horizontal scroll bar
Scrollbar vScroll; // vertical scroll bar
public void init() { // set up contents of applet
setBackground(Color.red); // background for applet
Panel displayPanel = new Panel(); // to hold display and scroll bars
displayPanel.setLayout(new BorderLayout());
display = new ColorCanvas("Hello World",ColorCanvas.RECT);
displayPanel.add(display, BorderLayout.CENTER);
hScroll = new Scrollbar(Scrollbar.HORIZONTAL,50,1,0,100);
displayPanel.add(hScroll, BorderLayout.SOUTH);
vScroll = new Scrollbar(Scrollbar.VERTICAL,0,1,0,100);
displayPanel.add(vScroll, BorderLayout.EAST);
Panel topPanel = new Panel(); // for controls
topPanel.setBackground(Color.white);
topPanel.setLayout(new GridLayout(1,3,5,5));
shapeChoice = new Choice();
shapeChoice.add("Rectangle");
shapeChoice.add("Oval");
shapeChoice.add("RoundRect");
topPanel.add(shapeChoice);
brightColors = new Checkbox("Bright Colors");
topPanel.add(brightColors);
text = new TextField("Hello World");
topPanel.add(text);
setLayout(new BorderLayout(1,1)); // applies to applet itself
add("Center", displayPanel);
add("North", topPanel);
setDisplayColors(); // defined below
text.addActionListener(this); // set this applet to listen for events
hScroll.addAdjustmentListener(this);
vScroll.addAdjustmentListener(this);
shapeChoice.addItemListener(this);
brightColors.addItemListener(this);
} // end of init()
public Insets getInsets() { // leave border around edge of applet
return new Insets(1,1,1,1);
}
void setDisplayColors() {
// Set foreground and background colors of display,
// depending on values of scroll bars and
// on state of the checkbox. (Colors are made
// using Color.getHSBColor(float,float,float),
// which creates a color given a hue, a satuation,
// and a brightness. The parameters must be between
// 0.0 and 1.0.)
float backgroundBrightness = hScroll.getValue() / 100.0F;
float foregroundHue = vScroll.getValue() / 100.0F;
float saturation = 1.0F;
float brightness;
if (brightColors.getState())
brightness = 1.0F;
else
brightness = 0.6F;
Color backgroundColor =
Color.getHSBColor(0.0F,0.0F,backgroundBrightness); // A grayscale color
Color foregroundColor =
Color.getHSBColor(foregroundHue,1.0F,brightness);
display.setBackground(backgroundColor);
display.setForeground(foregroundColor);
} // end of setDisplayColors()
public void itemStateChanged(ItemEvent evt) { // from the ItemListener interface
// An item event could have been generated by shapeChoice or brightColors.
Object source = evt.getSource(); // object that generated the event
if (source == shapeChoice) {
// user has selected a shape; set the shape
// that is shown in the display
switch (shapeChoice.getSelectedIndex()) {
case 0:
display.setShape(ColorCanvas.RECT);
break;
case 1:
display.setShape(ColorCanvas.OVAL);
break;
case 2:
display.setShape(ColorCanvas.ROUNDED);
break;
}
}
else if (source == brightColors) {
// user has changed the state of the checkbox;
// reset the colors for the display,
// and ask the system to redraw the display
setDisplayColors();
display.repaint();
}
} // end itemStateChanged()
public void actionPerformed(ActionEvent evt) { // from the ActionListener interface
// This can only be generated by the TextField, because the user has pressed
// return. Set the display text to the contents of the TextField, text.
display.setText(text.getText());
} // end of actionPerformed()
public void adjustmentValueChanged(AdjustmentEvent evt) {
// User has changed the value of one of the scroll bars.
// In thisprogram, it doesn't matter which one. Respond
// by setting the display colors.
setDisplayColors();
display.repaint();
} // end of adjustmentValueChanged()
} // end of class Event Demo
class ColorCanvas extends Canvas {
// Display a shape and some text.
// The canvas's setForeground() and setBackground()
// methods should be called to set the colors to
// be used for drawing.
private String text; // text to be displayed
private int shape; // code for shape to be displayed;
private Font font; // used for displaying the text
public final static int RECT = 0; // shape code for a rectangle
public final static int OVAL = 1; // shape code for an oval
public final static int ROUNDED = 2; // shape code for an round rect
public ColorCanvas(String text, int shape) {
this.text = text;
this.shape = shape;
font = new Font("Serif",Font.PLAIN,18);
}
public void setText(String text) {
this.text = text;
repaint();
}
public void setShape(int shape) {
this.shape = shape;
repaint();
}
public void paint(Graphics g) {
int width = getSize().width; // get size of canvas
int height = getSize().height;
int shape_left = width / 9; // compute position and size of shape
int shape_top = height / 3;
int shape_width = (7*width / 9);
int shape_height = (5*height / 9);
switch (shape) { // draw the shape
case RECT:
g.fillRect(shape_left,shape_top,shape_width,shape_height);
break;
case OVAL:
g.fillOval(shape_left,shape_top,shape_width,shape_height);
break;
case ROUNDED:
g.fillRoundRect(shape_left,shape_top,shape_width,shape_height,24,24);
break;
}
g.setFont(font);
g.drawString(text,width/9,2*height/9); // draw the text
}
} // end of class ColorCanvas