001: /*
002: * $RCSfile: ScrollingImagePanel.java,v $
003: *
004: * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Use is subject to license terms.
007: *
008: * $Revision: 1.1 $
009: * $Date: 2005/02/11 04:57:59 $
010: * $State: Exp $
011: */
012: package javax.media.jai.widget;
013:
014: import java.awt.BorderLayout;
015: import java.awt.Cursor;
016: import java.awt.Dimension;
017: import java.awt.Panel;
018: import java.awt.Point;
019: import java.awt.Scrollbar;
020: import java.awt.event.AdjustmentEvent;
021: import java.awt.event.AdjustmentListener;
022: import java.awt.event.MouseEvent;
023: import java.awt.event.MouseListener;
024: import java.awt.event.MouseMotionListener;
025: import java.awt.image.RenderedImage;
026: import java.util.Vector;
027:
028: import java.awt.ScrollPane;
029: import java.awt.event.ComponentEvent;
030: import java.awt.event.ComponentListener;
031: import java.awt.Container;
032: import java.awt.peer.ScrollPanePeer;
033:
034: /**
035: * An extension of java.awt.Panel that contains an ImageCanvas and
036: * vertical and horizontal scrollbars. The origin of the ImageCanvas
037: * is set according to the value of the scrollbars. Additionally, the
038: * origin may be changed by dragging the mouse. The window cursor
039: * will be changed to Cursor.MOVE_CURSOR for the duration of the
040: * drag.
041: *
042: * <p> Due to the limitations of BufferedImage, only TYPE_BYTE of band
043: * 1, 2, 3, 4, and TYPE_USHORT of band 1, 2, 3 images can be displayed
044: * using this widget.
045: *
046: *
047: * <p>
048: * This class has been deprecated. The source
049: * code has been moved to the samples/widget
050: * directory. These widgets are no longer
051: * supported.
052: *
053: * @deprecated as of JAI 1.1
054: */
055:
056: public class ScrollingImagePanel extends ScrollPane implements
057: AdjustmentListener, ComponentListener, MouseListener,
058: MouseMotionListener {
059:
060: /** The ImageCanvas we are controlling. */
061: protected ImageCanvas ic;
062: /** The RenderedImage displayed by the ImageCanvas. */
063: protected RenderedImage im;
064: /** The width of the panel. */
065: protected int panelWidth;
066: /** The height of the panel. */
067: protected int panelHeight;
068: /** Vector of ViewportListeners. */
069: protected Vector viewportListeners = new Vector();
070:
071: /**
072: * Constructs a ScrollingImagePanel of a given size for a
073: * given RenderedImage.
074: */
075: public ScrollingImagePanel(RenderedImage im, int width, int height) {
076: super ();
077: this .im = im;
078: this .panelWidth = width;
079: this .panelHeight = height;
080:
081: ic = new ImageCanvas(im);
082:
083: getHAdjustable().addAdjustmentListener(this );
084: getVAdjustable().addAdjustmentListener(this );
085:
086: super .setSize(width, height);
087: addComponentListener(this );
088: add("Center", ic);
089: }
090:
091: /** Adds the specified ViewportListener to the panel */
092: public void addViewportListener(ViewportListener l) {
093: viewportListeners.addElement(l);
094: l.setViewport(getXOrigin(), getYOrigin(), panelWidth,
095: panelHeight);
096: }
097:
098: /** Removes the specified ViewportListener */
099: public void removeViewportListener(ViewportListener l) {
100: viewportListeners.removeElement(l);
101: }
102:
103: private void notifyViewportListeners(int x, int y, int w, int h) {
104: int i;
105: int numListeners = viewportListeners.size();
106: for (i = 0; i < numListeners; i++) {
107: ViewportListener l = (ViewportListener) (viewportListeners
108: .elementAt(i));
109: l.setViewport(x, y, w, h);
110: }
111: }
112:
113: /**
114: * Returns the image canvas. Allows mouse listeners
115: * to be used on the image canvas.
116: *
117: * @since JAI 1.1
118: */
119: public ImageCanvas getImageCanvas() {
120: return ic;
121: }
122:
123: /** Returns the XOrigin of the image */
124: public int getXOrigin() {
125: return ic.getXOrigin();
126: }
127:
128: /** Returns the YOrigin of the image */
129: public int getYOrigin() {
130: return ic.getYOrigin();
131: }
132:
133: /**
134: * Sets the image origin to a given (x, y) position.
135: * The scrollbars are updated appropriately.
136: */
137: public void setOrigin(int x, int y) {
138: ic.setOrigin(x, y);
139: notifyViewportListeners(x, y, panelWidth, panelHeight);
140: }
141:
142: /**
143: * Set the center of the image to the given coordinates
144: * of the scroll window.
145: */
146: public synchronized void setCenter(int x, int y) {
147:
148: // scrollbar position
149: int sx = 0;
150: int sy = 0;
151:
152: // bounds
153: int iw = im.getWidth();
154: int ih = im.getHeight();
155: int vw = getViewportSize().width;
156: int vh = getViewportSize().height;
157:
158: // scrollbar lengths (proportional indicator)
159: int fx = getHAdjustable().getBlockIncrement();
160: int fy = getVAdjustable().getBlockIncrement();
161:
162: if (x < (vw - iw / 2)) {
163: sx = 0;
164: } else if (x > iw / 2) {
165: sx = iw - vw;
166: } else {
167: sx = x + (iw - vw - fx) / 2;
168: }
169:
170: if (y < (vh - ih / 2)) {
171: sy = 0;
172: } else if (y > ih / 2) {
173: sy = ih - vh;
174: } else {
175: sy = y + (ih - vh - fy) / 2;
176: }
177:
178: getHAdjustable().setValue(sx);
179: getVAdjustable().setValue(sy);
180:
181: notifyViewportListeners(getXOrigin(), getYOrigin(), panelWidth,
182: panelHeight);
183: }
184:
185: /** Sets the panel to display the specified image */
186: public void set(RenderedImage im) {
187: this .im = im;
188: ic.set(im);
189: }
190:
191: /** Returns the X co-ordinate of the image center. */
192: public int getXCenter() {
193: return getXOrigin() + panelWidth / 2;
194: }
195:
196: /** Returns the Y co-ordinate of the image center. */
197: public int getYCenter() {
198: return getYOrigin() + panelHeight / 2;
199: }
200:
201: /** Called by the AWT when instantiating the component. */
202: public Dimension getPreferredSize() {
203: return new Dimension(panelWidth, panelHeight);
204: }
205:
206: /** Called by the AWT during instantiation and
207: * when events such as resize occur.
208: */
209: public void setBounds(int x, int y, int width, int height) {
210:
211: // must set this first
212: super .setBounds(x, y, width, height);
213:
214: int vpw = getViewportSize().width;
215: int vph = getViewportSize().height;
216: int imw = im.getWidth();
217: int imh = im.getHeight();
218:
219: if (vpw >= imw && vph >= imh) {
220: ic.setBounds(x, y, width, height);
221: } else {
222: // BUG
223: // This fixes bad image positions during resize
224: // but breaks tiles (causes them all to load)
225: // Somehow the graphics context clip area gets
226: // changed in the ImageCanvas update(g) call.
227: // This occurs when the image size is greater
228: // than the viewport when the display first
229: // comes up. Removing the repaint in the
230: // ImageCanvas fixes this, but breaks ImageCanvas.
231: // Should have a new ImageCanvas call that sets
232: // the origin without a repaint.
233:
234: /* causes all tiles to be loaded, breaks scrolling, fixes resize */
235: //setOrigin(0, 0);
236: /* causes image to shift incorrectly on resize event */
237: ic.setBounds(x, y, vpw, vph);
238: }
239:
240: this .panelWidth = width;
241: this .panelHeight = height;
242: }
243:
244: /** Called by the AWT when either scrollbar changes. */
245: public void adjustmentValueChanged(AdjustmentEvent e) {
246: }
247:
248: /// ComponentListener Interface
249:
250: /** Called when the ImagePanel is resized */
251: public void componentResized(ComponentEvent e) {
252: notifyViewportListeners(getXOrigin(), getYOrigin(), panelWidth,
253: panelHeight);
254: }
255:
256: /** Ignored */
257: public void componentHidden(ComponentEvent e) {
258: }
259:
260: /** Ignored */
261: public void componentMoved(ComponentEvent e) {
262: }
263:
264: /** Ignored */
265: public void componentShown(ComponentEvent e) {
266: }
267:
268: //
269: // Mouse drag interface
270: //
271:
272: /** The initial Point of a mouse drag. */
273: protected Point moveSource;
274: /** True if we are in the middle of a mouse drag. */
275: protected boolean beingDragged = false;
276: /** A place to save the cursor. */
277: protected Cursor defaultCursor = null;
278:
279: /** Called at the beginning of a mouse drag. */
280: private synchronized void startDrag(Point p) {
281: this .setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
282: beingDragged = true;
283: moveSource = p;
284: }
285:
286: /** Called for each point of a mouse drag. */
287: protected synchronized void updateDrag(Point moveTarget) {
288: if (beingDragged) {
289: int dx = moveSource.x - moveTarget.x;
290: int dy = moveSource.y - moveTarget.y;
291: moveSource = moveTarget;
292:
293: int x = getHAdjustable().getValue() + dx;
294: int y = getVAdjustable().getValue() + dy;
295: setOrigin(x, y);
296: }
297: }
298:
299: /** Called at the end of a mouse drag. */
300: private synchronized void endDrag() {
301: this .setCursor(Cursor
302: .getPredefinedCursor(Cursor.DEFAULT_CURSOR));
303: beingDragged = false;
304: }
305:
306: /** Called by the AWT when the mouse button is pressed. */
307: public void mousePressed(MouseEvent me) {
308: startDrag(me.getPoint());
309: }
310:
311: /** Called by the AWT as the mouse is dragged. */
312: public void mouseDragged(MouseEvent me) {
313: updateDrag(me.getPoint());
314: }
315:
316: /** Called by the AWT when the mouse button is released. */
317: public void mouseReleased(MouseEvent me) {
318: endDrag();
319: }
320:
321: /** Called by the AWT when the mouse leaves the component. */
322: public void mouseExited(MouseEvent me) {
323: endDrag();
324: }
325:
326: /** Ignored. */
327: public void mouseClicked(MouseEvent me) {
328: }
329:
330: /** Ignored. */
331: public void mouseMoved(MouseEvent me) {
332: }
333:
334: /** Ignored. */
335: public void mouseEntered(MouseEvent me) {
336: }
337: }
|