PicBoard.java
上传用户:jennyfly
上传日期:2021-08-10
资源大小:735k
文件大小:5k
- package view;
- import java.awt.*;
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.Point;
- import java.awt.Rectangle;
- import java.awt.Shape;
- import java.awt.geom.Line2D;
- import java.util.ArrayList;
- import javax.swing.JPanel;
- import javax.swing.border.BevelBorder;
- import model.Model;
- /**
- *
- * @author 何晓飞
- * @version 2.0
- */
- public class PicBoard extends JPanel {
- private static final long serialVersionUID = 1L;
- /* 该类中需要的Model对象 */
- private Model model;
- /* 小图片的高度 */
- private int cellHeight;
- /* 小图片的宽度 */
- private int cellWidth;
- /*画笔用来绘制连线*/
- private Graphics2D g2;
-
- /**
- * GamePanel (构造函数)初始化类变量并设置GamePanel属性.
- *
- * @param model
- * 该类中需要的Model对象.
- */
- public PicBoard(Model model) {
- /*只被调用过一次,故无法实现数据更新*/
- this.model = model;
- cellWidth=model.getCellWidth();
- cellHeight=model.getCellHeight();
- setBackground(new Color(0,0,0));
- setBorder(new BevelBorder(BevelBorder.RAISED));
- }
- /**
- * 画出消去图片时显示的连接路径
- *
- * @param obj
- * 连接路径中的Point对象集
- * drawLink 方法的原理在于使用线程采集矩形对象画出线框,对符合消去的矩形画出
- * 路径.
- *
- */
- public void drawLink() {
- /*
- * 获取关键点
- */
- ArrayList<Point> points = model.getKeyPoints();
- ArrayList<Shape> shapes = new ArrayList<Shape>();
- int step = 80;
- // 添加路径起点
- shapes.add(model.pointToRect(points.get(0)));
- // 添加路径终点
- shapes.add(model.pointToRect(points.get(points.size() - 1)));
- // 添加路线
- for (int i = 1; i < points.size(); i++) {
- Point p1 = model.getCenter(model.pointToRect(points.get(i - 1)));
- Point p2 = model.getCenter(model.pointToRect(points.get(i)));
- if (p1.x == p2.x) {// 画横向的线
- int distance = p2.y - p1.y;// 计算线长
- for (int j = 0; j <= Math.abs(distance); j += step)
- shapes.add(new Line2D.Double(p1, new Point(p1.x,
- p1.y + distance / Math.abs(distance) * j)));// 生成一条线并加到shaps中
- } else if (p1.y == p2.y) {// 画纵向的线
- int distance = p2.x - p1.x;// 计算线长
- for (int j = 0; j <= Math.abs(distance); j += step)
- shapes.add(new Line2D.Double(p1, new Point(
- p1.x + distance/ Math.abs(distance) * j,
- p1.y)));// 生成一条线并加到shapes中
- }
- shapes.add(new Line2D.Double(p1, p2));// 构造并初始化一个具有坐标 (0,
- }
- float dash[] = { 1F };
- for (int index = 0; index < shapes.size(); index++) {
- if (index < 2)
- {
- g2.setColor(new Color(0, 0, 255));// 设置圈图片的颜色
- g2.setStroke(new BasicStroke(1.0F, BasicStroke.CAP_SQUARE,
- BasicStroke.JOIN_BEVEL, 10.0F, dash, 1.F));
- }
- else{
- g2.setColor(new Color(30, 144, 255));// 设置连线的颜色
- g2.setStroke(new BasicStroke(1.5F, BasicStroke.CAP_SQUARE,
- BasicStroke.JOIN_BEVEL, 10.0F, dash, 1.F));
- }
- g2.draw(shapes.get(index));// 使用当前 Graphics2D 上下文的设置勾画
- // Shape 的轮廓
- }
-
- try {// 连线显示0.4秒
- Thread.sleep(400);
- repaint();
- } catch (InterruptedException e) {
- }
-
- }
-
- /**
- * 根据调用model.getMaps()方法返回的二维数组map[][]画游戏中的小图片
- *
- * @param g
- * 该类中需要的Graphics对象,通过调用Graphics的drawImages方法画游戏中的小图片
- */
- public void paint(Graphics g) {
- Graphics2D g1=(Graphics2D)g;
- /*图片区域的大小*/
- setSize(820, 485);
- //设置背景色
- g1.setColor(Model.BG_COLOR);
- //填充背景
- g1.fillRect(0,0, getWidth(), getHeight());
- g1.setColor(new Color(118,238,0 ));
- //绘制方格
- for(int col=3;col<17;col++){
- for(int row=1;row<11;row++){
- int x=41*col-2;
- int y=41*row-2;
- g1.draw3DRect(x, y,41, 41, true);
- }
- }
- int map[][] = model.getMaps();// 获得Model中保存游戏中存在小图片位置的数组
- for (int i = 0; i < map.length; i++)
- for (int j = 0; j < map[0].length; j++) {
- Rectangle rect = model.matrixToRect(i, j);// 指定了坐标空间中的一个区域
- // 图片已被消去则跳出本次循环,否则把它画出来
- if (map[i][j] == 0)
- continue;
- g1.drawImage(ImageLoader.getImageOf(map[i][j],model.getPictureFile()),
- rect.x, rect.y, cellWidth, cellHeight, this);
- }
- // 画出游戏图片被选中时显示的小框
- if (model.getCellSelected() != null) {// 图片被选中
- Rectangle rect = model.pointToRect(model.getCellSelected());// 指定图片所在的区域
- g1.setColor(new Color(148, 0, 211 ));
- float dash[] = { 2f };
- g1.setStroke(new BasicStroke(1.0F, BasicStroke.CAP_SQUARE,
- BasicStroke.JOIN_BEVEL, 10.0F, dash, 0.F));// 画出规定样式的小筐
- g1.draw3DRect(rect.x,rect.y,rect.width,rect.height,true);
- }
- /*
- * 画笔必须在此处赋值,否则无法正确获取画笔,如果在drawLink中调用则会导致闪屏
- */
- g2 = (Graphics2D)this.getGraphics();
- }
- }