javalib.worldcanvas
Class CanvasPanel

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 javalib.worldcanvas.CanvasPanel
All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, Accessible

public class CanvasPanel
extends JPanel

A buffered panel to hold the drawings.

See Also:
Serialized Form

Nested Class Summary
protected static class CanvasPanel.Painter
          Panel that paints the internal buffered image that maintains the persistent graphics state of the buffered panel.
static class CanvasPanel.Refresh
          Class Refresh encapsulates methods for graphics refresh.
 
Nested classes/interfaces inherited from class javax.swing.JPanel
JPanel.AccessibleJPanel
 
Nested classes/interfaces inherited from class javax.swing.JComponent
JComponent.AccessibleJComponent
 
Nested classes/interfaces inherited from class java.awt.Container
Container.AccessibleAWTContainer
 
Nested classes/interfaces inherited from class java.awt.Component
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
 
Field Summary
protected  BufferedImage buffer
          The buffered image that maintains the persistent graphics state.
protected  int HEIGHT
           
protected  CanvasPanel.Painter painter
          The internal painter panel.
protected  int WIDTH
          The width and height for this buffered panel.
 
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, PROPERTIES, SOMEBITS
 
Constructor Summary
CanvasPanel(int width, int height)
          Constructs a BufferedPanel containing a buffered image with the given width and height, and the given background color or Paint for the image.
 
Method Summary
 void clearPanel()
          Fills this buffered panel with its background color or Paint.
 void drawImage(ImageMaker imread, int x, int y)
          Draw the image from the .png file that has been read by the given ImageMaker.
 void drawImage(String fileName, int x, int y)
          Draw the image from the given .png file at the specified location
 void drawImage(WorldImage image)
          Draw the given image into this panel
 void drawImagePixels(ImageMaker imread, int x, int y)
          Draw the image from the .png file that has been read by the given ImageMaker.
 BufferedImage getBuffer()
          Returns the internal buffered image for this panel.
 Graphics2D getBufferGraphics()
          Returns a Graphics2D object that permits painting to the internal buffered image for this panel.
 int getBufferHeight()
          Returns the height of the buffered image.
 int getBufferWidth()
          Returns the width of the buffered image.
 void getFonts()
           
 JPanel getInnerPanel()
          Returns the internal panel for this buffered panel, that is, the panel that paints the buffered image and handles the mouse and key adapters.
 boolean isFocusable()
          Overrides to delegate to the inner panel that is the panel returned by getInnerPanel().
 boolean isRequestFocusEnabled()
          Overrides to delegate to the inner panel that is the panel returned by getInnerPanel().
protected  boolean isWhite(Color c)
           
 void paintOver(Graphics2D g2)
          Override this paintOver method to add additional painting actions after the default buffer repaint is done during a repaint() call.
 void requestFocus()
          Overrides to delegate to the inner panel that is the panel returned by getInnerPanel().
 boolean requestFocusInWindow()
          Overrides to delegate to the inner panel that is the panel returned by getInnerPanel().
 void setBufferSize(int width, int height)
          Sets the size of the buffered image to the given height and width.
 void setFocusable(boolean focusable)
          Overrides to delegate to the inner panel that is the panel returned by getInnerPanel().
 
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, getBaseline, getBaselineResizeBehavior, 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, isPaintingForPrint, isPaintingTile, 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, 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, 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, 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
 

Field Detail

buffer

protected transient BufferedImage buffer
The buffered image that maintains the persistent graphics state.


HEIGHT

protected int HEIGHT

painter

protected transient CanvasPanel.Painter painter
The internal painter panel.


WIDTH

protected int WIDTH
The width and height for this buffered panel.

Constructor Detail

CanvasPanel

public CanvasPanel(int width,
                   int height)

Constructs a BufferedPanel containing a buffered image with the given width and height, and the given background color or Paint for the image.

If the given width or height is less than 1 pixel, that value is set to 1 pixel.

Though the component itself may grow arbitrarily large, the buffered image painted to the buffer will remain the size specified in this constructor unless the size is reset using the setBufferSize method.

Parameters:
width - the width of the buffered image
height - the height of the buffered image
Method Detail

clearPanel

public final void clearPanel()

Fills this buffered panel with its background color or Paint.


drawImage

public void drawImage(ImageMaker imread,
                      int x,
                      int y)
Draw the image from the .png file that has been read by the given ImageMaker.

Parameters:
imread - the given image maker
x - the x coordinate for the NW corner
y - the y coordinate for the NW corner

drawImage

public void drawImage(String fileName,
                      int x,
                      int y)
Draw the image from the given .png file at the specified location

Parameters:
fileName - the image filename
x - the x coordinate for the NW corner
y - the y coordinate for the NW corner

drawImage

public void drawImage(WorldImage image)
Draw the given image into this panel

Parameters:
image - the image to draw

drawImagePixels

public void drawImagePixels(ImageMaker imread,
                            int x,
                            int y)
Draw the image from the .png file that has been read by the given ImageMaker.

Parameters:
imread - the given image maker
x - the x coordinate for the NW corner
y - the y coordinate for the NW corner

getBuffer

public final BufferedImage getBuffer()

Returns the internal buffered image for this panel.


getBufferGraphics

public final Graphics2D getBufferGraphics()

Returns a Graphics2D object that permits painting to the internal buffered image for this panel.

The user should always use this object to paint to the buffer and thus indirectly modify this buffered panel.

To make painting changes to the buffer visible, the repaint() method must explicitly be called. This allows a number of painting operations to be done prior to screen repaint.


getBufferHeight

public final int getBufferHeight()
Returns the height of the buffered image.


getBufferWidth

public final int getBufferWidth()
Returns the width of the buffered image.


getFonts

public void getFonts()

getInnerPanel

public final JPanel getInnerPanel()

Returns the internal panel for this buffered panel, that is, the panel that paints the buffered image and handles the mouse and key adapters.

This panel may be used when access to the panel on which the graphics is drawn is needed.

Do not set a border on this internal panel. Set a border on the outer BufferedPanel object.

Since:
1.1

isFocusable

public final boolean isFocusable()

Overrides to delegate to the inner panel that is the panel returned by getInnerPanel().

Overrides:
isFocusable in class Component

isRequestFocusEnabled

public final boolean isRequestFocusEnabled()

Overrides to delegate to the inner panel that is the panel returned by getInnerPanel().

Overrides:
isRequestFocusEnabled in class JComponent

isWhite

protected boolean isWhite(Color c)

paintOver

public void paintOver(Graphics2D g2)

Override this paintOver method to add additional painting actions after the default buffer repaint is done during a repaint() call.

The intention of this facility is to enable algorithmic painting to be done via the paintOver method on top of the default painting of the buffer image on the panel. This makes the buffer appear to be the background and what is painted via the paintOver method to be painted in the foreground.

The default implementation of the paintOver method is to do nothing. This enables overrides as desired.

As of 2.4.0, this method is called after both the painting of the buffer and the painting of the internal paintable sequence. Given the power inherent in painting both the buffer bitmap and the internal paintable sequence, it is rare that this method will need to be overridden.

Parameters:
g2 - the Graphics2D context for the buffer repaint operation
Since:
1.0.1

requestFocus

public final void requestFocus()

Overrides to delegate to the inner panel that is the panel returned by getInnerPanel().

Overrides:
requestFocus in class JComponent

requestFocusInWindow

public final boolean requestFocusInWindow()

Overrides to delegate to the inner panel that is the panel returned by getInnerPanel().

Overrides:
requestFocusInWindow in class JComponent

setBufferSize

public void setBufferSize(int width,
                          int height)

Sets the size of the buffered image to the given height and width.

If the given width or height is less than 1 pixel, it is set to 1 pixel.

Any image area gained by an size increase in either direction will be painted with the current background color.

Any image area lost by a size decrease in either direction will be clipped on the right and/or bottom of the image.

For a short time both the image of the previous size and an image of the new size are maintained in memory.

Parameters:
width - the new width for the image
height - the new height for the image

setFocusable

public final void setFocusable(boolean focusable)

Overrides to delegate to the inner panel that is the panel returned by getInnerPanel().

Overrides:
setFocusable in class Component