您的位置:首页 > 编程语言 > Java开发

Java课程学习十一:图片匹配游戏

2017-11-24 16:11 274 查看

图片匹配游戏

引用转载请注明出处,Thanks!

通过为标签空间添加图形以及鼠标事件,完成图片配对的游戏。[1]

JFrame 简介:

Swing的三个基本构造块:标签、按钮和文本字段;但是需要个地方安放它们,并希望用户知道如何处理它们。JFrame

类就是解决这个问题的——它是一个容器,允许程序员把其他组件添加到它里面,把它们组织起来,并把它们呈现给用户。 JFrame 实际上不仅仅让程序员把组件放入其中并呈现给用户。比起它表面上的简单性,它实际上是 Swing 包中最复杂的组件。为了最大程度地简化组件,在独立于操作系统的 Swing组件与实际运行这些组件的操作系统之间,JFrame 起着桥梁的作用。JFrame 在本机操作系统中是以窗口的形式注册的,这么做之后,就可以得到许多熟悉的操作系统窗口的特性:最小化/最大化、改变大小、移动。Java 事件模型基础:监听器、事件、事件源、事件注册。

Java的GUI程序的基本思路是以JFrame为基础,它是屏幕上window的对象,能够最大化、最小化、关闭。[2]

JFrame 测试:

HelloWorldSwing.java[3]

import javax.swing.*;
public class HelloWorldSwing {
/**{
* 创建并显示GUI。出于线程安全的考虑,
* 这个方法在事件调用线程中调用。
*/
private static void createAndShowGUI() {
// 确保一个漂亮的外观风格
JFrame.setDefaultLookAndFeelDecorated(true);

// 创建及设置窗口
JFrame frame = new JFrame("HelloWorldSwing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// 添加 "Hello World" 标签
JLabel label = new JLabel("Hello World");
frame.getContentPane().add(label);

// 显示窗口
frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {
// 显示应用 GUI
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}


配对游戏源码:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;

@SuppressWarnings("serial")
public class PictureMatchingFrame extends JFrame implements MouseListener,
MouseMotionListener {
private JLabel img[] = new JLabel[3];// 显示图标的标签
private JLabel targets[] = new JLabel[3];// 窗体下面显示文字的标签
private Point pressPoint; // 鼠标按下时的起始坐标

public static void main(String args[]) {
PictureMatchingFrame frame = new PictureMatchingFrame(); // 创建本类对象
frame.setVisible(true); // 设置窗体为可视状态
}

public PictureMatchingFrame() {
super();
getContentPane().setLayout(new BorderLayout());
setBounds(100, 100, 364, 312);
setTitle("图片配对游戏");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel imagePanel = new JPanel();
imagePanel.setLayout(null);
imagePanel.setOpaque(false);
setGlassPane(imagePanel);
getGlassPane().setVisible(true);
ImageIcon icon[] = new ImageIcon[3];
icon[0] = new ImageIcon(getClass().getResource("screen.png"));
icon[1] = new ImageIcon(getClass().getResource("clothing.png"));
icon[2] = new ImageIcon(getClass().getResource("bike.png"));
final JPanel bottomPanel = new JPanel();
bottomPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 5));
getContentPane().add(bottomPanel, BorderLayout.SOUTH);
for (int i = 0; i < 3; i++) {
img[i] = new JLabel(icon[i]); // 创建图像标签
img[i].setSize(50, 50); // 设置标签大小
img[i].setBorder(new LineBorder(Color.GRAY)); // 设置线性边框
int x = (int) (Math.random() * (getWidth() - 50)); // 随机生成X坐标
int y = (int) (Math.random() * (getHeight() - 150));// 随机生成Y坐标
img[i].setLocation(x, y); // 设置随机坐标
img[i].addMouseListener(this); // 为每个图像标签添加鼠标事件监听器
img[i].addMouseMotionListener(this);
imagePanel.add(img[i]); // 添加图像标签到图像面板
targets[i] = new JLabel(); // 创建匹配位置标签
targets[i].setOpaque(true); // 使标签不透明,以设置背景色
targets[i].setBackground(Color.ORANGE); // 设置标签背景色
targets[i].setHorizontalTextPosition(SwingConstants.CENTER); // 设置文本与图像水平居中
targets[i].setVerticalTextPosition(SwingConstants.BOTTOM); // 设置文本显示在图像下方
targets[i].setPreferredSize(new Dimension(80, 80)); // 设置标签首先大小
targets[i].setHorizontalAlignment(SwingConstants.CENTER); // 文字居中对齐
bottomPanel.add(targets[i]); // 添加标签到底部面板
}
targets[0].setText("显示器"); // 设置匹配位置的文本
targets[1].setText("衣服");
targets[2].setText("自行车");
}

public void mouseClicked(MouseEvent e) {
}

public void mouseMoved(MouseEvent e) {
}

public void mouseEntered(MouseEvent e) {
}

public void mouseExited(MouseEvent e) {
}

public void mousePressed(MouseEvent e) {
pressPoint = e.getPoint(); // 保存拖放图片标签时的起始坐标
}

public void mouseReleased(MouseEvent e) {
if (checkPosition()) { // 如果配对正确
getGlassPane().setVisible(false);
for (int i = 0; i < 3; i++) { // 遍历所有匹配位置的标签
targets[i].setText("匹配成功"); // 设置正确提示
targets[i].setIcon(img[i].getIcon()); // 设置匹配的图标
}
}
}

/**
* 鼠标拖动控件时的事件处理方法
*/
public void mouseDragged(MouseEvent e) {
JLabel source = (JLabel) e.getSource(); // 获取事件源控件
Point imgPoint = source.getLocation(); // 获取控件坐标
Point point = e.getPoint(); // 获取鼠标坐标
source.setLocation(imgPoint.x + point.x - pressPoint.x, imgPoint.y
+ point.y - pressPoint.y); // 设置控件新坐标
}

private boolean checkPosition() {// 检查配对是否正确
boolean result = true;
for (int i = 0; i < 3; i++) {
Point location = img[i].getLocationOnScreen(); // 获取每个图像标签的位置
Point seat = targets[i].getLocationOnScreen(); // 获取每个对应位置的坐标
targets[i].setBackground(Color.GREEN); // 设置匹配后的颜色
// 如果配对错误
if (location.x < seat.x || location.y < seat.y
|| location.x > seat.x + 80 || location.y > seat.y + 80) {
targets[i].setBackground(Color.ORANGE); // 回复对应位置的颜色
result = false; // 检测结果为false
}
}
return result; // 返回检测结果
}
}






引用:

[注释1]:JAVA开发实战1200例(第二卷)

[注释2]:JFrame:http://blog.csdn.net/Changer_Wu/article/details/51595165

[注释3]:SWing简介:http://www.runoob.com/w3cnote/java-swing-demo-intro.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java 图片 程序员 游戏