📄 imgscrollpane.java
字号:
// Now redo the layout doLayout(); } /** * Scrolls to the specified position within the image. Specifying a * position outside of the legal scrolling bounds of the image will scroll * to the closest legal position. This is a convenience method which * interfaces with the Adjustable objects which represent the state of the * scrollbars. * * @param x the x position to scroll to * * @param y the y position to scroll to * */ public synchronized void setScrollPosition(int x, int y) { hsbar.setValueI(x); vsbar.setValueI(y); // Check if we need to repaint x = hsbar.getValue(); // get the actual value for check y = vsbar.getValue(); // get the actual value for check if (imgDisplay.lastUpdateOffset != null && imgDisplay.lastUpdateOffset.x == x && imgDisplay.lastUpdateOffset.y == y) { return; // No change } // New value changes from last drawn => repaint imgDisplay.repaint(); } /** * Scrolls to the specified position within the image. Specifying a * position outside of the legal scrolling bounds of the image will scroll * to the closest legal position. This is a convenience method which * interfaces with the Adjustable objects which represent the state of the * scrollbars. * * @param p the position to scroll to * */ public synchronized void setScrollPosition(Point p) { setScrollPosition(p.x,p.y); } /** * Returns the current x,y position within the child which is displayed at * the 0,0 location of the scrolled panel's view port. This is a * convenience method which interfaces with the adjustable objects which * represent the state of the scrollbars. * * @return the coordinate position for the current scroll position * */ public Point getScrollPosition() { return new Point(hsbar.getValue(),vsbar.getValue()); } /** * Returns the current size of the image scroll pane's view port. This is * the size of the image display area. If this component has not been * layed out yet the value is not defined. * * @return The size of the image display area * */ public Dimension getViewportSize() { return imgDisplay.getSize(); } /** * Sets if the scrolling is to be done by copying and redrawing of damaged * parts of the displayed image. Otherwise it is done by redrawing the * entire displayed image. In general copy scrolling is faster and * produces less annoying effects. See the class description. * * @param v If true scrolling will be done by copying. * */ public synchronized void setCopyScroll(boolean v) { copyScroll = v; } /** * Returns true if the scrolling is done by copying. * * @return If the copy is done by scrolling * */ public synchronized boolean getCopyScroll() { return copyScroll; } /** * Causes this container to lay out its components. Most programs should * not call this method directly, but should invoke the validate method * instead. * */ public synchronized void doLayout() { // Let's see if we should include the scrollbars or not if (sbType == SCROLLBARS_AS_NEEDED && imgDisplay.calcDim()) { Dimension sz = getSize(); Dimension imsz = imgDisplay.getPreferredSize(); if (sz.width>=imsz.width+2*INTERNAL_GAP) { if (sz.height>=imsz.height+2*INTERNAL_GAP) { // We don't need scrollbars hsbar.setVisible(false); vsbar.setVisible(false); } else { // We need at least the vertical one, check again for the // horizontal. vsbar.setVisible(true); if (sz.width >= imsz.width+3*INTERNAL_GAP+SCROLLBAR_THICKNESS) { hsbar.setVisible(false); } else { hsbar.setVisible(true); } } } else { // We need at least the horizontal, check for the vertical // one. hsbar.setVisible(true); if (sz.height >= imsz.height+3*INTERNAL_GAP+SCROLLBAR_THICKNESS) { vsbar.setVisible(false); } else { vsbar.setVisible(true); } } } // Indicate that we are erasing the image (the doLayout() will erase) imgDisplay.erase = true; // Now do the layout super.doLayout(); // Trick the lower scrollbar: if both scrollbars are showing then // shorten the horizontal one so that the traditional empty square // appears at the lower right corner. This is probably not the best // solution but it works. if (hsbar.isVisible() && vsbar.isVisible()) { Rectangle b = hsbar.getBounds(); if (b.width > SCROLLBAR_THICKNESS+INTERNAL_GAP) { b.width -= SCROLLBAR_THICKNESS+INTERNAL_GAP; } hsbar.setBounds(b); } // We need to calculate the scrollbars with the possibly new size setScrollbars(); } /** * Adds the specified focus listener to receive focus events from this * component. It is added to the image and scrollbar areas. * * @param l the focus listener * */ public synchronized void addFocusListener(FocusListener l) { super.addFocusListener(l); imgDisplay.addFocusListener(l); hsbar.addFocusListener(l); vsbar.addFocusListener(l); } /** * Removes the specified focus listener so that it no longer receives * focus events from this component. * * @param l the focus listener * */ public synchronized void removeFocusListener(FocusListener l) { super.removeFocusListener(l); imgDisplay.removeFocusListener(l); hsbar.removeFocusListener(l); vsbar.removeFocusListener(l); } /** * Adds the specified key listener to receive key events from this * component. It is added to the image and scrollbar areas. * * @param l the key listener * */ public synchronized void addKeyListener(KeyListener l) { super.addKeyListener(l); imgDisplay.addKeyListener(l); hsbar.addKeyListener(l); vsbar.addKeyListener(l); } /** * Removes the specified key listener so that it no longer receives key * events from this component. * * @param l the key listener * */ public synchronized void removeKeyListener(KeyListener l) { super.removeKeyListener(l); imgDisplay.removeKeyListener(l); hsbar.removeKeyListener(l); vsbar.removeKeyListener(l); } /** * Adds the specified mouse listener to receive mouse events from this * component. It is actually added to the image area only and not to the * scrollbar areas. * * @param l the mouse listener * */ public synchronized void addMouseListener(MouseListener l) { super.addMouseListener(l); imgDisplay.addMouseListener(l); } /** * Removes the specified mouse listener so that it no longer receives * mouse events from this component. * * @param l the mouse listener * */ public synchronized void removeMouseListener(MouseListener l) { super.removeMouseListener(l); imgDisplay.removeMouseListener(l); } /** * Adds the specified mouse motion listener to receive mouse motion events * from this component. It is actually added to the image area only and * not to the scrollbar areas. * * @param l the mouse motion listener * */ public synchronized void addMouseMotionListener(MouseMotionListener l) { super.addMouseMotionListener(l); imgDisplay.addMouseMotionListener(l); } /** * Removes the specified mouse motion listener so that it no longer * receives mouse motion events from this component. * * @param l the mouse motion listener * */ public synchronized void removeMouseMotionListener(MouseMotionListener l) { super.removeMouseMotionListener(l); imgDisplay.removeMouseMotionListener(l); } /** * Sets the background color of this component. It sets the background of * the 3 areas (image and scrollbars) plus the container itself. * * @param c The color to become background color for this component * */ public synchronized void setBackground(Color c) { super.setBackground(c); imgDisplay.setBackground(c); hsbar.setBackground(c); vsbar.setBackground(c); } /** * Set the cursor image to a predefined cursor. It sets the cursor of the * image area and this container to the specified one. It does not set the * cursor of the scrollbars. * * @param cursor One of the constants defined by the Cursor class. * */ public synchronized void setCursor(Cursor cursor) { super.setCursor(cursor); imgDisplay.setCursor(cursor); } /** * Enables or disables this component, depending on the value of the * parameter b. An enabled component can respond to user input and * generate events. Components are enabled initially by default. * * @param b If true, this component is enabled; otherwise this component * is disabled. * */ public synchronized void setEnabled(boolean b) { super.setEnabled(b); imgDisplay.setEnabled(b); hsbar.setEnabled(b); vsbar.setEnabled(b); } /** * Sets the foreground color of this component. It sets the foreground of * the 3 areas (image display and scrollbars) plus this contaioner's * foreground. * * @param c The color to become this component's foreground color. * */ public synchronized void setForeground(Color c) { super.setForeground(c); imgDisplay.setForeground(c); hsbar.setForeground(c); vsbar.setForeground(c); } /** * Throws an IllegalArgumentException since no components can be added to * this container. * */ public Component add(Component comp) { throw new IllegalArgumentException(); } /** * Throws an IllegalArgumentException since no components can be added to * this container. * */ public Component add(String name,Component comp) { throw new IllegalArgumentException(); } /** * Throws an IllegalArgumentException since no components can be added to * this container. * */ public Component add(Component comp, int index) { throw new IllegalArgumentException(); } /** * Throws an IllegalArgumentException since no components can be added to * this container. * */ public void add(Component comp, Object constraints) { throw new IllegalArgumentException(); } /** * Throws an IllegalArgumentException since no components can be added to * this container. * */ public void add(Component comp, Object constraints, int index) { throw new IllegalArgumentException(); } /** * Throws an IllegalArgumentException since the components should never be * removed from this container. * */ public void remove(int index) { throw new IllegalArgumentException(); } /** * Throws an IllegalArgumentException since the components should never be * removed from this container.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -