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

📄 gameai.java

📁 追击问题 小兔子追胡萝卜 并且累计追上的次数
💻 JAVA
字号:
/*
 * GameAI.java
 *
 * Created on 2007年3月15日, 下午1:08
 */

package gameai;
import javax.swing.*;
/**
 *
 * @author  Administrator
 */
public class GameAI extends javax.swing.JFrame implements Runnable{
    
    /** Creates new form GameAI */
    
    public final static int RABBIT = 0;
    public final static int PATTERN= 1;
    private int choose = 0;
    private JFrame th;
    private AIPattern pattern;
    public GameAI() {
        th = this;
        pattern = new AIPattern();
        pattern.setBounds(0,0,620,360);
        pattern.setVisible(false);
        this.add(pattern);
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" 生成的代码 ">//GEN-BEGIN:initComponents
    private void initComponents() {
        canvas = new javax.swing.JPanel();
        pre = new javax.swing.JLabel();
        qre = new javax.swing.JLabel();
        start = new javax.swing.JButton();
        stop = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        canvas.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createTitledBorder(""), "GameAI", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Dialog", 0, 13), new java.awt.Color(255, 0, 0)));
        pre.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gameai/rabbit.png")));

        qre.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gameai/Carrot.png")));

        org.jdesktop.layout.GroupLayout canvasLayout = new org.jdesktop.layout.GroupLayout(canvas);
        canvas.setLayout(canvasLayout);
        canvasLayout.setHorizontalGroup(
            canvasLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(canvasLayout.createSequentialGroup()
                .add(pre)
                .addContainerGap(538, Short.MAX_VALUE))
            .add(org.jdesktop.layout.GroupLayout.TRAILING, canvasLayout.createSequentialGroup()
                .addContainerGap(350, Short.MAX_VALUE)
                .add(qre)
                .add(194, 194, 194))
        );
        canvasLayout.setVerticalGroup(
            canvasLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, canvasLayout.createSequentialGroup()
                .add(qre, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 49, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 221, Short.MAX_VALUE)
                .add(pre))
        );

        start.setText("Start");
        start.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startActionPerformed(evt);
            }
        });

        stop.setText("Stop");
        stop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                stopActionPerformed(evt);
            }
        });

        jButton1.setText("Change Panel");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jSeparator1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 645, Short.MAX_VALUE)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(canvas, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18))
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jButton1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 386, Short.MAX_VALUE)
                .add(stop)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(start)
                .add(18, 18, 18))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(canvas, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jSeparator1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 18, Short.MAX_VALUE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButton1)
                    .add(stop)
                    .add(start))
                .addContainerGap())
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        choose = 1-choose;
        if(choose==RABBIT){
            pattern.setVisible(false);
            canvas.setVisible(true);
        }
        else if(choose==PATTERN){
            pattern.setVisible(true);
            canvas.setVisible(false);
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void stopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_stopActionPerformed
        if(choose==RABBIT){
            if(sir!=null){
                bSir = false;
                ch  = false;
                sir = null;
                num = 0;
                th.setTitle("I catch it "+(++num)+" times.");
            }
        }
        else if(choose==PATTERN){
            pattern.stop();
        }
    }//GEN-LAST:event_stopActionPerformed
    private Thread sir = null;
    private Thread wir = null;
    private boolean ch = true;
    private boolean bSir = true;
    private void startActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startActionPerformed
        if(choose==RABBIT){
            if(sir==null){
                sir = new Thread(this);
                bSir = true;
                sir.start();
                ch=true;
                wir = new Thread(){
                    public void run(){
                        while(ch){
                            try{
                                Thread.sleep(10);
                            }catch(Exception e){}
                            if(qre.getLocation().x >= canvas.getWidth() ||
                                    qre.getLocation().y >= canvas.getHeight()){
                                qre.setLocation((int)Math.round(Math.random()*300),(int)Math.round(Math.random()*300));
                            }    
                            else if(Math.abs(pre.getLocation().x-qre.getLocation().x)<=3 &&
                                     Math.abs(pre.getLocation().y-qre.getLocation().y)<=3) {
                                th.setTitle("I catch it "+(++num)+" times.");
                                qre.setLocation((int)Math.round(Math.random()*300),(int)Math.round(Math.random()*300));

                            }
                            else{
                                qre.setLocation(qre.getLocation().x+speed,qre.getLocation().y+speed);
                            }
                        }
                    }
                };
                wir.start();
            }
        }
        else if(choose==PATTERN){
            pattern.start();
        }
    }//GEN-LAST:event_startActionPerformed
    private int num = 0;
    private int speed = 1;
    public void run(){
        pre.getLocation();
        qre.getLocation();
        int x=0, y=0;
        while(bSir){
            try{
                Thread.sleep(7);
            }catch(Exception e){}
            if(pre.getLocation().x < qre.getLocation().x){
                x=pre.getLocation().x+speed;
            }
            else if(pre.getLocation().x > qre.getLocation().x){
                x=pre.getLocation().x-speed;
            }
            if(pre.getLocation().y > qre.getLocation().y){
                y=pre.getLocation().y-speed;
            }
            else if(pre.getLocation().y < qre.getLocation().y){
                y=pre.getLocation().y+speed;
            }
            pre.setLocation(x,y);
        }
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try{
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                }catch(Exception e){}
                new GameAI().setVisible(true);
            }
        });
    }
    
    // 变量声明 - 不进行修改//GEN-BEGIN:variables
    private javax.swing.JPanel canvas;
    private javax.swing.JButton jButton1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel pre;
    private javax.swing.JLabel qre;
    private javax.swing.JButton start;
    private javax.swing.JButton stop;
    // 变量声明结束//GEN-END:variables
    
}

⌨️ 快捷键说明

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