📄 jboard.java
字号:
/* Author: <insert your name here> ugrad.cs.ubc.ca address: <insert your ugrad.cs.ubc.ca e-mail address here> Date: <insert date here> By submitting this file, I acknowledge that the person whose name appears above is the *only* author of the code required to solve this assignment.*/package cs111.tetris.gui;//TODO: Make the JBoard instance redraw itself when the board state changes./* * This is the starter version of the JBoard.java file. * * The code is almost complete except for the implementation of the * listener relationship between the JBoard graphical component and * the underlying "model" (an instance of Game). * * To fix this you must: * * 1) make this class implement the GameListener interface * and put appropriate code in your definitions of the methods * of this interface to cause the board to redraw itself. * * Hints: * * - you are only interested in the board state but * you must implement all methods (so says the compiler!). * => you should put an empty method body {} if you don't * want to do anything for a given method. * * - ***IMPORTANT*** The proper * way to trigger a repaint is to call the repaint method * inherited from JComponent! You should * not call paintComponent directly! * * 2) Just making the class implement the interface is not * enough. * You should also register this component as a listener * with the game. Otherwise the game won't call your methods * and nothing will happen. * * Hints: * - You need to call a method of the Game class to register * a listener. * - you need to call this method from somewhere in this class * but there is no TO DO comment to mark the spot. So you * yourself should decide on a good place to insert that call. */import java.awt.Color;import java.awt.Dimension;import java.awt.Graphics;import java.awt.Rectangle;import javax.swing.JComponent;import cs111.tetris.data.Board;import cs111.tetris.data.Game;/** * JGame is a UI widget that constitutes a view on a tetris * game state model (instance of Game). It displays the board * in a graphical way. It listens to the Game * for state change notifications and updates the graphical * display as needed. * <p> * See also the explanation of "Model View Controller" in * <a href="package-summary.html">the * package summary</a>. * * @author Kris De Volder (based heavily on code by Nick Parlante). */public final class JBoard extends JComponent { private Game game; /** * Create a JBoard (GUI view that display a tetris board) for a * given tetris game. * * @param game The datastructure that represents the tetris game state (Model). */ public JBoard(Game game) { super(); this.game = game; setPreferredSize(new Dimension(game.getWidth()*16+2, (game.getHeight()+game.getTopSpace())*16+2)); } /* Pixel helpers. These centralize the translation of (x,y) coords that refer to blocks in the board to (x,y) coords that count pixels. Centralizing these computations here is the only prayer that repaintPiece() and paintComponent() will be consistent. The +1's and -2's are to account for the 1 pixel rect around the perimeter. */ // width in pixels of a block private final float dX() { return( ((float)(getWidth()-2)) / game.getWidth() ); } // height in pixels of a block private final float dY() { return( ((float)(getHeight()-2)) / game.getBoard().getHeight() ); } // the x pixel coord of the left side of a block private final int xPixel(int x) { return(Math.round(1 + (x * dX()))); } // the y pixel coord of the top of a block private final int yPixel(int y) { return(Math.round(getHeight() -1 - (y+1)*dY())); } /** * This method overrides the one from the superclass * This is where we put the code that draws a * our board on the screen. */ public void paintComponent(Graphics g) { // Draw a rect around the whole thing g.drawRect(0, 0, getWidth()-1, getHeight()-1); // Draw the line separating the top int spacerY = yPixel(game.getBoard().getHeight() - game.getTopSpace() - 1); g.drawLine(0, spacerY, getWidth()-1, spacerY); // check if we are drawing with clipping //Shape shape = g.getClip(); Rectangle clip = null; clip = g.getClipBounds(); // Factor a few things out to help the optimizer final int dx = Math.round(dX()-2); final int dy = Math.round(dY()-2); final int bWidth = game.getBoard().getWidth(); int x, y; // Loop through and draw all the blocks // left-right, bottom-top for (x=0; x<bWidth; x++) { int left = xPixel(x); // the left pixel // right pixel (useful for clip optimization) int right = xPixel(x+1) -1; // skip this x if it is outside the clip rect if (clip!=null) { if ((right<clip.x) || (left>=(clip.x+clip.width))) continue; } Board board = game.getBoard(); // draw from 0 up to the col height final int yHeight = board.getColumnHeight(x); for (y=0; y<yHeight; y++) { if (board.getGrid(x, y)) { final boolean filled = (board.getRowWidth(y)==bWidth); if (filled) g.setColor(Color.green); g.fillRect(left+1, yPixel(y)+1, dx, dy); // +1 to leave a white border if (filled) g.setColor(Color.black); } } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -