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、运行后效果

885534-20160802233317762-2055335530.png

转载于:https://www.cnblogs.com/laixiaolian/p/5731189.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值