java—连连看-实现封装
1、封装
Chess.java
package Linkup;
/**
* 棋子封装类
*
* @author laixl
*
*/
public class Chess {
// 图片的 状态 //1.....20
// 0表示消掉
private int status;
public Chess(int status) {
this.status = status;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
}
Param.java
package Linkup;
import java.awt.Image;
import javax.swing.ImageIcon;
public class Param {
//游戏总行数与总列数
public static int rows=8;
public static int cols=10;
//棋子图标 宽与高
public static int chessWidth=55;
public static int chessHeight=55;
//棋盘到边界的距离
public static int marginWidth = 200;
public static int marginHeight = 150;
//游戏的背景图片
public static Image imageBackground = new ImageIcon("Images/build/BackGround.jpg").getImage();
public static Image[] chessImage = new Image[20];
static {
for (int i = 0; i < chessImage.length; i++) {
chessImage[i] = new ImageIcon("Images/build/" + (i + 1) + ".png").getImage();
}
}
}
MapPanel.java
package Linkup;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Stroke;
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;
import javax.swing.JPanel;
/**
* 棋盘面板
*
* @author laixl
*
*/
public class MapPanel extends JPanel implements MouseListener {
Chess[][] arr = new Chess[Param.rows][Param.cols];
// 粗线条
Stroke stroke = new BasicStroke(3.0f);
public MapPanel() {
initArr();
this.addMouseListener(this);
}
public void initArr() {
Random random = new Random();
for (int i = 1; i <= 20; i++) {
int count = 0;
while (count < 4) {
int x = random.nextInt(8);
int y = random.nextInt(10);
if (arr[x][y] == null) {
arr[x][y] = new Chess(i);
count++;
}
}
}
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.green);
Font font = new Font("宋体", Font.BOLD, 28);
g.setFont(font); // 设置字体颜色和字体大小
g.drawImage(Param.imageBackground, 0, 0, this);// 设置背景图片
g.drawString("连连看", 400, 100);
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
int x = j * Param.chessWidth;
int y = i * Param.chessHeight;
g.drawImage(Param.chessImage[arr[i][j].getStatus() - 1], x
+ Param.marginWidth, y + Param.marginHeight, this);
}
}
}
@Override
public void mouseClicked(MouseEvent e) {}
@Override
public void mousePressed(MouseEvent e) {
if (e.getModifiers() != InputEvent.BUTTON1_MASK) {
return;
}
int x = e.getX();
int y = e.getY();
int X = (x - Param.marginWidth) / Param.chessWidth;
X = X * Param.chessWidth + Param.marginWidth;
int Y = (y - Param.marginHeight) / Param.chessHeight;
Y = Y * Param.chessHeight + Param.marginHeight;
Graphics g = getGraphics();
Graphics2D g2d = (Graphics2D) g;
g2d.setStroke(stroke);
g2d.setColor(Color.blue);
if ((x >= Param.marginWidth && x <= Param.marginWidth + Param.cols
* Param.chessWidth)
&& (y >= Param.marginHeight && y <= Param.marginHeight
+ Param.rows * Param.chessHeight)) {
g2d.drawRect(X, Y, Param.chessWidth, Param.chessHeight);
}
}
@Override
public void mouseReleased(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
}
TestGUI.java
package Linkup;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
public class TestGUI extends JFrame {
MapPanel mapPanel = new MapPanel();
public TestGUI() {
this.add(mapPanel);
this.setTitle("连连看");// 设置 标题
this.setSize(1000, 650);// 设置宽高
this.setLocationRelativeTo(null);// 自动适配到屏幕中间
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设置关闭模式
this.setResizable(false);
this.setVisible(true);// 设置可见
ImageIcon imageIcon = new ImageIcon("Images/Photos/serverstop.gif");
Image image = imageIcon.getImage();
this.setIconImage(image);// 设置连连看窗体图标
}
public static void main(String[] args) {
new TestGUI();
}
}
通过鼠标点击,选中该图标,在图标外加一层边框