📄 painter.java
字号:
/********************************************************************************************/
/* */
/* Painter.java */
/* */
/* 画布显示模块,实现了Observer接口,直接监听与格点有关的事件 */
/* */
/* Programed by Luo Pengkui on 2004-9 */
/* */
/********************************************************************************************/
import javax.swing.*;
import java.awt.*;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Observable;
import java.util.Observer;
import java.awt.geom.Ellipse2D;
//------------------------------------ BEGIN ------------------------------------------------
public class Painter implements Observer // 实现Observer接口
{
SnakeControl m_control = null;
SnakeModel m_snake = null;
MatrixModel m_matrix;
FoodModel[] m_foods;
Color m_mapColor;
Canvas m_paintCanvas;
JLabel m_labelScore;
JLabel m_labelEatenNum;
public static int canvasWidth = 200;
public static int canvasHeight = 300;
public static final int nodeWidth = 10;
public static final int nodeHeight = 10;
public static final int EMPTY = 0;
public static final int FRUIT = 1;
public static final int SNAKE = 2;
public static final int WALL = 3;
public Painter(SnakeModel snake, SnakeControl control,
FoodModel[] foods, MatrixModel matrix, Color mapColor,
int canvasCol, int canvasRow, Container cp )
{
m_snake = snake;
m_control = control;
m_foods = foods;
m_matrix = matrix;
m_mapColor = mapColor;
canvasWidth = canvasCol*nodeWidth;
canvasHeight = canvasRow*nodeHeight;
// 创建顶部的分数显示
m_labelScore = new JLabel(" Score:");
cp.add(m_labelScore, BorderLayout.NORTH);
// 创建中间的游戏显示区域
m_paintCanvas = new Canvas();
m_paintCanvas.setSize(canvasWidth + 1, canvasHeight + 1);
m_paintCanvas.addKeyListener(m_control);
cp.add(m_paintCanvas, BorderLayout.CENTER);
}
void repaint()
{
Graphics g = m_paintCanvas.getGraphics();
Graphics2D g2d = ( Graphics2D )g;
// 填充背景
g.setColor(m_mapColor);
g.fillRect(nodeWidth, nodeHeight, canvasWidth-2*nodeWidth, canvasHeight-2*nodeHeight);
// 画果实
g.setColor(Color.GREEN);
for ( int j=0; j<m_foods.length; j++ )
{
Point position = m_foods[j].mb_getPosition();
if ( m_matrix.mb_getGridState( position.x, position.y ) == FRUIT )
{
g2d.fill( new Ellipse2D.Double(
(position.x)*nodeWidth-1, (position.y)*nodeHeight-1 ,
nodeWidth, nodeHeight ) );
}
}
// 过贪吃蛇
g.setColor(Color.RED);
LinkedList na = m_snake.mb_getNodeArray();
Iterator it = na.iterator();
while (it.hasNext())
{
Point n = (Point) it.next();
drawNode(g, n);
g.setColor(Color.BLACK);
}
updateScore();
}
private void drawNode(Graphics g, Point n) // 画一个格点
{
g.fillRect(n.x * nodeWidth, n.y * nodeHeight, nodeWidth - 1, nodeHeight - 1);
}
public void updateScore() // 更新分数
{
String s = "Score: " + m_snake.mb_getScoreModel().mb_getScore();
m_labelScore.setText(s);
}
public void update(Observable o, Object arg) // 被setChanged()和notifyObservers()激活
{
repaint();
}
}
//------------------------------------- END -------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -