nankin.zheng.gatem.view
Class DisplayCacheImplSwing

java.lang.Object
  |
  +--nankin.zheng.gatem.view.DisplayCacheImplSwing
All Implemented Interfaces:
DisplayCache

public class DisplayCacheImplSwing
extends java.lang.Object
implements DisplayCache

A default implementation of interface DisplayCache as JPanel instance (for Swing-platform).

In Swing, the actual drawing is done by JPanel.paintComponent(), but in Gatem, the actual drawing is done by DisplayCache.flush(). So flush() should call JPanel.repaint() to update game frame, and JPanel.paintComponent() will draw game frame onto the screen according to the viewport and DrawCommand buffer.


Field Summary
private  java.util.Vector buffer
          Buffer saves all the draw commands.
private  int depth
           
private  Env env
           
private  Position viewport
           
 
Constructor Summary
DisplayCacheImplSwing(Env environ)
          Constructor.
 
Method Summary
 void addToCache(DrawCommand cmd)
          Add a DrawCommand object to DrawCommand buffer.
 void flush(java.lang.Object helper)
          Flush buffer and draw the game frame onto the screen.
 int getDepth()
          Get depth.
private  int getViewportPhysicalX(DrawCommand cmd)
          Get physical coordonate x of the top-left point of the image contained in cmd.
private  int getViewportPhysicalY(DrawCommand cmd)
          Get physical coordonate y of the top-left point of the image contained in cmd.
private  int getWindowPhysicalX(int x)
          Adjust viewport physical coordonate x to window physical coordonate x.
private  int getWindowPhysicalY(int y)
          Adjust viewport physical coordonate y to window physical coordonate y.
private  boolean isInsideViewport(java.awt.Image img, int x, int y)
          Whether the image is inside the viewport.
 void setDepth(int d)
          Set depth
 void setViewport(Position vp)
          Set viewport position.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

env

private Env env

viewport

private Position viewport

depth

private int depth

buffer

private java.util.Vector buffer
Buffer saves all the draw commands. NOTICE that since JPanel.repaint() and g.drawImage() will create new threads to do the actual work, buffer should be visited mutually.

Constructor Detail

DisplayCacheImplSwing

public DisplayCacheImplSwing(Env environ)
Constructor.

Parameters:
environ - game environment object.
Throws:
java.lang.NullPointerException - if environ is null.
Method Detail

getDepth

public int getDepth()
Get depth.

Specified by:
getDepth in interface DisplayCache
Returns:
depth

setDepth

public void setDepth(int d)
Set depth

Specified by:
setDepth in interface DisplayCache
Parameters:
d - depth to be set

setViewport

public void setViewport(Position vp)
Set viewport position. The viewport is the visual part of all the background.

Note that since DisplayCache.flush() is called by a thread, we CANNOT make sure that before another frame updating starts, the old DrawCommand instances have all been flushed. So, while setting viewport, it is possible that there are still some DrawCommand instances left in the buffer.

Specified by:
setViewport in interface DisplayCache
Parameters:
vp - position of viewport.

addToCache

public void addToCache(DrawCommand cmd)
Add a DrawCommand object to DrawCommand buffer.

While adding command to buffer, the image in the command is not drawn onto the screen but only saved in the buffer. All the draw commands will be executed (their images will be drawn onto the screen) for one time when flush() is called.

This method is set synchronized to avoid modifying buffer while buffer is being visited by paintComponent().

Specified by:
addToCache in interface DisplayCache
Parameters:
cmd - DrawCommand object.
Throws:
java.lang.NullPointerException - if cmd is null.

flush

public void flush(java.lang.Object helper)
           throws GameException
Flush buffer and draw the game frame onto the screen. After flushing, it will clean the buffer.

NOTE that this method does the actual drawing work, and it calls g.drawImage(Image, int, int) method. So this method will be called by JPanel.paintComponent() method.

NOTE that this method is set synchronized to prevent other method (addToCache()) from modifying the buffer while it is in use.

Specified by:
flush in interface DisplayCache
Parameters:
helper - Graphics object, containing the drawing environment.
Throws:
GameException - if helper is not an instance of Graphics.

getViewportPhysicalX

private int getViewportPhysicalX(DrawCommand cmd)
Get physical coordonate x of the top-left point of the image contained in cmd.

Parameters:
cmd - DrawCommand object which contains the image and the logical coordonate of the image.
Returns:
physical coordonate x of the top-left point of the image.

getViewportPhysicalY

private int getViewportPhysicalY(DrawCommand cmd)
Get physical coordonate y of the top-left point of the image contained in cmd.

Parameters:
cmd - DrawCommand object which contains the image and the logical coordonate of the image.
Returns:
physical coordonate y of the top-left point of the image.

getWindowPhysicalX

private int getWindowPhysicalX(int x)
Adjust viewport physical coordonate x to window physical coordonate x.


getWindowPhysicalY

private int getWindowPhysicalY(int y)
Adjust viewport physical coordonate y to window physical coordonate y.


isInsideViewport

private boolean isInsideViewport(java.awt.Image img,
                                 int x,
                                 int y)
Whether the image is inside the viewport.

Parameters:
img - image.
x - physical coordonate x of the top-left point of the image.
y - physical coordonate y of the top-left point of the image.
Returns:
true if the image is inside the viewport.