java—连连看GUI

1、连连看棋盘图形化

package Link;

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 {
	ImageIcon imageIcon = new ImageIcon("Images/build/BackGround.jpg");
	Image imageBackground = imageIcon.getImage();

	static int arr[][] = new int[8][10];
	
	static{
		Random random = new Random();
		for (int i = 0; i < 20; i++) {
			int count = 0;
			while (count < 4) {
				int x = random.nextInt(8);
				int y = random.nextInt(10);
				if (arr[x][y] == 0) {
					arr[x][y] = i;
					count++;
				}
			}
		}
	}

	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();
		}
	}

	public TestGUI() {
		this.setTitle("连连看");// 设置 标题

		this.setSize(1000, 650);// 设置宽高

		this.setLocationRelativeTo(null);// 自动适配到屏幕中间

		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设置关闭模式
		this.setVisible(true);// 设置可见

		ImageIcon imageIcon = new ImageIcon("Images/build/biao.png");
		Image image = imageIcon.getImage();
		this.setIconImage(image);// 设置连连看窗体图标
	}

	@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(imageBackground, 0, 0, this);// 设置背景图片

		g.drawString("连连看", 400, 100);

		for (int i = 0; i < 8; i++) {
			for (int j = 0; j < 10; j++) {
				g.drawImage(chessImage[arr[i][j]], 200 + (j * 55), 150 + (i * 55), this);
			}
		}

	}

	public static void main(String[] args) {
		
		new TestGUI();

	}
}

2、运行后效果

原文地址:https://www.cnblogs.com/laixiaolian/p/5731189.html