| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1445 人关注过本帖
标题:一个简单的华容道(可以用鼠标拖曳)
只看楼主 加入收藏
jmasm
Rank: 1
等 级:新手上路
威 望:1
帖 子:368
专家分:0
注 册:2006-9-24
收藏
 问题点数:0 回复次数:0 
一个简单的华容道(可以用鼠标拖曳)
网上对人物的行动方向都是判断鼠标按下时在按扭上的位置来确定,这种方法有个缺点当出现2个方向可以走的时候,就容易出问题了,他们的解决方法是通过两次单击来解决,第一次单击后在按扭上出现一个方向指针,第二次点击对应的方向移动,
这种操作方法很不方便,我的解决方法就是使用鼠标拖曳。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MoveExample
{
    public static void main(String args[])
    {
        new Hua_Rong_Road();
    }
}
class Person extends JButton
{
    int number;
    Person(int number,Icon i)
    {
        super(i);
        this.number=number;
    }
}
class Hua_Rong_Road extends JFrame implements MouseListener,MouseMotionListener,KeyListener,ActionListener
{
    Person person[]=new Person[10];
    JButton left,right,above,below;
    JButton restart=new JButton("重新开始");
    JLabel jlabel=new JLabel("关卡:");
    JTextField jtf=new JTextField("横刀立马");
    JButton help=new JButton("帮助");
    Person man;
    boolean leftButton,rightButton;
    Rectangle manRect;
    boolean keyState=false;
    int fx,fy;
    int bx,by;
    public Hua_Rong_Road()
    {
        super("华容道");
        init();
        setBounds(100,100,320,380);
        setResizable(false);
        setVisible(true);
        validate();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public void init()
    {
        setLayout(null);
        getContentPane().add(restart);
        getContentPane().add(help);
        getContentPane().add(jlabel);
        getContentPane().add(jtf);
        jlabel.setBounds(60,20,60,30);
        jtf.setBounds(100,20,60,30);
        help.setBounds(180,20,60,25);
        restart.setBounds(100,320,120,25);
        restart.addActionListener(this);
        help.addActionListener(this);
        for(int k=0;k<10;k++)
        {
            person[k]=new Person(k,new ImageIcon("images/"+(k+1)+".gif"));
            person[k].addMouseListener(this);
            person[k].addKeyListener(this);
            person[k].addMouseMotionListener(this);
            getContentPane().add(person[k]);
        }
        person[0].setBounds(104,54,100,100);
        person[1].setBounds(104,154,100,50);
        person[2].setBounds(54,154,50,100);
        person[3].setBounds(204,154,50,100);
        person[4].setBounds(54,54,50,100);
        person[5].setBounds(204,54,50,100);
        person[6].setBounds(54,254,50,50);
        person[7].setBounds(204,254,50,50);
        person[8].setBounds(104,204,50,50);
        person[9].setBounds(154,204,50,50);
        person[9].requestFocus();
        left=new JButton();
        right=new JButton();
        above=new JButton();
        below=new JButton();
        getContentPane().add(left);
        getContentPane().add(right);
        getContentPane().add(above);
        getContentPane().add(below);
        left.setBounds(49,49,5,260);
        right.setBounds(254,49,5,260);
        above.setBounds(49,49,210,5);
        below.setBounds(49,304,210,5);
        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,below);
        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)
    {
        if(e.getButton()==3)
            rightButton=true;
        if(e.getButton()==1)
            leftButton=true;
        if (keyState==false)
        {
            keyState=true;
            man=(Person)e.getSource();
            manRect=man.getBounds();
            bx=e.getX();
            by=e.getY();
        }
    }
    public void mouseDragged(MouseEvent e)//鼠标拖曳控件
    {
        if (keyState)
        {    
            int nowX=man.getX();
            int nowY=man.getY();
            int x=e.getX();
            int y=e.getY();
            man.setLocation(nowX+(x-bx),nowY+(y-by));
        }
    }

    public void mouseReleased(MouseEvent e){
        boolean move=true;
        keyState=false;
        revise(man);  //修正按扭的位置
        Rectangle rectA=man.getBounds();
        //与九个人物比较
        for(int k=0;k<10;k++)
        {
            if((man.number!=k))
            {    
                Rectangle rectB=person[k].getBounds();
                if(rectA.intersects(rectB))
                {
                    move=false;
                    break;
                }
            }
        }
        //与边界上的4个按扭比较
        if(move!=false)
        {
            if(rectA.intersects(left.getBounds()))
                move=false;
            if(rectA.intersects(right.getBounds()))
                move=false;
            if(rectA.intersects(above.getBounds()))
                move=false;
            if(rectA.intersects(below.getBounds()))
                move=false;
        }
        if (move==false)
                man.setLocation(manRect.x,manRect.y); //还原位置
        if(leftButton && rightButton)
        {
            person[0].setLocation(104,204);
            leftButton=false;
            rightButton=false;
        }
        if (person[0].getX()==104 && person[0].getY()==204) //判断胜利
        {
            winner();
            dispose();
            new Hua_Rong_Road();
        }
    }
    public void winner()
    {
        JOptionPane.showMessageDialog(this,"哇,你太厉害啦!!!!!!","华容道",3);
    }
    public void revise(Person manA)   //修正按扭A的位置
    {
        //要保证x,y只有一个坐标会被改变
        boolean change=false;
        int ax=manA.getX();
        int ay=manA.getY();
        int distanceX=ax-manRect.x;
        int distanceY=ay-manRect.y;
        //System.out.println(distanceX+" "+distanceY);
        if(Math.abs(distanceX)>=13)
        {
            distanceX=(distanceX>0)?50:-50;
            change=true;
        }
        else
            distanceX=0;
        if(Math.abs(distanceY)>=13)
            distanceY=(distanceY>0)?50:-50;
        else
            distanceY=0;
        if (change==true)
            manA.setLocation(manRect.x+distanceX,manRect.y);
        else
            manA.setLocation(manRect.x,manRect.y+distanceY);
    }
    public void mouseEntered(MouseEvent e){}
    public void mouseExited(MouseEvent e){}
    public void mouseClicked(MouseEvent e){}
    public void mouseMoved(MouseEvent e){}
    public void go(Person man,JButton direction)
    {
        boolean move=true;
        Rectangle manRect=man.getBounds();
        int x=man.getBounds().x;
        int y=man.getBounds().y;
        if(direction==below)
            y+=50;
        if(direction==above)
            y-=50;
        if(direction==left)
            x-=50;
        if(direction==right)
            x+=50;
        manRect.setLocation(x,y);
        Rectangle directionRect=direction.getBounds();
        for(int k=0;k<10;k++)
        {
            Rectangle personRect=person[k].getBounds();
            if (man.number!=k)
                if(manRect.intersects(personRect))
                {
                    move=false;
                    break;
                }
        }
        if(manRect.intersects(directionRect))
            move=false;
        if(move==true)
            man.setLocation(x,y);
    }
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource()==restart)
        {
        dispose();
        new Hua_Rong_Road();
        }
        if(e.getSource()==help)
        {
        JOptionPane.showMessageDialog(null,"       华容道是一个古老的智力游戏,被誉为"+"\n"+"“智力游戏界三大不可思议之一”,得到了"+"\n"+"广泛的传播。你的任务就是通过移动各棋子"+"\n"+"来把被围困的曹操走出重围,来到出口位置。" +"\n"+"\n"+"                    加油,祝你好运","华容道游戏帮助信息",3);    
        }
    }
}

附件解压后java MoveExample运行程序

[[it] 本帖最后由 jmasm 于 2008-6-19 09:18 编辑 [/it]]

[[it] 本帖最后由 jmasm 于 2008-6-19 09:24 编辑 [/it]]

hrd.rar (113.42 KB)
搜索更多相关主题的帖子: 华容道 鼠标 number import 拖曳 
2008-06-19 09:13
快速回复:一个简单的华容道(可以用鼠标拖曳)
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016820 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved