⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄

📁 实现8张图片的拼接
💻
📖 第 1 页 / 共 2 页
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.ImageIcon;
import java.awt.Color;
import java.io.*;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.lang.Throwable;
import java.util.*;

class Person extends JButton  //按纽类的定义与实现
{
	int number;
        ImageIcon  image;
	Person(int number,ImageIcon image)
	{
                this.number = number;
                this.image=image;
          }
}

class pintu extends JFrame implements ActionListener, MouseListener,KeyListener,Runnable
{       Thread shijian=new Thread(this);
        long time1,time2,shijiancha,sec,mic,min,count;
        int casecount;
        String s;
        Person person[] = new Person[8];//声明拼图游戏的元素,拼图游戏各元素
        JButton left, right, blelow, above;
        boolean Flag=false,tflag=false,moveflag=true,Tflag=false;
        JButton btshow, tianchong,restart,EXIT;
        TextField text1,text3;
        TextArea text2;
        JLabel label;
        JMenuBar menubar;
        JMenu   menu,Level;
        JMenuItem Close,Introduction,RandomSelect,Select_Select,Start;
       public pintu()
	{   
              
               setBounds(200, 200, 200, 200);
		setVisible(true);
                addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				System.exit(0);
			}
		}
		);
      //初始化窗体的个组件以及相关容器
	       
                 setLayout(null);
                 btshow = new JButton();
                 tianchong=new JButton();
                 restart = new JButton("AGAIN");
                 EXIT=new JButton("EXIT");
                 text1 = new TextField();
                 text2=new TextArea();
                 text3=new TextField();
                 label=new JLabel("说明");
                add(btshow);
	        add(label);
                add(text1);
                add(text2);
                add(text3);
                add(tianchong);
                label.setVisible(true);
                label.setBounds(100,50,50,50);
                btshow.setBounds(700,0,600,700);
                btshow.setIcon(new ImageIcon("E:/2/10.gif"));
                text1.setBounds(100, 0, 150, 50);
                text1.addActionListener(this);
                text2.setBounds(150,50,473,50);
                text2.setVisible(false);
                text3.setBounds(250,0,150,50);
                label.setVisible(false);
                menubar=new JMenuBar();
                menu=new JMenu();
                Level=new JMenu("Level");
                Close=new JMenuItem("Close");
                Start=new JMenuItem("Start");
                RandomSelect=new JMenuItem("RandomSelect");
                Select_Select=new JMenuItem("Select_Select");
                Introduction=new JMenuItem("Introduction");
                Close.setMnemonic(KeyEvent.VK_E); 
                Close.addActionListener(this);
                Introduction.addActionListener(this);
                RandomSelect.addActionListener(this);
                Select_Select.addActionListener(this);
                restart.addActionListener(this);
                EXIT.addActionListener(this);
                Start.addActionListener(this);
                add(restart);
                add(EXIT);
                EXIT.setBounds(400,0,110,50);
                restart.setBounds(512,0,112,50);
                menu.add(Close);
                menu.add(Introduction);
                menu.add(Level);
                menu.add(Start);
                Level.add(RandomSelect);
                Level.add(Select_Select);
                menubar.add(menu);
                setJMenuBar(menubar);
                for (int i = 0; i < 8; i++)//确定各按钮的初始位置与图片,形成“一对一”的关系模式
		{
			person[i] = new Person(i,new ImageIcon());
			person[i].addMouseListener(this);
			person[i].addKeyListener(this);
			add(person[i]);
                  }
                 left = new JButton();
		right = new JButton();
		above = new JButton();
		blelow = new  JButton();
                tianchong.setVisible(false);
                person[0].setIcon(new ImageIcon("E:/2/"+1+".gif"));  //图片与按钮形成一对一,便于控制后可能出现的情形
	        person[1].setIcon(new ImageIcon("E:/2/"+2+".gif"));
		person[2].setIcon(new ImageIcon("E:/2/"+3+".gif"));
		person[3].setIcon(new ImageIcon("E:/2/"+4+".gif"));
	        person[4].setIcon(new ImageIcon("E:/2/"+5+".gif"));
		person[5].setIcon(new ImageIcon("E:/2/"+6+".gif"));
		person[6].setIcon(new ImageIcon("E:/2/"+7+".gif"));
	        person[7].setIcon(new ImageIcon("E:/2/"+8+".gif"));
                person[0].setBounds(100, 100, 170, 190);//初始图片位置
                person[1].setBounds(275, 100,170, 190);
                person[2].setBounds(450, 480, 170, 190);
                person[3].setBounds(100, 290, 170,190);
                person[4].setBounds(275, 290, 170, 190);
                person[5].setBounds(450, 290, 170, 190);
                person[6].setBounds(100, 480, 170, 190);
                person[7].setBounds(450,100,170,190);
                person[2].requestFocus();
                time1= System.currentTimeMillis();
               text1.setFont(new   Font("Times   New   Roman",   0,   18));
                validate();//让所有组件都正常显示出来
              }
	public void keyTyped(KeyEvent e) { }
	public void keyReleased(KeyEvent e) { }
	public void keyPressed(KeyEvent e)//四个方向键的功能实现
	{
		Person man = (Person)e.getSource();
		if (e.getKeyCode() == KeyEvent.VK_DOWN)
		{
		    go(man, blelow);
		}
		if (e.getKeyCode() == KeyEvent.VK_UP)
		{ 
                    go(man, above);
                }
		if (e.getKeyCode() == KeyEvent.VK_LEFT)
		{ 
                    go(man, left);
                }
		if (e.getKeyCode() == KeyEvent.VK_RIGHT)
		{ 
                    go(man, right); 
                }

   }
	public void mousePressed(MouseEvent e)//点击鼠标后的处理
	{
		Person man = (Person)e.getSource();
		int x = -1, y = -1;
		x = e.getX();
		y = e.getY();
		System.out.println("x=" + x + "y=" + y);
                int w = man.getBounds().width;
		int h = man.getBounds().height;
		boolean flag = false;
                label.setVisible(false);
                text1.setVisible(true);
                text2.setVisible(false);
                text3.setVisible(true);
		if ((y > h / 2))
		{
			flag = go(man, blelow);
                 }
		if ((!flag) && (y < h / 2))
		{
			flag = go(man, above);
                  }
		if ((!flag) && x < w / 2)
		{
			flag = go(man, left);
                 }
		if ((!flag) && x > w / 2)
		{
			flag = go(man, right);
                 }
                 if(check())
                 {           
                    getimage();
                    tianchong.setVisible(true);
                    text1.setText("");
                    text1.setFont(new   Font("Times   New   Roman",   0,   20));   
                    text1.setText("恭喜你,顺利通过!");
                    return;
                 }
                count=count+1;
                text1.setText("你已经点击了"+String.valueOf(count)+"次"); //显示点击图片次数,做为衡量游戏者的水平
 }
	public void mouseReleased(MouseEvent e) { }//鼠标的各种事件
	public void mouseEntered(MouseEvent e) { }
	public void mouseExited(MouseEvent e) { }
	public void mouseClicked(MouseEvent e) { }
	public boolean go(Person man, JButton direction)//控制按钮的移动
	{
		boolean move = true;
		Rectangle manRect = man.getBounds();
		int x = man.getLocation().x;
		int y = man.getLocation().y;
		if( (direction == blelow)&&(y<=290))
		{
			y = y + 190;
	         }
		else if((direction == above)&&(y>=290) )
		{
			y = y - 190;
	        }
		else if ((direction == left)&&(x>=275))
		{
			x = x - 175;
	         }
		else if ((direction == right)&&(x<=275) )
		{
			x = x + 175;
		 }
                manRect.setLocation(x, y);
                for (int k = 0; k <= 7; k++)
		{
			Rectangle personRect = person[k].getBounds();
			if (manRect.intersects(personRect) && (man != person[k]))
			{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -