|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--nankin.zheng.gatem.view.DisplayCacheImplSwing
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 |
private Env env
private Position viewport
private int depth
private java.util.Vector buffer
Constructor Detail |
public DisplayCacheImplSwing(Env environ)
environ
- game environment object.
java.lang.NullPointerException
- if environ is null.Method Detail |
public int getDepth()
getDepth
in interface DisplayCache
public void setDepth(int d)
setDepth
in interface DisplayCache
d
- depth to be setpublic void setViewport(Position vp)
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.
setViewport
in interface DisplayCache
vp
- position of viewport.public void addToCache(DrawCommand cmd)
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().
addToCache
in interface DisplayCache
cmd
- DrawCommand object.
java.lang.NullPointerException
- if cmd is null.public void flush(java.lang.Object helper) throws GameException
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.
flush
in interface DisplayCache
helper
- Graphics object, containing the drawing environment.
GameException
- if helper is not an instance of Graphics.private int getViewportPhysicalX(DrawCommand cmd)
cmd
- DrawCommand object which contains the image
and the logical coordonate of the image.
private int getViewportPhysicalY(DrawCommand cmd)
cmd
- DrawCommand object which contains the image
and the logical coordonate of the image.
private int getWindowPhysicalX(int x)
private int getWindowPhysicalY(int y)
private boolean isInsideViewport(java.awt.Image img, int x, int y)
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.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |