|
1 /*************************************************************************** |
|
2 Title: awtUtilities/ScrollCanvas.java |
|
3 ID: $Id$ |
|
4 Author: Stefan Berghofer, TU Muenchen |
|
5 Copyright 1997 TU Muenchen |
|
6 |
|
7 This class contains defines a window with scrollbars, which is not |
|
8 supported by the 1.0.2 version of the JDK. To use this class, simply |
|
9 derive your own class from it and overwrite the paintCanvas method. |
|
10 When a repaint event occurs, ScrollCanvas automatically calls the |
|
11 paintCanvas method. The user doesn't have to take care of transposing |
|
12 mouse or graphics coordinates. |
|
13 Note: In later versions of the JDK, a ScrollPane class will be provided, |
|
14 which can be used instead. |
|
15 ***************************************************************************/ |
|
16 |
|
17 package awtUtilities; |
|
18 |
|
19 import java.awt.*; |
|
20 |
|
21 class DummyCanvas extends Canvas { |
|
22 ScrollCanvas parent; |
|
23 |
|
24 public DummyCanvas(ScrollCanvas p) { |
|
25 parent=p; |
|
26 } |
|
27 |
|
28 public void paint(Graphics g) { |
|
29 parent.paint(g); |
|
30 } |
|
31 |
|
32 public void update(Graphics g) { |
|
33 parent.update(g); |
|
34 } |
|
35 } |
|
36 |
|
37 public class ScrollCanvas extends Panel |
|
38 { |
|
39 Scrollbar scrollv,scrollh; |
|
40 Canvas can; |
|
41 Image img=null; |
|
42 int offset_x=0,offset_y=0; |
|
43 int size_x=100,size_y=100; |
|
44 int old_x=0,old_y=0; |
|
45 |
|
46 public boolean handleEvent(Event evt) |
|
47 { |
|
48 evt.x+=offset_x; |
|
49 evt.y+=offset_y; |
|
50 if (evt.target instanceof Scrollbar) |
|
51 { |
|
52 offset_x=scrollh.getValue(); |
|
53 offset_y=scrollv.getValue(); |
|
54 Graphics g=can.getGraphics(); |
|
55 if (old_x!=size_x || old_y!=size_y) |
|
56 update(g); |
|
57 else { |
|
58 g.translate(-offset_x,-offset_y); |
|
59 g.drawImage(img,0,0,this); |
|
60 } |
|
61 return true; |
|
62 } |
|
63 else return super.handleEvent(evt); |
|
64 } |
|
65 |
|
66 public void adjust_scrollbars() |
|
67 { |
|
68 int viewport_v=can.size().height; |
|
69 int viewport_h=can.size().width; |
|
70 int value_v=scrollv.getValue(); |
|
71 int value_h=scrollh.getValue(); |
|
72 |
|
73 scrollv.setValues(value_v,viewport_v,0,size_y-viewport_v); |
|
74 scrollh.setValues(value_h,viewport_h,0,size_x-viewport_h); |
|
75 scrollv.setLineIncrement(viewport_v/20); |
|
76 scrollv.setPageIncrement(viewport_v); |
|
77 scrollh.setLineIncrement(viewport_h/20); |
|
78 scrollh.setPageIncrement(viewport_h); |
|
79 offset_x=scrollh.getValue(); |
|
80 offset_y=scrollv.getValue(); |
|
81 } |
|
82 |
|
83 public void set_size(int x,int y) |
|
84 { |
|
85 size_x=x;size_y=y; |
|
86 adjust_scrollbars(); |
|
87 } |
|
88 |
|
89 public void focus_to(int x,int y) { |
|
90 offset_x=Math.min(scrollh.getMaximum(),Math.max(0,x-can.size().width/2)); |
|
91 offset_y=Math.min(scrollv.getMaximum(),Math.max(0,y-can.size().height/2)); |
|
92 |
|
93 scrollh.setValue(offset_x); |
|
94 scrollv.setValue(offset_y); |
|
95 Graphics g=can.getGraphics(); |
|
96 if (old_x!=size_x || old_y!=size_y) |
|
97 update(g); |
|
98 else { |
|
99 g.translate(-offset_x,-offset_y); |
|
100 g.drawImage(img,0,0,this); |
|
101 } |
|
102 } |
|
103 |
|
104 public void update(Graphics g) { |
|
105 g=can.getGraphics(); |
|
106 int viewport_v=can.size().height; |
|
107 int viewport_h=can.size().width; |
|
108 int img_x=Math.max(viewport_h,size_x+1); |
|
109 int img_y=Math.max(viewport_v,size_y+1); |
|
110 |
|
111 old_x=size_x;old_y=size_y; |
|
112 img=createImage(img_x,img_y); |
|
113 Graphics g2=img.getGraphics(); |
|
114 g2.setColor(Color.lightGray); |
|
115 g2.fillRect(0,0,img_x,img_y); |
|
116 g2.setColor(Color.black); |
|
117 paintCanvas(g2); |
|
118 adjust_scrollbars(); |
|
119 g.translate(-offset_x,-offset_y); |
|
120 g.drawImage(img,0,0,this); |
|
121 } |
|
122 |
|
123 public void paint(Graphics g) |
|
124 { |
|
125 if (img!=null && old_x==size_x && old_y==size_y) { |
|
126 g=can.getGraphics(); |
|
127 adjust_scrollbars(); |
|
128 g.translate(-offset_x,-offset_y); |
|
129 g.drawImage(img,0,0,this); |
|
130 } else update(g); |
|
131 } |
|
132 |
|
133 public Graphics getGraphics() { |
|
134 Graphics g=can.getGraphics(); |
|
135 |
|
136 g.translate(-offset_x,-offset_y); |
|
137 return g; |
|
138 } |
|
139 |
|
140 public void paintCanvas(Graphics g) |
|
141 {} |
|
142 |
|
143 public ScrollCanvas() |
|
144 { |
|
145 can=new DummyCanvas(this); |
|
146 GridBagLayout gridbag = new GridBagLayout(); |
|
147 GridBagConstraints c = new GridBagConstraints(); |
|
148 scrollv=new Scrollbar(Scrollbar.VERTICAL); |
|
149 scrollh=new Scrollbar(Scrollbar.HORIZONTAL); |
|
150 |
|
151 setLayout(gridbag); |
|
152 c.weightx = 1.0; |
|
153 c.weighty = 1.0; |
|
154 c.gridwidth=1; |
|
155 c.fill = GridBagConstraints.BOTH; |
|
156 gridbag.setConstraints(can,c); |
|
157 add(can); |
|
158 c.weightx=0; |
|
159 c.weighty=0; |
|
160 c.gridwidth = GridBagConstraints.REMAINDER; |
|
161 c.fill = GridBagConstraints.VERTICAL; |
|
162 gridbag.setConstraints(scrollv,c); |
|
163 add(scrollv); |
|
164 c.gridwidth = 1; |
|
165 c.fill = GridBagConstraints.HORIZONTAL; |
|
166 gridbag.setConstraints(scrollh,c); |
|
167 add(scrollh); |
|
168 } |
|
169 } |