forked from icterguru/JavaProgrammingA2Z
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJ16_13.java
More file actions
57 lines (49 loc) · 1.6 KB
/
J16_13.java
File metadata and controls
57 lines (49 loc) · 1.6 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
package chapter16;
/* J16_13.java */
/* Using Scrollbar Component */
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class J16_13 extends Applet
implements AdjustmentListener, MouseMotionListener {
String msg = "";
Scrollbar VerticalSB, HorizontalSB;
public void init() {
int width = Integer.parseInt(getParameter("width"));
int height = Integer.parseInt(getParameter("height"));
VerticalSB = new Scrollbar(Scrollbar.VERTICAL,
0, 1, 0, height);
HorizontalSB = new Scrollbar(Scrollbar.HORIZONTAL,
0, 1, 0, width);
add(VerticalSB);
add(HorizontalSB);
// register to receive adjustment events
VerticalSB.addAdjustmentListener(this);
HorizontalSB.addAdjustmentListener(this);
addMouseMotionListener(this);
}
public void adjustmentValueChanged(AdjustmentEvent ae) {
repaint();
}
// Update scroll bars to reflect mouse dragging.
public void mouseDragged(MouseEvent me)
{
int x = me.getX();
int y = me.getY();
VerticalSB.setValue(y);
HorizontalSB.setValue(x);
repaint();
}
// Necessary for MouseMotionListener
public void mouseMoved(MouseEvent me) { }
// display current value of scroll bars.
public void paint(Graphics g) {
g.drawString("Cursor Position is : ", 10, 100);
msg = "Vertical: " + VerticalSB.getValue();
msg += ", Horizontal: " + HorizontalSB.getValue();
g.drawString(msg, 10, 120);
// show current mouse drag position
g.drawString("|", HorizontalSB.getValue(),
VerticalSB.getValue());
}
}