/*============================================================================= | | NAME | | ScrollableImage.java | | DESCRIPTION | | Image contained within a scroll panel. | | | LEGAL | | Prentice Version 1.1 - An Artist's Software Apprentice. | Copyright (C) 2003-2008 by Sean Erik O'Connor. All Rights Reserved. | | This program is free software; you can redistribute it and/or | modify it under the terms of the GNU General Public License | as published by the Free Software Foundation; version 2 | of the License. | | This program is distributed in the hope that it will be useful, | but WITHOUT ANY WARRANTY; without even the implied warranty of | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | GNU General Public License for more details. | | You should have received a copy of the GNU General Public License | along with this program; if not, write to the Free Software | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | | The author's address is artifex@seanerikoconnor.freeservers.com. | +============================================================================*/ package View ; // Package name for this project. import java.awt.* ; // AWT basic window handling. import java.awt.event.* ; // AWT basic event handling. import java.awt.geom.* ; // 2D graphics. import java.awt.image.* ; // Images and operations. import javax.swing.* ; // Swing main GUI package. import javax.swing.event.* ; // Swing GUI event handling. import Prentice.* ; // Prentice stuff. import Model.* ; // Panel for custom image painting, // scrollable interface for a scroll-savy client. public class ScrollableImage extends JPanel implements Scrollable { private Prentice app ; private PrenticeCanvas renderedImage ; private MouseHandler mouseHandler ; // Scrollable image. public ScrollableImage( Prentice app ) { this.app = app ; DebugLog.println( "Entering ScrollableImage" ) ; DebugLog.println( " app = " + app ) ; // Get image to be rendered. renderedImage = app.getCanvas() ; // Generate synthetic mouse drag events when user drags // outside this pane and stops. setAutoscrolls( true ) ; // Create listener for the mouse. mouseHandler = new MouseHandler( app ) ; // Listen for both mouse button and motion events. addMouseListener( mouseHandler ) ; addMouseMotionListener( mouseHandler ) ; } ; // Update this component. public void update() { repaint() ; } // Handle all custom painting here. public void paintComponent( Graphics g ) { super.paintComponent( g ) ; // Paint the background. // Get access to the 2D graphics functions. Graphics2D g2d = (Graphics2D) g ; // Antialias image and render at high quality. g2d.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON ) ; g2d.setRenderingHint( RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY ) ; // Update the image first. app.getCanvas().repaint() ; // Fast copy the image to the screen. g2d.drawImage( app.getCanvas(), 0, 0, this ) ; // Get the clipping rectangle. Rectangle clipRect = g.getClipBounds() ; // Show clipping rectangle, if any. if (clipRect != null) { DebugLog.println( "clipping rectangle = " + clipRect ) ; //g2d.setColor( Color.yellow ) ; //g.drawRect( clipRect.x, clipRect.y, // clipRect.width - 1, clipRect.height - 1); } // Rubber band line painting. g2d.setColor( Color.green ) ; Line2D.Double l = mouseHandler.getLine() ; DebugLog.println( "line = " + l ) ; g2d.draw( l ) ; // Border (if any). Insets insets = getInsets() ; int w = getWidth() - insets.left - insets.right ; int h = getHeight() - insets.top - insets.bottom ; DebugLog.println( "Insets = " + insets ) ; DebugLog.println( " w = " + w ) ; DebugLog.println( " w = " + h ) ; DebugLog.println( "Repainting Scrollable image" ) ; } // Scroll one increment upon scroll bar button click. // Windows XP mouse wheel scrolls 3 unit increments. public int getScrollableUnitIncrement( Rectangle visibleRect, int orientation, int direction ) { //Get the current position. int currentPosition = 0; if (orientation == SwingConstants.HORIZONTAL) currentPosition = visibleRect.x; else currentPosition = visibleRect.y; DebugLog.println( "Unit scroll " + orientation + " to position " + currentPosition ) ; if (direction < 0) return 1 ; else return 1 ; } public int getScrollableBlockIncrement( Rectangle visibleRect, int orientation, int direction ) { // Get the current position. int currentPosition = 0; if (orientation == SwingConstants.HORIZONTAL) currentPosition = visibleRect.x; else currentPosition = visibleRect.y; DebugLog.println( "Block scroll " + orientation + " to position " + currentPosition ) ; return 10 ; } public Dimension getPreferredScrollableViewportSize() { return new Dimension( 1280, 1024 ) ; } // Don't force client to be same size as viewport. public boolean getScrollableTracksViewportHeight() { return false ; } // Don't force client to be same size as viewport. public boolean getScrollableTracksViewportWidth() { return false ; } } // end class