java编写的简单图片匹配游戏

package Chapter12;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
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;

public class Demo8 extends JFrame implements MouseMotionListener,MouseListener{

	private JLabel img[] = new JLabel[5];
	private JLabel target[] = new JLabel[5];
	private Point pressPoint;
	
	public Demo(){
		super();
		setTitle("简单图片匹配游戏");
		setBounds(100, 100, 536, 401);
		getContentPane().setLayout(new BorderLayout());
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JPanel imgPanel = new JPanel();
		imgPanel.setLayout(null);
		imgPanel.setOpaque(false);//使标签透明
		setGlassPane(imgPanel);
		getGlassPane().setVisible(true);
		
		ImageIcon icon[] = new ImageIcon[5];
		icon[0] = new ImageIcon(getClass().getResource("kafei.png"));
		icon[1] = new ImageIcon(getClass().getResource("xianshiqi.png"));
		icon[2] = new ImageIcon(getClass().getResource("xiyiji.png"));
		icon[3] = new ImageIcon(getClass().getResource("yifu.png"));
		icon[4] = new ImageIcon(getClass().getResource("zixingche.png"));
		
		JPanel bottomPanel = new JPanel();
		bottomPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 5));
		getContentPane().add(bottomPanel,BorderLayout.SOUTH);
		for(int i=0;i<5;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));
			int y = (int) (Math.random()*(getHeight()-150));
			img[i].setLocation(x, y);
			img[i].addMouseListener(this);
			img[i].addMouseMotionListener(this);
			imgPanel.add(img[i]);
			
			target[i] = new JLabel();
			target[i].setBackground(Color.ORANGE);
			target[i].setOpaque(true);//设置标签不透明,以便设置标签背景颜色
			target[i].setHorizontalAlignment(SwingConstants.CENTER);//设置文本与图片水平居中
			target[i].setVerticalTextPosition(SwingConstants.BOTTOM);//设置文本显示在图片下方
			target[i].setPreferredSize(new Dimension(80, 80));//设置图片大小
			target[i].setHorizontalTextPosition(SwingConstants.CENTER);//设置文本居中对齐
			bottomPanel.add(target[i]);
		}
		target[0].setText("咖啡");
		target[1].setText("显示器");
		target[2].setText("洗衣机");
		target[3].setText("衣服");
		target[4].setText("自行车");
	//	setVisible(true);
	}
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Demo8 demo8 = new Demo8();
					demo8.setVisible(true);
				}
				catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mousePressed(MouseEvent e) {
		pressPoint = e.getPoint();
	}
	@Override
	public void mouseReleased(MouseEvent e) {
		if(check()){//配对成功
			getGlassPane().setVisible(false);
			for(int i=0;i<5;i++){
				target[i].setText("配对成功");
				target[i].setIcon(img[i].getIcon());
			}
		}
	}
	private boolean check() {
		boolean result = true;
		for(int i=0;i<5;i++){
			Point location = img[i].getLocationOnScreen();
			Point seat = target[i].getLocationOnScreen();
			target[i].setBackground(Color.GREEN);
			//如果配对失败
			if(location.x<seat.x||location.y<seat.y||location.x>seat.x+80||location.y>seat.y+80){
				target[i].setBackground(Color.ORANGE);
				result = false;
			}
		}
		return result;
	}
	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
    @Override
	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);
	}
	@Override
	public void mouseMoved(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值