edu.mines.jtk.mosaic
Class IPanel

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by javax.swing.JComponent
              extended by javax.swing.JPanel
                  extended by edu.mines.jtk.mosaic.IPanel
All Implemented Interfaces:
java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable, javax.accessibility.Accessible
Direct Known Subclasses:
ColorBar, Mosaic, PlotPanel, Tile, TileAxis

public class IPanel
extends javax.swing.JPanel

A panel that can paint itself to fit an image. Some components in this package, such as mosaics, tiles, and tile axes, must be able to render themselves to images as well as on screen. For various reasons, those images often have resolution that is higher than that of a display screen. Simply scaling an on-screen rendering does not exploit this higher resolution, because screen coordinates are typically specified as integers. Rounding to the nearest integer screen coordinates and then scaling to a high resolution image yields visual artifacts, such as curves that appear jagged in the image.

Classes that extend this base class work differently. They paint themselves to fit any specified rectangle of a specified graphics context. When painting to a display screen, that graphics rectangle is simply the panel's rectangle, in screen coordinates. However, when painting to an image, the dimensions of that rectangle may be much larger, corresponding to the higher resolution of the image. When painting, these panels round coordinates to the nearest pixel of that graphics rectangle, not the panel's on-screen rectangle. In this way, panels can paint themselves with any desired resolution.

One complication is font size. Another is line width. Such properties are typically specified in points, which are roughly equivalant to on-screen pixels. Therefore, when drawing to a high-resolution image, font sizes and line widths must be increased. This base class provides methods that panels in this package use to properly scale font sizes, line widths, and other resolution-dependent properties.

Version:
2005.12.21
Author:
Dave Hale, Colorado School of Mines
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class javax.swing.JPanel
javax.swing.JPanel.AccessibleJPanel
 
Nested classes/interfaces inherited from class javax.swing.JComponent
javax.swing.JComponent.AccessibleJComponent
 
Nested classes/interfaces inherited from class java.awt.Container
java.awt.Container.AccessibleAWTContainer
 
Nested classes/interfaces inherited from class java.awt.Component
java.awt.Component.AccessibleAWTComponent, java.awt.Component.BltBufferStrategy, java.awt.Component.FlipBufferStrategy
 
Field Summary
 
Fields inherited from class javax.swing.JComponent
accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
IPanel()
           
 
Method Summary
protected  double computeScale(int w, int h)
          Returns a scale factor for painting this panel to fit a rectangle.
protected  java.awt.Graphics2D createGraphics(java.awt.Graphics2D g2d, int x, int y, int w, int h)
          Creates a graphics context for the specified graphics rectangle.
protected  float getLineWidth(java.awt.Graphics2D g2d)
          Gets the line width for the specified graphics context.
 void paintToImage(java.awt.image.BufferedImage image)
          Paints this panel to fit the specified image.
 java.awt.image.BufferedImage paintToImage(int width)
          Paints this panel to fit a new image with specified width in pixels.
 void paintToPng(double dpi, double win, java.lang.String fileName)
          Paints this panel to a PNG image with specified resolution and width.
 void paintToRect(java.awt.Graphics2D g2d, int x, int y, int w, int h)
          Paints this panel to a specified rectangle of a graphics context.
protected  void scaleLineWidth(java.awt.Graphics2D g2d, double scale)
          Scales the line width for the specified graphics context.
 
Methods inherited from class javax.swing.JPanel
getAccessibleContext, getUI, getUIClassID, paramString, setUI, updateUI
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusBackward, transferFocusDownCycle, validate, validateTree
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

IPanel

public IPanel()
Method Detail

paintToRect

public void paintToRect(java.awt.Graphics2D g2d,
                        int x,
                        int y,
                        int w,
                        int h)
Paints this panel to a specified rectangle of a graphics context. This implementation simply paints any IPanel children of this panel. It ignores and does not draw any children that are not IPanels.

Classes that extend this base class typically override this method to draw something besides children of this panel. When appropriate, those extensions may also call this method.

Parameters:
g2d - the graphics context.
x - the x-coordinate of the graphics rectangle.
y - the y-coordinate of the graphics rectangle.
w - the width of the graphics rectangle.
h - the height of the graphics rectangle.

paintToImage

public void paintToImage(java.awt.image.BufferedImage image)
Paints this panel to fit the specified image.

Parameters:
image - the image.

paintToImage

public java.awt.image.BufferedImage paintToImage(int width)
Paints this panel to fit a new image with specified width in pixels. The image height is computed so that the image has the same aspect ratio as this panel.

Parameters:
width - the image width, in pixels.
Returns:
the image.

paintToPng

public void paintToPng(double dpi,
                       double win,
                       java.lang.String fileName)
                throws java.io.IOException
Paints this panel to a PNG image with specified resolution and width. The image height is computed so that the image has the same aspect ratio as this panel.

Parameters:
dpi - the image resolution, in dots per inch.
win - the image width, in inches.
fileName - the name of the file to contain the PNG image.
Throws:
java.io.IOException

computeScale

protected double computeScale(int w,
                              int h)
Returns a scale factor for painting this panel to fit a rectangle. The scale factor depends on the specified width and height and the corresponding width and height of this panel.

Parameters:
w - the rectangle width.
h - the rectangle height.
Returns:
the scale factor.

createGraphics

protected java.awt.Graphics2D createGraphics(java.awt.Graphics2D g2d,
                                             int x,
                                             int y,
                                             int w,
                                             int h)
Creates a graphics context for the specified graphics rectangle. First computes a scale factor from the dimensions of this panel and those of the specified graphics rectangle. Then (1) sets the clip rectangle using the specified graphics rectangle, (2) translates the coordinate system by the specified (x,y), (3) sets the line width, and (4) sets the font to be a scaled version of this panel's font.

Classes that extend this base class typically call this method in their implementation of paintToRect(java.awt.Graphics2D,int,int,int,int). When painting to a high-resolution image, this method makes lines and text appear as they would on screen, neither too thin nor too small.

When the returned graphics context is no longer needed, it should be disposed.

Parameters:
g2d - the graphics context.
x - the x-coordinate of the graphics rectangle.
y - the y-coordinate of the graphics rectangle.
w - the width of the graphics rectangle.
h - the height of the graphics rectangle.

getLineWidth

protected float getLineWidth(java.awt.Graphics2D g2d)
Gets the line width for the specified graphics context.

Parameters:
g2d - the graphics context.
Returns:
the line width.

scaleLineWidth

protected void scaleLineWidth(java.awt.Graphics2D g2d,
                              double scale)
Scales the line width for the specified graphics context. Classes that extend this base class should not assume that the current line width is one, because the line width may have been set to a larger value in any graphics context created by createGraphics(java.awt.Graphics2D,int,int,int,int).

Parameters:
g2d - the graphics context.
scale - the scale factor.