cs1705.tetris
Class JTetris

java.lang.Object
  extended byjava.awt.Component
      extended byjava.awt.Container
          extended byjavax.swing.JComponent
              extended bycs1705.tetris.JTetris
All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable
Direct Known Subclasses:
JBrainTetris

public class JTetris
extends JComponent

JTetris presents a Tetris game in a window. It handles the GUI and the animation. The Piece and Board classes handle the lower-level computations. This code is provided in finished form for the students.

Version:
2003.10.13
Author:
Stephen Edwards (based on Nick Parlante's original)
See Also:
Serialized Form

Nested Class Summary
 
Nested classes inherited from class javax.swing.JComponent
JComponent.AccessibleJComponent
 
Field Summary
 int DELAY
           
static int DOWN
           
static int DROP
           
static int HEIGHT
           
static int LEFT
           
static int RIGHT
           
static int ROTATE
           
 int TEST_LIMIT
           
static int TOP_SPACE
           
static int WIDTH
           
 
Fields inherited from class javax.swing.JComponent
TOOL_TIP_TEXT_KEY, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
 
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, PROPERTIES, SOMEBITS
 
Method Summary
 void addNewPiece()
          Tries to add a new random at the top of the board.
 void computeNewPosition(int verb)
          Figures a new position for the current piece based on the given verb (LEFT, RIGHT, ...).
 Container createControlPanel()
          Creates the panel of UI controls.
static void main(String[] args)
          Creates a Window, installs the JTetris or JBrainTetris, checks the testMode state, install the controls in the WEST.
 void paintComponent(Graphics g)
          Draws the current board with a 1 pixel border around the whole thing.
 Piece pickNextPiece()
          Selects the next piece to use using the random generator set in startGame().
 void repaintPiece(Piece piece, int x, int y)
          Given a piece and a position for the piece, generates a repaint for the rectangle that just encloses the piece.
 int setCurrent(Piece piece, int x, int y)
          Given a piece, tries to install that piece into the board and set it to be the current piece.
 void startGame()
          Sets the internal state and starts the timer so the game is happening.
 void stopGame()
          Stops the game.
 void tick(int verb)
          Called to change the position of the current piece.
 void updateTimer()
          Updates the timer to reflect the current setting of the speed slider.
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addPropertyChangeListener, addPropertyChangeListener, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAccessibleContext, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getGraphics, getHeight, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPreferredSize, getPropertyChangeListeners, getPropertyChangeListeners, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getUIClassID, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isPreferredSizeSet, isRequestFocusEnabled, isValidateRoot, paint, paintImmediately, paintImmediately, print, printAll, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFont, setForeground, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update, updateUI
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, remove, remove, removeAll, removeContainerListener, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, setLayout, transferFocusBackward, transferFocusDownCycle, validate
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, contains, createImage, createImage, createVolatileImage, createVolatileImage, dispatchEvent, enable, enableInputMethods, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMouseWheelListeners, getName, getParent, getPeer, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DELAY

public final int DELAY
See Also:
Constant Field Values

DOWN

public static final int DOWN
See Also:
Constant Field Values

DROP

public static final int DROP
See Also:
Constant Field Values

HEIGHT

public static final int HEIGHT
See Also:
Constant Field Values

LEFT

public static final int LEFT
See Also:
Constant Field Values

RIGHT

public static final int RIGHT
See Also:
Constant Field Values

ROTATE

public static final int ROTATE
See Also:
Constant Field Values

TEST_LIMIT

public final int TEST_LIMIT
See Also:
Constant Field Values

TOP_SPACE

public static final int TOP_SPACE
See Also:
Constant Field Values

WIDTH

public static final int WIDTH
See Also:
Constant Field Values
Method Detail

addNewPiece

public void addNewPiece()
Tries to add a new random at the top of the board. Ends the game if it's not possible.


computeNewPosition

public void computeNewPosition(int verb)
Figures a new position for the current piece based on the given verb (LEFT, RIGHT, ...). The board should be in the committed state -- i.e. the piece should not be in the board at the moment. This is necessary so Board.rowAfterDrop(Piece,int) may be called without the piece "hitting itself" on the way down. Sets the ivars newX, newY, and newPiece to hold what it thinks the new piece position should be. (Storing an intermediate result like that in ivars is a little tacky.)


createControlPanel

public Container createControlPanel()
Creates the panel of UI controls.


main

public static void main(String[] args)
Creates a Window, installs the JTetris or JBrainTetris, checks the testMode state, install the controls in the WEST.


paintComponent

public void paintComponent(Graphics g)
Draws the current board with a 1 pixel border around the whole thing. Uses the pixel helpers above to map board coords to pixel coords. Draws rows that are filled all the way across in green.


pickNextPiece

public Piece pickNextPiece()
Selects the next piece to use using the random generator set in startGame().


repaintPiece

public void repaintPiece(Piece piece,
                         int x,
                         int y)
Given a piece and a position for the piece, generates a repaint for the rectangle that just encloses the piece.


setCurrent

public int setCurrent(Piece piece,
                      int x,
                      int y)
Given a piece, tries to install that piece into the board and set it to be the current piece. Does the necessary repaints. If the placement is not possible, then the placement is undone, and the board is not changed. The board should be in the committed state when this is called. Returns the same error code as Board.place().


startGame

public void startGame()
Sets the internal state and starts the timer so the game is happening.


stopGame

public void stopGame()
Stops the game.


tick

public void tick(int verb)
Called to change the position of the current piece. Each key press call this once with the verbs LEFT RIGHT ROTATE DROP for the user moves, and the timer calls it with the verb DOWN to move the piece down one square. Before this is called, the piece is at some location in the board. This advances the piece to be at its next location. Overriden by the brain when it plays.


updateTimer

public void updateTimer()
Updates the timer to reflect the current setting of the speed slider.