Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package hu.kazocsaba.imageviewer;
  
  import java.awt.Dimension;
  import java.awt.Point;
A general purpose image viewer component. It contains a scroll pane and manages the size of the image in accordance with the resize strategy property. It can also provide a popup menu containing generally useful viewing controls and functions; this default popup can be disabled by passing defaultPopupMenu = null to the constructor.

The Swing component that can be added to the GUI is obtained by calling getComponent().

ImageViewer supports status bars: arbitrary components that can be added to the viewer and are displayed below the image.

Overlays can also be added to the viewer; for details, see the the documentation of the Overlay class.

Author(s):
Kazó Csaba
See also:
StatusBar
Overlay
 
 public final class ImageViewer {
 	private final LayeredImageView view;
 	private final JScrollPane scroller;
 	private JPanel panel;
 	private StatusBar statusBar;
 	private boolean statusBarVisible=false;
Creates a new image viewer. Initially it will be empty, and it will have a default popup menu.
 
 	public ImageViewer() {
 		this(null);
 	}
Creates a new image viewer displaying the specified image. TThe viewer will have a default popup menu.

Parameters:
image the image to display; if null then no image is displayed
 
 	public ImageViewer(BufferedImage image) {
 		this(imagetrue);
 	}
Creates a new image viewer displaying the specified image.

Parameters:
image the image to display; if null then no image is displayed
defaultPopupMenu if true, then a default popup menu will be created and registered for the viewer
See also:
setImage(java.awt.image.BufferedImage)
 
 	public ImageViewer(BufferedImage imageboolean defaultPopupMenu) {
 		=new JPanel(new BorderLayout());
 		=new JScrollPane() {
 
 			protected JViewport createViewport() {
 				return new JViewport() {
 
 						return new CustomViewportLayout(ImageViewer.this);
 					}
 					
 					public Dimension getMaximumSize() {
 					}
 					
 				};
 			}
 			
 			public boolean isValidateRoot() {
 				return false;
 			}
 			
 		};
		if (defaultPopupMenu) {
				private void showPopup(MouseEvent e) {
					if (==null=new DefaultViewerPopup(ImageViewer.this);
					if (p == null) {
						p = e.getPoint();
					}
					.show(e.getComponent(), p.xp.y);
				}
				public void mousePressed(MouseEvent e) {
					if (e.isPopupTrigger()) {
					}
				}
				public void mouseReleased(MouseEvent e) {
					if (e.isPopupTrigger()) {
					}
				}
			});
		}
	}
Sets the status bar component for this image viewer. The new status bar is made visible only if the statusBarVisible property is true. If statusBar is null this method removes any existing status bar.

Parameters:
statusBar the new status bar component to set
Throws:
java.lang.IllegalArgumentException if the status bar has already been added to a different image viewer
	public void setStatusBar(StatusBar statusBar) {
		if (statusBar==this.return;
		if (statusBar.getImageViewer()!=null)
			throw new IllegalArgumentException("Status bar already added to an image viewer");
		StatusBar oldStatusBar=this.;
		if (oldStatusBar!=null) {
			.remove(oldStatusBar.getComponent());
			oldStatusBar.setImageViewer(null);
		}
		this.=statusBar;
		if (this.!=null) {
			}
		}
		.firePropertyChange("statusBar"oldStatusBarstatusBar);
	}
Returns the status bar currently associated with this viewer.

Returns:
the current status bar, or null if the viewer has no status bar
	public StatusBar getStatusBar() {
		return ;
	}
Sets whether the status bar is visible. The status bar is hidden by default.

Parameters:
statusBarVisible true, if the status bar should be visible; false otherwise
	public void setStatusBarVisible(boolean statusBarVisible) {
		if (this. == statusBarVisiblereturn;
		if (!=null) {
			if (statusBarVisible)
			else
		}
		boolean prev = this.;
		this. = statusBarVisible;
		.firePropertyChange("statusBarVisible"prevstatusBarVisible);
	}
Returns whether the status bar is set to be visible. The status bar is hidden by default.

Returns:
the statusBarVisible property
	public boolean isStatusBarVisible() {
	}
Returns the image viewer component that can be displayed.

Returns:
the image viewer component
		return ;
	}
Sets the image displayed by the viewer. If the argument is the same object as the image currently being displayed, then this method will trigger a refresh. If you modify the image shown by the viewer, use this function to notify the component and cause it to update.

Parameters:
image the new image to display; if null then no image is displayed
	public void setImage(BufferedImage image) {
	}
Returns the currently displayed image.

Returns:
the current image, or null if no image is displayed
	public BufferedImage getImage() {
		return .getImage();
	}
Sets the resize strategy this viewer should use. The default is ResizeStrategy.SHRINK_TO_FIT.

Parameters:
resizeStrategy the new resize strategy
	public void setResizeStrategy(ResizeStrategy resizeStrategy) {
		.setResizeStrategy(resizeStrategy);
	}
Returns the current resize strategy. The default is ResizeStrategy.SHRINK_TO_FIT.

Returns:
the current resize strategy
	}

Sets whether the image should be resized with nearest neighbor interpolation when it is expanded. The default is false.

Parameters:
pixelatedZoom the new value of the pixelatedZoom property
	public void setPixelatedZoom(boolean pixelatedZoom) {
		.setPixelatedZoom(pixelatedZoom);
	}
Returns the current pixelated zoom setting. The default is false.

Returns:
the current pixelated zoom setting
	public boolean isPixelatedZoom() {
	}

Returns the current interpolation type. The default is java.awt.RenderingHints.VALUE_INTERPOLATION_BICUBIC.

Returns:
the interpolation type
See also:
setInterpolationType(java.lang.Object)
	}

Sets the interpolation type to use when resizing images. See java.awt.RenderingHints.KEY_INTERPOLATION for details. The default value is RenderingHints.VALUE_INTERPOLATION_BICUBIC.

The allowed values are:

Changing the interpolation type to bilinear or nearest neighbor improves painting performance when the image needs to be resized.

Note: when the pixelatedZoom property is set to true and the image is enlarged, then the nearest neighbor method is used regardless of this setting.

Parameters:
type the interpolation type to use when resizing images
Throws:
java.lang.IllegalArgumentException if the parameter is not one of the allowed values
	public void setInterpolationType(Object type) {
	}

Returns the zoom factor used when resize strategy is CUSTOM_ZOOM. The default value is 1.

Returns:
the custom zoom factor
	public double getZoomFactor() {
	}

Sets the zoom factor to use when the resize strategy is CUSTOM_ZOOM. The default value is 1.

Note that calling this function does not change the current resize strategy.

Parameters:
newZoomFactor the new zoom factor for the CUSTOM_ZOOM strategy
Throws:
java.lang.IllegalArgumentException if newZoomFactor is not a positive number
	public void setZoomFactor(double newZoomFactor) {
		.setZoomFactor(newZoomFactor);
	}
Returns the transformation that is applied to the image. Most commonly the transformation is the concatenation of a uniform scale and a translation.

The AffineTransform instance returned by this method should not be modified.

Returns:
the transformation applied to the image before painting
Throws:
java.lang.IllegalStateException if there is no image set or if the size of the viewer is 0 (for example because it is not in a visible component)
	}

Adds an overlay as the specified layer.

Parameters:
overlay the overlay to add
layer the layer to add the overlay to; higher layers are on top of lower layers; the image resides in layer 0
	public void addOverlay(Overlay overlayint layer) {
		.addOverlay(overlaylayer);
	}

Adds an overlay to layer 1.

Parameters:
overlay the overlay to add
	public void addOverlay(Overlay overlay) {
		addOverlay(overlay, 1);
	}
Removes an overlay from the image viewer.

Parameters:
overlay the overlay to remove
Throws:
java.lang.IllegalArgumentException if the overlay is not in the image viewer
	public void removeOverlay(Overlay overlay) {
	}
Adds the specified mouse listener to receive mouse events from the image component of this image viewer. If listener l is null, no exception is thrown and no action is performed.

Parameters:
l the mouse listener
	public void addMouseListener(MouseListener l) {
	}
Removes the specified mouse listener so that it no longer receives mouse motion events from the image component of this image viewer. This method performs no function, nor does it throw an exception, if the listener specified by the argument was not previously added to this component. If listener l is null, no exception is thrown and no action is performed.

Parameters:
l the mouse motion listener
	}
Adds the specified mouse motion listener to receive mouse events from the image component of this image viewer. If listener l is null, no exception is thrown and no action is performed.

Parameters:
l the mouse listener
	}
Removes the specified mouse motion listener so that it no longer receives mouse motion events from the image component of this image viewer. This method performs no function, nor does it throw an exception, if the listener specified by the argument was not previously added to this component. If listener l is null, no exception is thrown and no action is performed.

Parameters:
l the mouse motion listener
	}
Adds the specified image mouse motion listener to this viewer. The listener is notified as the mouse moves over pixels of the image. If listener l is null, no exception is thrown and no action is performed.

Parameters:
l the image mouse motion listener
	}

Removes the specified image mouse motion listener so that it no longer receives mouse motion events from the image component of this image viewer. This method performs no function, nor does it throw an exception, if the listener specified by the argument was not previously added to this component. If listener l is null, no exception is thrown and no action is performed.

Parameters:
l the mouse motion listener
	}

Adds the specified image mouse listener to this viewer. The listener is notified as mouse buttons are clicked over pixels of the image. If listener l is null, no exception is thrown and no action is performed.

Parameters:
l the image mouse motion listener
	}

Removes the specified image mouse listener so that it no longer receives mouse click events from the image component of this image viewer. This method performs no function, nor does it throw an exception, if the listener specified by the argument was not previously added to this component. If the listener l is null, no exception is thrown and no action is performed.

Parameters:
l the mouse motion listener
	}

Adds a PropertyChangeListener to the listener list. The same listener object may be added more than once, and will be called as many times as it is added. If the listener is null, no exception is thrown and no action is taken.

Parameters:
l the listener to be added
	}

Remove a PropertyChangeListener from the listener list. This removes a listener that was registered for all properties. If the listener was added more than once, it will be notified one less time after being removed. If the listener is null, or was never added, no exception is thrown and no action is taken.

Parameters:
l the listener to remove
	}

Adds a PropertyChangeListener for a specific property. The listener will be invoked only when a call on firePropertyChange names that specific property. The same listener object may be added more than once. For each property, the listener will be invoked the number of times it was added for that property. If the property name or the listener is null, no exception is thrown and no action is taken.

Parameters:
name the name of the property to listen on
l the listener to add
	}

Remove a PropertyChangeListener from the listener list. This removes a PropertyChangeListener that was registered for all properties. If the listener was added more than once, it will be notified one less time after being removed. If the listener is null, or was never added, no exception is thrown and no action is taken.

Parameters:
name the name of the property that was listened on
l the listener to remove
	}

Returns the scroll pane of the image viewer.

Returns:
the scroll pane
		return ;
	}
		return ;
	}
	void setSynchronizer(Synchronizer newSync) {
		=newSync;
	}
Returns the image pixel corresponding to the given point. If the clipToImage parameter is false, then the function will return an appropriately positioned pixel on an infinite plane, even if the point is outside the image bounds. If clipToImage is true then the function will return null for such positions, and any non-null return value will be a valid image pixel.

Parameters:
p a point in component coordinate system
clipToImage whether the function should return null for positions outside the image bounds
Returns:
the corresponding image pixel
Throws:
java.lang.IllegalStateException if there is no image set or if the size of the viewer is 0 (for example because it is not in a visible component)
	public Point pointToPixel(Point pboolean clipToImage) {
		return .pointToPixel(pclipToImage);
	}
New to GrepCode? Check out our FAQ X