net.kolls.railworld
Class RailCanvas

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Canvas
          extended by net.kolls.railworld.RailCanvas
All Implemented Interfaces:
java.awt.event.MouseListener, java.awt.event.MouseMotionListener, java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable, java.util.EventListener, javax.accessibility.Accessible
Direct Known Subclasses:
EditCanvas, PlayCanvas

public abstract class RailCanvas
extends java.awt.Canvas
implements java.awt.event.MouseListener, java.awt.event.MouseMotionListener

The RailCanvas is one of the workhorses of the Rail World game. A RailCanvas represents the main drawing window where the image is drawn and overlaid with rail segments and trains. The rail canvas is also where the user clicks to select trains and operate segment devices, such as switches.

Author:
Steve Kollmansberger
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class java.awt.Canvas
java.awt.Canvas.AccessibleAWTCanvas
 
Nested classes/interfaces inherited from class java.awt.Component
java.awt.Component.AccessibleAWTComponent, java.awt.Component.BaselineResizeBehavior, java.awt.Component.BltBufferStrategy, java.awt.Component.FlipBufferStrategy
 
Field Summary
 RailSegment[] la
          The rail segments.
protected  java.awt.image.BufferedImage minisource
          The miniviewer image
protected  MiniViewer miniv
          The mini-viewer for this canvas
protected  int mx
          The mouse location during a drag (x)
protected  int my
          The mouse location during a drag (y)
 java.awt.image.BufferedImage orig_src
          The original, unmodified source image
protected  int vx
          The left edge of the current viewing location on the image
protected  int vy
          The upper edge of the current viewing location on the image
static double zoom
          The zoom value may be changed at any time, but appropriate redraws may be needed.
 
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
RailCanvas(java.awt.image.BufferedImage s, RailSegment[] lines, MiniViewer mini)
          Construct a new rail canvas
 
Method Summary
static java.awt.geom.Line2D angle(java.awt.geom.Line2D bl, java.awt.geom.Point2D myPoint, double angle, Distance len)
          Find a position relative to an existing line.
 java.awt.Dimension areaSize()
          Finds the area of the entire playing map at current zoom level.
abstract  void doMiniPaint(java.awt.Graphics2D ofg)
          Given the graphics context of the mini image, paint it.
protected abstract  void doPaint(java.awt.Graphics2D ofg, int hvx, int hvy, boolean detailed)
          Paint the main image.
 void draw(java.awt.Graphics2D g, java.awt.geom.Point2D center, int width, int height, double useZoom, boolean detailed)
          Draws a portion of the display
 void drawCanvas()
          Draw the rail canvas and its miniviewer, if appropriate.
static void drawOutlineFont(java.awt.Graphics2D ofg, int x, int y, java.lang.String text, int size, java.awt.Color c, double angle, boolean centered)
          Draws a shadowed text on a graphics device.
 java.awt.geom.Point2D getCenterPoint()
          Returns the center of the currently displayed area.
 java.awt.geom.Point2D getVXY()
           
 void leftDrag(java.awt.event.MouseEvent e)
          Called if the left mouse button is held and dragged.
 void leftMove(java.awt.event.MouseEvent e)
          Called if the mouse is moved without the right mouse button down
 void leftPress(java.awt.event.MouseEvent e)
          Called if the left mouse button is pressed
 void leftRelease(java.awt.event.MouseEvent e)
          Called if the left mouse button is released
static double lineAngle(java.awt.geom.Line2D bl)
          Calculate the angle of a line, in radians.
 void mouseClicked(java.awt.event.MouseEvent e)
           
 void mouseDragged(java.awt.event.MouseEvent e)
          If the right mouse button is dragged, moves the displayed location.
 void mouseEntered(java.awt.event.MouseEvent e)
           
 void mouseExited(java.awt.event.MouseEvent e)
           
 void mouseMoved(java.awt.event.MouseEvent e)
           
 void mousePressed(java.awt.event.MouseEvent e)
           
 void mouseReleased(java.awt.event.MouseEvent e)
           
 void recomp()
          Recompute all segment details
 void stop()
          Deallocate images for garbage collection.
 void submitCenterCoords(int x, int y)
          Move the display to attempt to accomodate the given image coordinates as a center point.
 void submitCoords(int x, int y)
          Move the display to attempt to accomodate the given upper-left coordinates on the static image.
 double trackLength()
          Calculates the total length of all rail segments in feet.
protected  java.awt.geom.Point2D transform(java.awt.event.MouseEvent e)
          Transforms a point on the canvas to a point on the image.
protected  java.awt.geom.Point2D transform(java.awt.geom.Point2D p)
          Transforms a point on the canvas to a point on the image.
 
Methods inherited from class java.awt.Canvas
addNotify, createBufferStrategy, createBufferStrategy, getAccessibleContext, getBufferStrategy, paint, update
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, deliverEvent, disable, disableEvents, dispatchEvent, doLayout, enable, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAlignmentX, getAlignmentY, getBackground, getBaseline, getBaselineResizeBehavior, getBounds, getBounds, getColorModel, 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, getMaximumSize, getMinimumSize, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPreferredSize, 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, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, layout, list, list, list, list, list, locate, location, lostFocus, minimumSize, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, paramString, postEvent, preferredSize, prepareImage, prepareImage, print, printAll, processComponentEvent, processEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, reshape, resize, resize, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeys, setFocusTraversalKeysEnabled, setFont, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setMinimumSize, setName, setPreferredSize, setSize, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle, validate
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

orig_src

public java.awt.image.BufferedImage orig_src
The original, unmodified source image


minisource

protected java.awt.image.BufferedImage minisource
The miniviewer image


la

public RailSegment[] la
The rail segments. This should be considered read-only


vx

protected int vx
The left edge of the current viewing location on the image


vy

protected int vy
The upper edge of the current viewing location on the image


mx

protected int mx
The mouse location during a drag (x)


my

protected int my
The mouse location during a drag (y)


miniv

protected MiniViewer miniv
The mini-viewer for this canvas


zoom

public static double zoom
The zoom value may be changed at any time, but appropriate redraws may be needed.

Constructor Detail

RailCanvas

public RailCanvas(java.awt.image.BufferedImage s,
                  RailSegment[] lines,
                  MiniViewer mini)
Construct a new rail canvas

Parameters:
s - The source image (@link{#orig_src}) to use.
lines - The rail segments (@link{#la}) to use.
mini - The mini viewer (@link{#miniv}) to use.
Method Detail

getVXY

public java.awt.geom.Point2D getVXY()
Returns:
The current positional offset

trackLength

public double trackLength()
Calculates the total length of all rail segments in feet.

Returns:
Sum of all rail segment lengths, in feet.

stop

public void stop()
Deallocate images for garbage collection.


recomp

public void recomp()
Recompute all segment details


getCenterPoint

public java.awt.geom.Point2D getCenterPoint()
Returns the center of the currently displayed area.

Returns:
A Point2D representing the center point of display on the image.

submitCenterCoords

public void submitCenterCoords(int x,
                               int y)
Move the display to attempt to accomodate the given image coordinates as a center point.

Parameters:
x - X position on static image
y - Y position on static image

submitCoords

public void submitCoords(int x,
                         int y)
Move the display to attempt to accomodate the given upper-left coordinates on the static image.

Parameters:
x - The x coordinate on the image
y - The y coordinate on the image

lineAngle

public static double lineAngle(java.awt.geom.Line2D bl)
Calculate the angle of a line, in radians.

Parameters:
bl - A Line2D to find the angle of.
Returns:
A double value, representing angle in radians.

angle

public static java.awt.geom.Line2D angle(java.awt.geom.Line2D bl,
                                         java.awt.geom.Point2D myPoint,
                                         double angle,
                                         Distance len)
Find a position relative to an existing line. This is used, for example, to draw the accepting cars or the switch selector. The angle is relative to the given line; for example, a 90 degree angle would be perpendicular to the line no matter which way the line faced. The point is a fixed starting point, and the len is the distance to travel at the given angle.

Parameters:
bl - A Line2D to start from.
myPoint - A Point2D as a fixed starting point.
angle - An angle, in radians, relative to the line given.
len - A Distance at the given relative angle to move.
Returns:
A Line2D from the given starting point to the end along the relative angle.

drawOutlineFont

public static void drawOutlineFont(java.awt.Graphics2D ofg,
                                   int x,
                                   int y,
                                   java.lang.String text,
                                   int size,
                                   java.awt.Color c,
                                   double angle,
                                   boolean centered)
Draws a shadowed text on a graphics device.

Parameters:
ofg - A Graphics2D to draw on.
x - Position to draw the text.
y -
text - The actual text to draw.
size - The size, in pixels, of the text.
c - The Color of the text.
angle - The angle, in radians, to draw the text.
centered - The position given represents the center of the text?

draw

public void draw(java.awt.Graphics2D g,
                 java.awt.geom.Point2D center,
                 int width,
                 int height,
                 double useZoom,
                 boolean detailed)
Draws a portion of the display

Parameters:
center - A Point2D representing the center position of the image.
g - The Graphics2D to draw into. Drawing is always performed at (0,0) in the target graphics.
width - The width of the draw.
height - The height of the draw.
useZoom - The zoom factor to use for this draw. Normally zoom
detailed - Should high expense items like scripts be drawn as well?

drawCanvas

public void drawCanvas()
Draw the rail canvas and its miniviewer, if appropriate.


areaSize

public java.awt.Dimension areaSize()
Finds the area of the entire playing map at current zoom level.

Returns:
Dimension indicating area.

doMiniPaint

public abstract void doMiniPaint(java.awt.Graphics2D ofg)
Given the graphics context of the mini image, paint it.

Parameters:
ofg - A Graphics2D context for the mini image.

doPaint

protected abstract void doPaint(java.awt.Graphics2D ofg,
                                int hvx,
                                int hvy,
                                boolean detailed)
Paint the main image.

Parameters:
ofg - A Graphics2D context for the main image.
hvy - The vx coordinate to use
hvx - The vy coordinate to use
detailed - Should expensive items also be drawn?

transform

protected final java.awt.geom.Point2D transform(java.awt.event.MouseEvent e)
Transforms a point on the canvas to a point on the image.

Parameters:
e - The mouse event which includes the point.
Returns:
A point on the static image

transform

protected final java.awt.geom.Point2D transform(java.awt.geom.Point2D p)
Transforms a point on the canvas to a point on the image.

Parameters:
p - The point on the canvas to transform
Returns:
A point on the static image

leftDrag

public void leftDrag(java.awt.event.MouseEvent e)
Called if the left mouse button is held and dragged.

Parameters:
e - The mouse event generated

leftMove

public void leftMove(java.awt.event.MouseEvent e)
Called if the mouse is moved without the right mouse button down

Parameters:
e - The mouse event generated

leftRelease

public void leftRelease(java.awt.event.MouseEvent e)
Called if the left mouse button is released

Parameters:
e - The mouse event generated

leftPress

public void leftPress(java.awt.event.MouseEvent e)
Called if the left mouse button is pressed

Parameters:
e - The mouse event generated

mouseClicked

public void mouseClicked(java.awt.event.MouseEvent e)
Specified by:
mouseClicked in interface java.awt.event.MouseListener

mouseEntered

public void mouseEntered(java.awt.event.MouseEvent e)
Specified by:
mouseEntered in interface java.awt.event.MouseListener

mouseExited

public void mouseExited(java.awt.event.MouseEvent e)
Specified by:
mouseExited in interface java.awt.event.MouseListener

mousePressed

public final void mousePressed(java.awt.event.MouseEvent e)
Specified by:
mousePressed in interface java.awt.event.MouseListener

mouseReleased

public final void mouseReleased(java.awt.event.MouseEvent e)
Specified by:
mouseReleased in interface java.awt.event.MouseListener

mouseDragged

public final void mouseDragged(java.awt.event.MouseEvent e)
If the right mouse button is dragged, moves the displayed location. Otherwise delegates to leftDrag(MouseEvent).

Specified by:
mouseDragged in interface java.awt.event.MouseMotionListener

mouseMoved

public final void mouseMoved(java.awt.event.MouseEvent e)
Specified by:
mouseMoved in interface java.awt.event.MouseMotionListener