yops
Class Image

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 yops.Graphic
                      extended by yops.Image
All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, Accessible

public class Image
extends Graphic

An Image is a visual display of a picture represented as a raster of pixels in an (x,y) coordinate system, with (0,0) at the upper left. An image can be loaded from a gif or jpg image file. Also, the pixels of an image can be individually set.

Author:
Kenneth J. Goldman
Created Jun 28, 2005
See Also:
Serialized Form

Nested Class Summary
 
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.BltBufferStrategy, Component.FlipBufferStrategy
 
Field Summary
 
Fields inherited from class yops.Graphic
THICK_STROKE
 
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
Image()
          Creates a new Image with a default width and height.
Image(GraphicsPanel panel, int x, int y, int width, int height)
          Creates a new image by copying a rectangular region from the main image of the given GraphicsPanel.
Image(Image img, int x, int y, int width, int height)
          Creates a new Image by copying a rectangular region from the main image of the given Image.
Image(int width, int height)
          Creates a new Image with the given width and height.
 
Method Summary
 void copy(Image destination)
          Copies this Image into the given destination image.
 void copy(int srcX, int srcY, int width, int height, Image destination)
          Copies a rectangular potion of this image into the given destination image.
 void copy(int srcX, int srcY, int width, int height, Image destination, int destX, int destY)
          Copies a rectangular potion of this image into the given destination image.
 void fillRegion(int x, int y, int width, int height, Color color)
          Fills a rectangular region of the image with the given color.
 void fillRegion(int x, int y, int width, int height, int rgb)
          Fills a rectangular region of the image with the given color.
 int getPixel(int x, int y)
          Returns the value of the pixel with the given coordinates.
 Color getPixelColor(int x, int y)
          Returns a Color object for the color of the pixel with the given coordinates.
 Dimension getPreferredSize()
          This returns the size of the image, for use by Java's layout manager.
 void loadImage(String imageFile)
          Loads a gif or jpeg file into the raster.
 void paint(Graphics g)
          Paints the Image when requested by the Java's graphics system, using the current zoom and rotation.
 void setCopyAsDisplayed(boolean copyAsDisplayed)
          When copyAsDisplayed is set to true, the current zoom and rotation are taken into account every time the image is copied.
 void setPixel(int x, int y, Color c)
          Sets the the pixel with the given coordinates to the given color.
 void setPixel(int x, int y, int rgb)
          Sets the the pixel with the given coordinates to the given integer value.
 void setRotation(double degrees)
          Rotates the image display about its center within its rectangular frame.
 void setZoom(double percent)
          Scales the image display by the given percentage, keeping the center fixed within its rectangular frame.
 void swapPixels(int x1, int y1, int x2, int y2)
          Swaps the color values of the two pixels with coordinates (x1,y1) and (x2,y2).
 
Methods inherited from class yops.Graphic
add, getCenterX, getCenterY, isFilled, setCenter, setFillColor, setFilled, setLineColor, setUpperLeft, toString
 
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, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isRequestFocusEnabled, isValidateRoot, 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, 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, transferFocus, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Image

public Image()
Creates a new Image with a default width and height.


Image

public Image(int width,
             int height)
Creates a new Image with the given width and height.

Parameters:
width - in pixels
height - in pixels

Image

public Image(GraphicsPanel panel,
             int x,
             int y,
             int width,
             int height)
Creates a new image by copying a rectangular region from the main image of the given GraphicsPanel.

Parameters:
panel - the GraphicsPanel whose image should be copied from
x - the left pixel coordinate of the region to be copied
y - the top pixel coordinate of the region to be copied
width - in pixels
height - in pixels

Image

public Image(Image img,
             int x,
             int y,
             int width,
             int height)
Creates a new Image by copying a rectangular region from the main image of the given Image.

Parameters:
img - the image that should be copied from
x - the left pixel coordinate of the region to be copied
y - the top pixel coordinate of the region to be copied
width - in pixels
height - in pixels
Method Detail

getPreferredSize

public Dimension getPreferredSize()
This returns the size of the image, for use by Java's layout manager. You should not need to call this method.

Overrides:
getPreferredSize in class JComponent

loadImage

public void loadImage(String imageFile)
Loads a gif or jpeg file into the raster. The loaded image is centered and scaled as large as possible to fit within the dimensions of this Image object without changing the image proportions.

Parameters:
imageFile - the full path name of the file to be loaded

setRotation

public void setRotation(double degrees)
Rotates the image display about its center within its rectangular frame. This affects only the display of the image. It does not change the values of the pixels in the raster.

Parameters:
degrees - the angle of rotation, in the counter clockwise direction

setZoom

public void setZoom(double percent)
Scales the image display by the given percentage, keeping the center fixed within its rectangular frame. Values less than 100 make the image appear smaller, while values greater than 100 make it appear larger. This affects only the display of the image. It does not change the values of the pixels in the raster.

Parameters:
percent - the zoom factor, as a percentage

setCopyAsDisplayed

public void setCopyAsDisplayed(boolean copyAsDisplayed)
When copyAsDisplayed is set to true, the current zoom and rotation are taken into account every time the image is copied. Otherwize, copies are taken directly from the unzoomed and unrotated raster.

Parameters:
copyAsDisplayed - whether or not zooming and rotation should be used during image copying

copy

public void copy(Image destination)
Copies this Image into the given destination image. The image will be painted into the destination starting at the upper left corner of the destination.

Parameters:
destination - the image into which this image should be copied

copy

public void copy(int srcX,
                 int srcY,
                 int width,
                 int height,
                 Image destination)
Copies a rectangular potion of this image into the given destination image. The image will be painted into the destination starting at the upper left corner of the destination.

Parameters:
srcX - the left pixel coordiniate of the region to be copied
srcY - the top pixel coordiniate of the region to be copied
width - of the region to be copied, in pixels
height - of the region to be copied, in pixels
destination - the image into which this image should be copied

copy

public void copy(int srcX,
                 int srcY,
                 int width,
                 int height,
                 Image destination,
                 int destX,
                 int destY)
Copies a rectangular potion of this image into the given destination image. The image will be painted into the destination starting at the given coordinates (destX,destY).

Parameters:
srcX - the left pixel coordiniate of the region to be copied
srcY - the top pixel coordiniate of the region to be copied
width - of the region to be copied, in pixels
height - of the region to be copied, in pixels
destination - the image into which this image should be copied
destX - the x coordinate of the upper left corner within the destination, where the image should be copied
destY - the y coordinate of the upper left corner within the destination, where the image should be copied

fillRegion

public void fillRegion(int x,
                       int y,
                       int width,
                       int height,
                       Color color)
Fills a rectangular region of the image with the given color.

Parameters:
x - the left pixel coordinate of the region
y - the top pixel coordinate of the region
width - in pixels
height - in pixels
color - the color to be used for filling

fillRegion

public void fillRegion(int x,
                       int y,
                       int width,
                       int height,
                       int rgb)
Fills a rectangular region of the image with the given color.

Parameters:
x - the left pixel coordinate of the region
y - the top pixel coordinate of the region
width - in pixels
height - in pixels
rgb - the color to be used for filling, expressed as an int, with 8 bits each for red (bits 16-23), green (bits 8-15), and blue (bits 0-7).

getPixelColor

public Color getPixelColor(int x,
                           int y)
Returns a Color object for the color of the pixel with the given coordinates.

Parameters:
x -
y -
Returns:
the Color of the pixel

getPixel

public int getPixel(int x,
                    int y)
Returns the value of the pixel with the given coordinates. The result given as an int, with 8 bits each for red (bits 16-23), green (bits 8-15), and blue (bits 0-7).

Parameters:
x -
y -
Returns:
the pixel value

setPixel

public void setPixel(int x,
                     int y,
                     Color c)
Sets the the pixel with the given coordinates to the given color.

Parameters:
x -
y -
c - the desired color for pixel (x,y)

setPixel

public void setPixel(int x,
                     int y,
                     int rgb)
Sets the the pixel with the given coordinates to the given integer value.

Parameters:
x -
y -
rgb - the desired color for pixel (x,y), expressed as an int, with 8 bits each for red (bits 16-23), green (bits 8-15), and blue (bits 0-7).

swapPixels

public void swapPixels(int x1,
                       int y1,
                       int x2,
                       int y2)
Swaps the color values of the two pixels with coordinates (x1,y1) and (x2,y2).

Parameters:
x1 - x coordinate of the first pixel
y1 - y coordinate of the first pixel
x2 - x coordinate of the second pixel
y2 - y coordinate of the second pixel

paint

public void paint(Graphics g)
Paints the Image when requested by the Java's graphics system, using the current zoom and rotation. You should not need to call this method. If you want to specifically request that a component be repainted, call Component.repaint(). (If you do not see this graphic, it may not have been added to a visible container, or it may be behind another graphic.)

Overrides:
paint in class JComponent