注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

众里寻他千百度,名师成就满分路

AP 微积分 AP 计算机 腾飞的博客

 
 
 

日志

 
 
关于我

大学讲师,中国首批AP计算机教师,著有中国第一套,历经五年实践证明深受学生欢迎的成功的AP计算机双语教材,2013年以93%的满分率开创了中国AP计算机成功的先河,远远超出全美26.6%的满分率,为中国AP计算机教学树立了典范,并在同年加拿大计算机竞赛中勇夺桂冠,任教学生获哥伦比亚大学,麻省理工学院,卡耐基梅隆大学,宾夕法尼亚大学,康奈尔大学,西北大学等学校录取,远程学生遍及北京、长春、南京、重庆、广州、济南, 深圳、成都、费城,洛杉矶,加州,宾州,新罕布什尔州等地,希望借此平台为信息技术的发展做出贡献!

网易考拉推荐

围棋博弈  

2013-05-06 16:17:29|  分类: 默认分类 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
//白子类
public class ChessPoint_white extends Canvas implements MouseListener{
    ChessPad chesspad=null;
    ChessPoint_white(ChessPad p) {
        setSize(20,20);
        chesspad=p;
        addMouseListener(this);
    }
    public void paint(Graphics g){
        g.setColor(Color.white);
        g.fillOval(0,0,20,20);
    }
    public void mousePressed(MouseEvent e){
        if(e.getModifiers()==InputEvent.BUTTON3_MASK){
            chesspad.remove(this);
            chesspad.flag=false;
            chesspad.text2.setText("White First");
            chesspad.text1.setText("");
        }
    }
    public void mouseReleased(MouseEvent e){}
    public void mouseEntered(MouseEvent e){}
    public void mouseExited(MouseEvent e){}
    public void mouseClicked(MouseEvent e){
        if(e.getClickCount()>=2)
            chesspad.remove(this);
    }
}
//黑子类
public class ChessPoint_black extends Canvas implements MouseListener{
    ChessPad chesspad=null;

    ChessPoint_black(ChessPad p) {
        setSize(20,20);
        chesspad=p;
        addMouseListener(this);
    }
    @Override
    public void paint(Graphics g){
        g.setColor(Color.black);
        g.fillOval(0,0,20,20);
    }
    public void mousePressed(MouseEvent e){
        if(e.getModifiers()==InputEvent.BUTTON3_MASK){
            chesspad.remove(this);
            chesspad.flag=true;
            chesspad.text2.setText("");
            chesspad.text1.setText("Black First");
        }
    }
    @Override
    public void mouseReleased(MouseEvent e){}
    public void mouseEntered(MouseEvent e){}
    public void mouseExited(MouseEvent e){}
    
    public void mouseClicked(MouseEvent e){
        if(e.getClickCount()>=2)
            chesspad.remove(this);
    }
}
//棋盘类
public class ChessPad extends Panel implements MouseListener, ActionListener {

    int x = -1, y = -1;
    public boolean flag = true;
    Button button = new Button("Replay");
    public TextField text1 = new TextField("Black First");
    public TextField text2 = new TextField("");

    /** Creates new form ChessPad */
    public ChessPad() {

        setSize(440, 440);
        setLayout(null);
        setBackground(Color.orange);
        addMouseListener(this);
        add(button);

        button.setBounds(10, 5, 60, 26);
        button.addActionListener(this);
        add(text1);
        text1.setBounds(90, 5, 90, 24);
        add(text2);
        text2.setBounds(290, 5, 60, 24);


        text1.setEditable(false);
        text2.setEditable(false);

    }

    public void paint(Graphics g) {
        for (int i = 40; i <= 380; i += 20) {
            g.drawLine(40, i, 400, i);
        }
        g.drawLine(40, 400, 400, 400);
        for (int j = 40; j <= 380; j += 20) {
            g.drawLine(j, 40, j, 400);
        }
        g.drawLine(400, 40, 400, 400);
        g.fillOval(97, 97, 6, 6);
        g.fillOval(337, 97, 6, 6);
        g.fillOval(97, 337, 6, 6);
        g.fillOval(337, 337, 6, 6);
        g.fillOval(217, 217, 6, 6);
    }

    @Override
    public void mousePressed(MouseEvent e) {
        if (e.getModifiers() == InputEvent.BUTTON1_MASK) {
            x = (int) (e.getX());
            y = (int) (e.getY());
            ChessPoint_black chesspoint_black = new ChessPoint_black(this);
            ChessPoint_white chesspoint_white = new ChessPoint_white(this);
            int a = (x + 10) / 20, b = (y + 10) / 20;
            if (x / 20 < 2 || y / 20 < 2 || x / 20 > 19 || y / 20 > 19) {
            } else {
                if (flag) {
                    this.add(chesspoint_black);
                    chesspoint_black.setBounds(a * 20 - 10, b * 20 - 10, 20, 20);
                    flag = false;
                    text2.setText("White First");
                    text1.setText("");
                } else if (flag==false) {
                    this.add(chesspoint_white);
                    chesspoint_white.setBounds(a * 20 - 10, b * 20 - 10, 20, 20);
                    flag = true;
                    text1.setText("Black First");
                    text2.setText("");
                }
            }
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        this.removeAll();
        flag = true;
        add(button);
        button.setVisible(true);
        button.setBackground(Color.red);
        button.setBounds(10, 5, 60, 26);
        add(text1);
        text1.setBounds(90, 5, 90, 24);
        add(text2);
        text2.setText("");
        text1.setText("Black First");
        text2.setBounds(290, 5, 90, 24);
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }

    /** 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.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 477, Short.MAX_VALUE));
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 359, Short.MAX_VALUE));
    }// </editor-fold>
    // Variables declaration - do not modify
    // End of variables declaration

    @Override
    public void mouseClicked(MouseEvent e) {
    }
}

//主类
public class Chess extends Frame {
    ChessPad chesspad=new ChessPad();
    public Chess() {
        setVisible(true);
        setLayout(null);
        Label label=new Label("click start,double eat",Label.CENTER);
        add(label);
        label.setBounds(70,55,440,26);
       // label.setBackground(Color.orange);
        add(chesspad);
        chesspad.setBounds(70,90,440, 440);
        addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){
                System.exit(0);
            }
        });
       pack();
       setSize(600,550);
    }

    /** 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.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 592, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Chess.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Chess.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Chess.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Chess.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        Chess chess=new Chess();
      
    }
    // Variables declaration - do not modify
    // End of variables declaration


}


  评论这张
 
阅读(93)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017