public class View
extends javax.swing.JPanel
Modifier and Type | Class and Description |
---|---|
private static class |
View.HorizLocation
A x-location of something
|
private static class |
View.VertLocation
A y-location of something
|
javax.swing.JPanel.AccessibleJPanel
javax.swing.JComponent.AccessibleJComponent
Modifier and Type | Field and Description |
---|---|
private int |
animalHealth
The current health of the animal
|
private boolean |
arrowKeyPrompt
Boolean that determines whether the arrow key prompt should be shown on
screen.
|
private static java.awt.Color |
BACKGROUND_COLOR
The background color of the screen
|
private static int |
crabXLoc
The x location of the crab in world coordinates
|
private static int |
crabYLoc
The y location of the crab in world coordinates
|
private static int |
END_SCREEN_SCORE_TRANSITION_DURATION
Duration over which to slowly raise the score when transitioning into the end game screen.
|
private long |
endScreenTimestamp
The time that the phase GamePhase.GAME_END was entered.
|
private int |
endTime
A long representing when the game should end in order to draw the truck timer
in the correct spot
|
private java.util.Map<java.lang.Integer,java.lang.Integer> |
fontWorldToPt
Stores font sizes calculated using binary search.
|
(package private) javax.swing.JFrame |
frame
The JFrame housing this View.
|
(package private) GamePhase |
gamePhase
The current phase of the game
|
private boolean |
hasLitter
Whether the player is holding on to litter
|
private boolean |
hasLost
Whether the player has lost or won the game.
|
private boolean |
hoverLitter
Boolean that represents whether or not the Player is hovering, but not
picking up a Litter object
|
private java.util.HashSet<java.util.ArrayList<java.lang.Integer>> |
litterAttrSet
Contains all Litter objects to be rendered onscreen, maps them to a
Sprite.ID.
|
private static int |
litterCount
The number of distinct litter sprites
|
private static java.util.ArrayList<java.util.ArrayList<Sprite.ID>> |
litterImgLists
A list containing lists of litter sprite ids.
|
private java.util.HashSet<Litter> |
litterSet
contains all of the Litter objects on-screen
|
private java.util.ArrayList<java.lang.Integer> |
pickedUpAttr
The image attributes of the Litter object most recently held by the Player
|
private java.util.ArrayList<Plant> |
plants
Contains all plant objects onscreen
|
private static Direction |
playerDirection
The current direction of the player
|
private int |
playerHealth
The current health of the player
|
private static PlayerStatus |
playerStatus
The status of the player, i.e.&nbdp;idle, moving, etc.
|
private static int |
playerXLoc
The x location of the player in world coordinates
|
private static int |
playerYLoc
The y location of the player in world coordinates
|
private static int |
recImgCount
The number of distinct recyclable sprites
|
private boolean |
rGlow
A Boolean to decide if the recycling bin is in the glowing deposit state
|
private River |
river
The river
|
private int[] |
riverAlphaMapPixels
A array of every pixel in the image
Sprite.ID.RIVER_ALPHA , set the first time drawRiver(java.awt.Graphics2D, int) os run. |
private int[] |
riverTexturePixels
A array of every pixel in the image
Sprite.ID.RIVER_TEXTURE , set the first time drawRiver(java.awt.Graphics2D, int) is run. |
private static java.awt.Dimension |
screenDimension
The dimensions of the computer screen, in pixels.
|
private static double |
screenHeight
The height of the computer screen, in pixels.
|
private static double |
screenWidth
The width of the computer screen, in pixels.
|
private long |
startTime
A long representing when the game started in order to draw the truck timer in
the correct spot
|
private boolean |
tGlow
A Boolean to decide if the trash bin is in the glowing deposit state
|
private int |
totalLitterCollected
The number of Litter objects that the Player has picked up throughout the game
|
private int |
totalPlantsPlanted
The number of Plants that the Player has replanted throughout the game
|
private static int |
trashImgCount
The number of distinct trash sprites
|
(package private) TutorialState |
tutorialState
Gamestate variable that represents the current stage of the tutorial the
player is at
|
static int |
WORLD_HEIGHT
The height of the game world
|
static int |
WORLD_WIDTH
The width of the game world
|
listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
Constructor and Description |
---|
View()
Creates a new View, places it in a new JPanel, arranges everything, and makes it visible.
|
Modifier and Type | Method and Description |
---|---|
private void |
drawEndScreenOverlay(java.awt.Graphics g)
Draws the end screen text onto the screen.
|
private void |
drawHealth(java.awt.Graphics g)
Draws the health bar and the crabs in the upper right hand corner of the
screen.
|
private void |
drawImage(java.awt.Graphics g,
Sprite.ID s,
int world_x,
int world_y)
Renders a
Sprite.ID onto the given Graphics by consuming
world coordinates. |
private void |
drawLitterContainerOverlay(java.awt.Graphics g)
Draws the litter container overlay
|
private void |
drawOverlayBox(java.awt.Graphics g)
Draws a box on the screen appropriate for title screen, end score, etc.
|
private void |
drawRiver(java.awt.Graphics2D g,
int xpos_world)
Draws the river on the screen.
|
private void |
drawStartScreenText(java.awt.Graphics g)
Draws the start screen text onto the screen.
|
private void |
drawString(java.awt.Graphics g,
java.lang.String str,
int xpos,
int ypos,
int height,
View.HorizLocation horizLocation,
View.VertLocation vertLocation)
Draws a string centered in the specified horizontal line by consuming
world coordinates.
|
private void |
drawTimer(java.awt.Graphics g)
Draws a timer on the screen.
|
private void |
drawTutorialOverlays(java.awt.Graphics g)
Draws the tutorial overlay
|
private java.awt.Dimension |
getFrameDimensions()
Returns the dimensions of the inner frame in pixels.
|
private int |
getFrameHeight()
Returns the height in pixels of the inner frame.
|
private int |
getFrameHorizOffset()
Returns the distance in pixels between a side of the window and the inner
frame.
|
private int |
getFrameVertOffset()
Returns the distance in pixels between the top/bottom of the window and the
inner frame.
|
private int |
getFrameWidth()
Returns the width in pixels of the inner frame.
|
private Sprite.ID |
getPlayerSprite()
Determines which
Sprite.ID to use to render the player. |
Sprite.ID |
getSpriteID(int lType,
int imgID)
Chooses a Sprite ID to represent a Litter object.
|
void |
paint(java.awt.Graphics g)
Paints this view
|
void |
preloadLitterImgs()
Populates trashImgList and recyclableImgList with Sprite.IDs.
|
private int |
setFontSize(java.awt.Graphics g,
int worldHeight)
Sets the font of the given
Graphics to match the given height in world coordinates. |
void |
setKeyListener(java.awt.event.KeyListener listener)
Adds a key listener to the associated JFrame
|
private void |
setPaint(java.awt.Graphics2D g,
Sprite.ID s)
Sets the paint of the given Graphics2D to be a tesselation of the given Sprite.ID, scaling to match rendering size.
|
void |
update(GamePhase gamePhase,
int playerX,
int playerY,
Direction dir,
PlayerStatus status,
int crabX,
int crabY,
java.util.ArrayList<java.lang.Integer> pickedUpAttr,
boolean hasLitter,
java.util.ArrayList<Plant> plants,
boolean tVictory,
boolean rVictory,
int playerHealth,
int animalHealth,
River river,
TutorialState tutorialState,
java.util.HashSet<java.util.ArrayList<java.lang.Integer>> litterAttrSet,
boolean arrowKeyPrompt,
boolean hoverLitter,
long startTime,
int endTime,
int totalLitterCollected,
int totalPlantsPlanted,
boolean hasLost)
Updates the View based on the given parameters.
|
private int |
worldHeightToPixelHeight(int height)
Consumes a height in world coordinates, computes the expected height
in the window (i.e. pixel coordinates).
|
private int |
worldWidthToPixelWidth(int world_width)
Consumes a width in world coordinates, computes the expected width
in the window (i.e. pixel coordinates).
|
private int |
worldXToPixelX(int world_x)
Consumes a x-coordinate in world coordinates, computes the expected
x-coordinate in the window (i.e. pixel coordinates).
|
private int |
worldYToPixelY(int world_y)
Consumes a y-coordinate in world coordinates, computes the expected
y-coordinate in the window (i.e. pixel coordinates).
|
getAccessibleContext, getUI, getUIClassID, paramString, setUI, updateUI
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, hide, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingOrigin, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusDownCycle, validate, validateTree
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
public static final int WORLD_WIDTH
public static final int WORLD_HEIGHT
private static final java.awt.Dimension screenDimension
private static final double screenHeight
private static final double screenWidth
javax.swing.JFrame frame
private static int playerXLoc
private static int playerYLoc
private static PlayerStatus playerStatus
private static int crabXLoc
private static int crabYLoc
private static Direction playerDirection
private static final java.awt.Color BACKGROUND_COLOR
private static final int trashImgCount
private static final int recImgCount
private static final int litterCount
private java.util.ArrayList<java.lang.Integer> pickedUpAttr
private boolean hasLitter
private static java.util.ArrayList<java.util.ArrayList<Sprite.ID>> litterImgLists
private java.util.HashSet<java.util.ArrayList<java.lang.Integer>> litterAttrSet
private java.util.ArrayList<Plant> plants
private java.util.HashSet<Litter> litterSet
TutorialState tutorialState
private boolean tGlow
private boolean rGlow
private int playerHealth
private int animalHealth
private River river
private long startTime
private int endTime
private boolean arrowKeyPrompt
private boolean hoverLitter
private java.util.Map<java.lang.Integer,java.lang.Integer> fontWorldToPt
GamePhase gamePhase
private long endScreenTimestamp
private static final int END_SCREEN_SCORE_TRANSITION_DURATION
private int totalLitterCollected
private int totalPlantsPlanted
private boolean hasLost
private int[] riverTexturePixels
Sprite.ID.RIVER_TEXTURE
, set the first time drawRiver(java.awt.Graphics2D, int)
is run.private int[] riverAlphaMapPixels
Sprite.ID.RIVER_ALPHA
, set the first time drawRiver(java.awt.Graphics2D, int)
os run.public View()
public void setKeyListener(java.awt.event.KeyListener listener)
listener
- The listener to addpublic void paint(java.awt.Graphics g)
paint
in class javax.swing.JComponent
g
- The Graphics
object to use for paintingprivate void drawLitterContainerOverlay(java.awt.Graphics g)
g
- The Graphics
object to use for paintingprivate void drawTutorialOverlays(java.awt.Graphics g)
g
- The Graphics
object to use for paintingprivate void drawHealth(java.awt.Graphics g)
g
- The Graphics
object to use for paintingprivate void drawOverlayBox(java.awt.Graphics g)
g
- The Graphics
object to use for paintingprivate void drawTimer(java.awt.Graphics g)
g
- The Graphics
object to use for paintingprivate void drawStartScreenText(java.awt.Graphics g)
g
- The Graphics
object to use for paintingprivate void drawEndScreenOverlay(java.awt.Graphics g)
g
- The Graphics
object to use for paintingprivate void drawRiver(java.awt.Graphics2D g, int xpos_world)
g
- The graphics object to use for renderingxpos_world
- The x-position of the River, in world coordinatesprivate Sprite.ID getPlayerSprite()
Sprite.ID
to use to render the player. Determines
this based on the player's status
and
direction
.None.
- Sprite.ID
to use to render the playerprivate void drawImage(java.awt.Graphics g, Sprite.ID s, int world_x, int world_y)
Sprite.ID
onto the given Graphics by consuming
world coordinates. Does all conversions necessary to render at the
correct pixel coordinates.private void drawString(java.awt.Graphics g, java.lang.String str, int xpos, int ypos, int height, View.HorizLocation horizLocation, View.VertLocation vertLocation)
g
- The Graphics
object to use for painting.word
- The text to render.width
- The length of the line on which to render the textXPos
- the x-coordinate of the left endpoint of the line, in
world coordinates.YPos
- the y-coordinate of the left endpoint of the line, in
world coordinates.private void setPaint(java.awt.Graphics2D g, Sprite.ID s)
g
- The Graphics
object to use for paintings
- The Sprite.ID
to use as the textureprivate int worldXToPixelX(int world_x)
world_x
- The x-coordinate in world coordinates.private int worldYToPixelY(int world_y)
world_y
- The y-coordinate in world coordinates.private int worldWidthToPixelWidth(int world_width)
width
- in world coordinates.private int worldHeightToPixelHeight(int height)
height
- in world coordinates.private int getFrameWidth()
private int getFrameHeight()
private int getFrameHorizOffset()
0
if the inner frame size is contrained by the
width of the window.private int getFrameVertOffset()
0
if the inner frame size is contrained by
the height of the window.private java.awt.Dimension getFrameDimensions()
private int setFontSize(java.awt.Graphics g, int worldHeight)
Graphics
to match the given height in world coordinates.
Uses the font Times New Roman, Bold.g
- The Graphics to set the font onworldHeight
- The expected font height, in world coordinates.public void update(GamePhase gamePhase, int playerX, int playerY, Direction dir, PlayerStatus status, int crabX, int crabY, java.util.ArrayList<java.lang.Integer> pickedUpAttr, boolean hasLitter, java.util.ArrayList<Plant> plants, boolean tVictory, boolean rVictory, int playerHealth, int animalHealth, River river, TutorialState tutorialState, java.util.HashSet<java.util.ArrayList<java.lang.Integer>> litterAttrSet, boolean arrowKeyPrompt, boolean hoverLitter, long startTime, int endTime, int totalLitterCollected, int totalPlantsPlanted, boolean hasLost)
Litter
, whether the player is currently holding a Litter
, and the most recent Litter
eaten by the animal.
Updates the current game score.gamePhase
- The current game phase.playerX
- The Player's X-location in world coordinates.playerY
- The Player's Y-location in world coordinates.dir
- The current Direction of the Player.status
- The Player's current status.crabX
- The Animal's X-location in world coordinates.crabY
- The Animal's Y-location in world coordinates.playerPickedUpAttr
- The attributes of the most recent litter object picked up by the
player.hasLitter
- Boolean value representing if the Player is currently holding a
Litter object.plants
- All plants to rendertVictory
- Whether the player has recently deposited a Trash.rVictory
- Whether the player has recently deposited a Recyclable.animalHealth
- The health of the Animalriver
- The river, including its position.tutorialState
- The current state of the tutoriallitterAttrSet
- The attributes of all litter to renderarrowKeyPrompt
- Whether to render an arrow key tutorial prompthoverLitter
- Whether the player has touched the litter yet
in the tutorial phasestartTime
- The System.currentTimeMillis()
when the game was started.
Should be 0 immediately after the tutorial ends and normal gameplay begins.endTime
- The number of milliseconds that a game should lasttotalLitterCollected
- The total number of litter objects collected throughout the gametotalPlatsPlanted
- The total number of plants that were replanted throughout the gamehasLost
- Whether the player has lost the game.public Sprite.ID getSpriteID(int lType, int imgID)
lType
- the Litter type whose Sprite ID will be chosen.imgID
- The image id of the given litter.public void preloadLitterImgs()
None.
-