javalib.worldcanvas
Class AppletCanvas

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

public class AppletCanvas
extends JPanel

A class to represent a canvas for the applet to draw on

Since:
November 28, 2007
Author:
Viera K. Proulx
See Also:
Serialized Form

Nested Class Summary
static class AppletCanvas.Painter
          Panel that paints the internal buffered image that maintains the persistent graphics state of the buffered panel.
static class AppletCanvas.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
           
 AppletCanvas.Painter painter
          The internal painter panel.
static AppletCanvas theCanvas
           
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
AppletCanvas(int width, int height)
          Create an instance of a CanvasPanel of the given dimensions.
 
Method Summary
 void clear()
          Fills this buffered panel with its background color or Paint.
 void clearPanel()
          Fills this buffered panel with its background color or Paint.
 void drawImage(ImageMaker imread, int x, int y)
          Draw the image provided by the ImageMaker on the Canvas at the given NW corner
 void drawImage(String fileName, Posn nw)
          Draw the image in the specified file at the location of the given NW corner
 void drawImage(WorldImage image)
          Draw the image on the Canvas at its pinhole location.
 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.
 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()
          We must implement this method to make sure the canvas panel gets focus when moused over - and responds to the key events appropriately.
 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().
 String toIndentedString(String indent)
          Produce an indented String representation of this Canvas
 String toString()
          Produce a String representation of this Canvas
 
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, transferFocus, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

buffer

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


HEIGHT

protected int HEIGHT

painter

public AppletCanvas.Painter painter
The internal painter panel.


theCanvas

public static AppletCanvas theCanvas

WIDTH

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

Constructor Detail

AppletCanvas

public AppletCanvas(int width,
                    int height)
Create an instance of a CanvasPanel of the given dimensions.

Parameters:
width - the width of the panel
height - the height of the panel
Method Detail

clear

public final void clear()

Fills this buffered panel with its background color or Paint.


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 provided by the ImageMaker on the Canvas at the given NW corner

Parameters:
imread - the image maker for the image file
x - the x coordinate for the image placement
y - the y coordinate for the image placement

drawImage

public void drawImage(String fileName,
                      Posn nw)
Draw the image in the specified file at the location of the given NW corner

Parameters:
fileName - the name of the image file
nw - the NW corner for the image placement

drawImage

public void drawImage(WorldImage image)
Draw the image on the Canvas at its pinhole location.

Parameters:
image - the image to be drawn

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.


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.


isFocusable

public boolean isFocusable()
We must implement this method to make sure the canvas panel gets focus when moused over - and responds to the key events appropriately.

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

toIndentedString

public String toIndentedString(String indent)
Produce an indented String representation of this Canvas

Parameters:
indent - the desired indentation: ignored, because we only produce one line of text.

toString

public String toString()
Produce a String representation of this Canvas

Overrides:
toString in class Component