uk.org.redfelineninja.awt
Class PixMap

java.lang.Object
  |
  +--java.awt.Component
        |
        +--uk.org.redfelineninja.awt.PixMap
All Implemented Interfaces:
java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable
Direct Known Subclasses:
ImageMap

public class PixMap
extends java.awt.Component

A component similar to Canvas but with per pixel access built in. This class originated through dissatisfaction with java.awt.Graphics. I need to test the colour of a pixel. java.awt.Graphics does not seem to provide this.

See Also:
Serialized Form

Field Summary
 
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
PixMap(java.awt.Image img)
          Create a new PixMap from an Image using a direct RGB colour mapping.
PixMap(java.awt.Image img, java.awt.image.ColorModel cm)
          Create a PixMap from an Image using the supplied ColorModel.
 
Method Summary
 void drawLine(int x1, int y1, int x2, int y2)
          Draws a line interior to the supplied points.
 java.awt.Rectangle floodFill(int x, int y)
          Uses a simple recursive algorithm to perform a flood fill.
 java.awt.Rectangle floodFillOld(int x, int y)
           
 boolean getAutoRepaint()
          Find whether or not the PixMap will automatically repaint itself.
 int getColor()
          Get the current draw colour.
 java.awt.image.ColorModel getColorModel()
          Returns the ColorModel used when creating the image from the PixMap
 java.awt.Image getImage()
          Supplies the Image version of the PixMap.
 java.awt.Dimension getMaximumSize()
          Get the maximum size of this component.
 java.awt.Dimension getMinimumSize()
          Get the minimum size of this component.
 int getPixel(int x, int y)
          Get the colour of the pixel at (x, y).
 int getPixel(java.awt.Point d)
          Get the colour of the pixel at the supplied point.
 int[] getPixels()
          Supplies the pixels used in this PixMap.
 java.awt.Dimension getPreferredSize()
          Get the preferred size of this component.
 boolean loadImage()
          Ensures the image is ready to be output.
 void newPixels()
          Tells the PixMap that the whole of is pixels need to be updated.
 void newPixels(int x, int y, int w, int h)
          Tells the PixMap that the pixels within the supplied box need to be updated.
 void paint(java.awt.Graphics g)
          Draw the Image as supplied by getImage to screen.
 void setAutoRepaint(boolean ar)
          Set whether or not the PixMap will automatically repaint itself.
 void setColor(java.awt.Color c)
          Sets the draw colour.
 void setColor(int c)
          Sets the draw colour.
 void setColorModel(java.awt.image.ColorModel cm)
          Sets the ColorModel used when creating the image from the PixMap
 void setPixel(int x, int y, int color)
          Set the colour of the pixel at (x, y).
 void setPixel(java.awt.Point d, int color)
          Set the colour of the pixel at the supplied point.
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, addNotify, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, bounds, checkImage, checkImage, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, deliverEvent, disable, dispatchEvent, doLayout, enable, enable, enableInputMethods, getAccessibleContext, getAlignmentX, getAlignmentY, getBackground, getBounds, getBounds, getComponentAt, getComponentAt, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeys, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getListeners, getLocale, getLocation, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, invalidate, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusCycleRoot, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isOpaque, isShowing, isValid, isVisible, keyDown, keyUp, layout, list, list, list, list, list, locate, location, lostFocus, minimumSize, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, preferredSize, prepareImage, prepareImage, print, printAll, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, requestFocusInWindow, reshape, resize, resize, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeys, setFocusTraversalKeysEnabled, setFont, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle, update, validate
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

PixMap

public PixMap(java.awt.Image img)
Create a new PixMap from an Image using a direct RGB colour mapping.

Parameters:
img - Image from which to generate the PixMap

PixMap

public PixMap(java.awt.Image img,
              java.awt.image.ColorModel cm)
Create a PixMap from an Image using the supplied ColorModel.

Parameters:
img - Image from which to generate the PixMap
cm - ColorModel to apply to the image
See Also:
ColorModel
Method Detail

getImage

public java.awt.Image getImage()
Supplies the Image version of the PixMap. Provides a means to use the graphics operations of PixMap without having to build the Component into a GUI.

Returns:
The image version of the PixMap

getPixels

public final int[] getPixels()
Supplies the pixels used in this PixMap. Provides a means for direct manipulation of pixels. Nine times out of ten it would be better to sub-class a PixMap and make use of the direct manipulation provided by that route.

Returns:
The array of integers that the PixMap is generated from

getPixel

public final int getPixel(int x,
                          int y)
Get the colour of the pixel at (x, y). This method is not clever and so you may get an ArrayIndexOutofBoundsException thrown to you.

Returns:
Integer representing a colour. Bits 24-31 represent alpha, 16-23, red, 8-15 green and 0-7 blue.

getPixel

public final int getPixel(java.awt.Point d)
Get the colour of the pixel at the supplied point. This method is not clever and so you may get an ArrayIndexOutofBoundsException thrown to you.

Returns:
Integer representing a colour. Bits 24-31 represent alpha, 16-23, red, 8-15 green and 0-7 blue.

setPixel

public final void setPixel(int x,
                           int y,
                           int color)
Set the colour of the pixel at (x, y). This method is not clever and so you may get an ArrayIndexOutofBoundsException thrown to you.

Parameters:
color - Integer representing a colour. Bits 24-31 represent alpha, 16-23, red, 8-15 green and 0-7 blue.

setPixel

public final void setPixel(java.awt.Point d,
                           int color)
Set the colour of the pixel at the supplied point. This method is not clever and so you may get an ArrayIndexOutofBoundsException thrown to you.

Parameters:
color - Integer representing a colour. Bits 24-31 represent alpha, 16-23, red, 8-15 green and 0-7 blue.

getColorModel

public java.awt.image.ColorModel getColorModel()
Returns the ColorModel used when creating the image from the PixMap

Overrides:
getColorModel in class java.awt.Component
Returns:
the ColorModel used in this PixMap

setColorModel

public void setColorModel(java.awt.image.ColorModel cm)
Sets the ColorModel used when creating the image from the PixMap


getColor

public int getColor()
Get the current draw colour.

Returns:
Integer representing a colour. Bits 24-31 represent alpha, 16-23, red, 8-15 green and 0-7 blue.

setColor

public void setColor(int c)
Sets the draw colour.

Parameters:
c - Integer representing a colour. Bits 24-31 represent alpha, 16-23, red, 8-15 green and 0-7 blue.

setColor

public void setColor(java.awt.Color c)
Sets the draw colour.


drawLine

public void drawLine(int x1,
                     int y1,
                     int x2,
                     int y2)
Draws a line interior to the supplied points. This method using bresenhams algorithm draw a line between two points.


getPreferredSize

public java.awt.Dimension getPreferredSize()
Get the preferred size of this component. This is the actual size of the PixMap and can be used to calculate indexs if the pixels are accessed directly.

Overrides:
getPreferredSize in class java.awt.Component
Returns:
the size of the PixMap
See Also:
getPixels()

getMinimumSize

public java.awt.Dimension getMinimumSize()
Get the minimum size of this component. This is the actual size of the PixMap and can be used to calculate indexs if the pixels are accessed directly.

Overrides:
getMinimumSize in class java.awt.Component
Returns:
the size of the PixMap
See Also:
getPixels()

getMaximumSize

public java.awt.Dimension getMaximumSize()
Get the maximum size of this component. This is the actual size of the PixMap and can be used to calculate indexs if the pixels are accessed directly.

Overrides:
getMaximumSize in class java.awt.Component
Returns:
the size of the PixMap
See Also:
getPixels()

getAutoRepaint

public boolean getAutoRepaint()
Find whether or not the PixMap will automatically repaint itself.

Returns:
true if the PixMap will automatically repaint.

setAutoRepaint

public void setAutoRepaint(boolean ar)
Set whether or not the PixMap will automatically repaint itself.

Parameters:
ar - true if the PixMap should automatically repaint.

floodFill

public java.awt.Rectangle floodFill(int x,
                                    int y)
Uses a simple recursive algorithm to perform a flood fill.

WARNING
The algorithm to perform flood filling is deeply recursive. It works fine on most virtual machines. It only works for very small areas on PersonalJava VMs, it throws a StackOverflowException if it does not work. It will be updated when a better way comes to light.

Returns:
the bounding box of the flood fill.

floodFillOld

public java.awt.Rectangle floodFillOld(int x,
                                       int y)

newPixels

public void newPixels()
Tells the PixMap that the whole of is pixels need to be updated.


newPixels

public void newPixels(int x,
                      int y,
                      int w,
                      int h)
Tells the PixMap that the pixels within the supplied box need to be updated.

Parameters:
x - the x coordinate of the upper left corner of the new Pixels
y - the y coordinate of the upper left corner of the new Pixels
w - the width of the rectangle
h - the height of the rectangle

loadImage

public boolean loadImage()
Ensures the image is ready to be output. This is only really for the paranoid, since the image is sourced from memory there is no reason for in not to be loaded.

Returns:
false if the image could not be loaded

paint

public void paint(java.awt.Graphics g)
Draw the Image as supplied by getImage to screen.

Overrides:
paint in class java.awt.Component
See Also:
getImage()