becker.robots.icons
Class Icon

java.lang.Object
  extended by becker.robots.icons.Icon
Direct Known Subclasses:
BrokenIcon, CompositeIcon, LabelIcon, ShapeIcon

public abstract class Icon
extends Object

Icons are used to display Robots, Intersections and Things. This class is abstract, merely defining what kinds of behaviors are required by the system. Subclasses define icons for specific purposes.

A very simple icon which displays a blue square could be defined as

public class BlueIcon extends Icon
  {  public BlueIcon(double relativeSize)
     {  super(relativeSize);
     }
     protected void renderImage(Graphics2D g2, int width, int height)
     {  g2.setColor(Color.blue);
        g2.fill(new Rectangle2D.Double(0.0, 0.0, 1.0, 1.0));
     }
   }
The drawing area is from from 0.0 to 1.0 in both dimensions. An alternative approach that is more consistent with using paintComponent is
public class BlueIcon extends Icon
  {  public BlueIcon(double relativeSize)
     {  super(relativeSize);
     }
     protected void paintIcon(Graphics g)
     {  g.setColor(Color.blue);
        g.fillRect(0, 0, 100, 100);
     }
   }
In this approach the implicit drawing area is 100 x 100 pixels. The system scales and rotates the image appropriately when it is drawn. The ShapeIcon class makes this even easier.

Author:
Byron Weber Becker

Field Summary
static Color transparent
          A transparent "color" which allows the underlying image to show through.
 
Constructor Summary
Icon()
          Construct a new, full-sized, transparent icon.
Icon(Color color)
          Construct a new icon of the specified color.
Icon(double relativeSize)
          Construct a new icon of the specified size.
Icon(double relativeSize, Color color)
          Construct a new icon of the specified size and color.
 
Method Summary
protected  void applyTransforms(Graphics2D g2, int width, int height, double rotation, double relSize)
          Apply translations, scaling and rotation to g2 in preparation for rendering the image.
 Color getColor()
          Get this icon's current color.
 Image getImage(int width, int height, double rotation)
          Get this icon's image with a preferred size (in pixels).
 String getLabel()
          Get the label currently being displayed on this icon.
protected  double getRotation()
          Get number of radians this icon is rotated.
 double getSize()
          Get the relative size of this icon.
 double getTransparency()
          Get this icon's current transparency.
protected  boolean hasChanged()
          Has this icon changed since it was last rendered?
protected  void markChanged()
          Mark this icon as changed so it is rendered again.
protected  void paintIcon(Graphics g)
          Override this method (or renderImage(java.awt.Graphics2D, int, int)) to specify how this icon looks.
protected  void renderImage(Graphics2D g2, int width, int height)
          Override either this method or paintIcon(java.awt.Graphics) to specify how the icon looks.
 void setColor(Color newColor)
          Change the color of this icon.
 void setLabel(String aLabel)
          Set the label to display on this icon.
 void setSize(double relativeSize)
          Change the relative size of this icon.
 void setTransparency(double trans)
          Set the transparency for this icon; that is, the degree to which it permits objects under it show through.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

transparent

public static final Color transparent
A transparent "color" which allows the underlying image to show through.

Constructor Detail

Icon

public Icon()
Construct a new, full-sized, transparent icon.


Icon

public Icon(double relativeSize)
Construct a new icon of the specified size.


Icon

public Icon(Color color)
Construct a new icon of the specified color.


Icon

public Icon(double relativeSize,
            Color color)
Construct a new icon of the specified size and color.

Method Detail

getColor

public Color getColor()
Get this icon's current color.

Returns:
this icon's current color.

setColor

public void setColor(Color newColor)
Change the color of this icon.

Parameters:
newColor - The new color.

getTransparency

public double getTransparency()
Get this icon's current transparency.

Returns:
A number between 0.0 (fully opaque) and 1.0 (fully transparent).

setTransparency

public void setTransparency(double trans)
Set the transparency for this icon; that is, the degree to which it permits objects under it show through.

Parameters:
trans - A number between 0.0 (fully opaque) and 1.0 (fully transparent).

getSize

public double getSize()
Get the relative size of this icon.

Returns:
a number between 0.0 (very small) and 1.0 (large).

setSize

public void setSize(double relativeSize)
Change the relative size of this icon.

Parameters:
relativeSize - a number which specifies how large the icon should be. 1.0 is as large as possible, 0.5 is half-size, 0.001 is very, very small. 0.0 < relativeSize <= 1.0.

getRotation

protected double getRotation()
Get number of radians this icon is rotated.

Returns:
the icon's rotation

getLabel

public String getLabel()
Get the label currently being displayed on this icon.

Returns:
The label currently displayed.

setLabel

public void setLabel(String aLabel)
Set the label to display on this icon.

Parameters:
aLabel - The label to display.

markChanged

protected void markChanged()
Mark this icon as changed so it is rendered again.


hasChanged

protected boolean hasChanged()
Has this icon changed since it was last rendered?

Returns:
true if this icon has changed since it was last rendered; false otherwise

renderImage

protected void renderImage(Graphics2D g2,
                           int width,
                           int height)
Override either this method or paintIcon(java.awt.Graphics) to specify how the icon looks. For this method, the upper left corner of the icon has coordinates (0,0). The width and height are both 1.0.

Parameters:
g2 - the graphics context where the icon should be drawn.
width - the number of pixels wide the image will be
height - the number of pixels high the image will be

paintIcon

protected void paintIcon(Graphics g)
Override this method (or renderImage(java.awt.Graphics2D, int, int)) to specify how this icon looks. For this method, the uper left corner of the icon has coordinates (0,0) and width and height of 100 pixels each. It's standard position is facing NORTH; other methods will rotate it, if required.

Parameters:
g - The graphics context where the icon should be drawn.

getImage

public Image getImage(int width,
                      int height,
                      double rotation)
Get this icon's image with a preferred size (in pixels).


applyTransforms

protected void applyTransforms(Graphics2D g2,
                               int width,
                               int height,
                               double rotation,
                               double relSize)
Apply translations, scaling and rotation to g2 in preparation for rendering the image. The standard transformations are:
    g2.translate(width/2.0, height/2.0);
    g2.scale(width*this.relSize, height*this.relSize);
    g2.rotate(rotation);
    g2.translate( - 0.5,  - 0.5);
    g2.setStroke(new BasicStroke(1.0F / width));
 

Parameters:
g2 - The graphics context to apply transformations to
width - the width (in pixels) of the icon
height - the height (in pixels) of the icon
rotation - the rotation of the icon, in radians
relSize - the relative size (0.0 < relSize <= 1.0) of the icon