📄 boardpanel.java
字号:
package edu.rit.cs.mlr5773.connectfour;import java.awt.*;import java.util.*;/** * GUI Object to view a Connect 4 Board * * @author Mark Roth */public class BoardPanel extends Panel implements Observer { /** The Board we are observing */ private Board board; /* The color to represent each player by: */ private static final Color[] playerColor = { Color.lightGray, Color.red, Color.black }; /** * Creates a new Board Panel, and adds this object as an observer to * the game board. */ public BoardPanel(Board board){ this.board = board; board.addObserver( this ); } /** * Draws a single piece on the board */ private void drawPiece( Graphics g, int x, int y, Color c ) { g.setColor( c ); g.fillOval( x*50 + 10, (5-y)*50 + 10, 50 - 10, 50 - 10); g.setColor( Color.white ); g.drawOval( x*50 + 10-1, (5-y)*50 + 10-1, 50 - 10, 50 - 10); g.setColor( new Color( 128, 128, 0 ) ); g.drawOval( x*50 + 10, (5-y)*50 + 10, 50 - 10, 50 - 10); } /** * Returns the desired size of the panel */ private Dimension getGridSize() { return new Dimension( 7*50 + 10, 6*50 + 10); } /** * Gets the maximum size of this object */ public Dimension getMaximumSize() { return getGridSize(); } /** * Gets the minimum size of this object */ public Dimension getMinimumSize() { return getGridSize(); } /** * Gets the preferred size of this object */ public Dimension getPreferredSize() { return getGridSize(); } /** * Draws this board to the given Graphics object */ public void paint( Graphics g ) { int x, y; g.setColor( Color.yellow ); g.fillRect( 0, 0, 7*50 + 10, 6*50 + 10 ); for( y = 0; y < 6; y++ ) { for( x = 0; x < 7; x++ ) { drawPiece( g, x, y, playerColor[board.getAt( x, y )] ); } } } /** * Incrementally draws this object */ public void update( Observable source, Object hint ) { Point move = (Point)hint; Board board = (Board)source; int player = board.getAt( move.x, move.y ); drawPiece( getGraphics(), move.x, move.y, playerColor[ player ] ); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -