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

古城风~~~

竹密无妨溪水过,天高不碍白云飞。这天下总有一份是属于我古城的天地!

 
 
 

日志

 
 

棋盘  

2008-08-07 04:46:30|  分类: java/jsp |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

import java.awt.Button;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Label;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Chess extends Frame {
 private static final long serialVersionUID = 1L;
 ChessPad chessPad;
 Chess(){
  this.setLayout(null);
  Label label=new Label("单击左键下棋子,双击吃棋子,用右键单击棋子悔棋",Label.CENTER);
  label.setBounds(70, 55, 440, 26);
  label.setBackground(Color.orange);
  this.add(label);
  chessPad=new ChessPad();
  chessPad.setBounds(70, 90, 440, 440);
  this.add(chessPad);
  this.addWindowListener(new WindowAdapter(){
   public void windowClosing(WindowEvent we){
    System.exit(0);
   }
  });
  this.setBounds(150, 150, 600, 550);
  this.setVisible(true);
  this.validate();
  //this.pack();
 }

 public static void main(String[] args) {
  new Chess();
 }

}


class ChessPad extends Panel implements MouseListener, ActionListener {

 private static final long serialVersionUID = 1L;

 int x = -1, y = -1, color = 1;
 Button button;
 Label label1, label2;

 ChessPad() {
  this.setSize(440, 440);
  this.setLayout(null);
  this.setBackground(Color.orange);
  button = new Button("重新开局");
  button.setBounds(40, 5, 60, 26);
  button.addActionListener(this);
  this.add(button);
  label1 = new Label("请黑棋下子");
  label2 = new Label();
  label1.setForeground(Color.blue);
  label2.setForeground(Color.blue);
  label1.setBounds(160, 5, 90, 24);
  label2.setBounds(290, 5, 90, 24);
  this.add(label1);
  this.add(label2);
  this.addMouseListener(this);
 }

 public void paint(Graphics g) {
  for (int i = 40; i <= 400; i += 20) {
   g.drawLine(40, i, 400, i);
  }
  for (int j = 40; j <= 400; j += 20) {
   g.drawLine(j, 40, j, 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);
 }

 public void mouseClicked(MouseEvent arg0) {

 }

 public void mouseEntered(MouseEvent arg0) {

 }

 public void mouseExited(MouseEvent arg0) {

 }

 public void mousePressed(MouseEvent me) {
  // 当按下鼠标左键时下棋子
  if (me.getModifiers() == InputEvent.BUTTON1_MASK) {
   x = (int) me.getX();
   y = (int) me.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||x/20>19||y/20<2||y/20>19){
   }else{
    if(color==1){
     this.add(chesspoint_black);
     chesspoint_black.setBounds(a*20-10, b*20-10, 20, 20);
     color=color*(-1);
     label2.setText("请白棋下子");
     label1.setText("");
    }else if(color==-1){
     this.add(chesspoint_white);
     chesspoint_white.setBounds(a*20-10, b*20-10, 20, 20);
     color=color*(-1);
     label2.setText("");
     label1.setText("请黑棋下子");
    }
   }
  }
 }

 public void mouseReleased(MouseEvent arg0) {

 }

 public void actionPerformed(ActionEvent ae) {
  this.removeAll();
  color=1;
  button.setBounds(40, 5, 60, 26);
  this.add(button);
  label1.setText("请黑棋下子");
  label2.setText("");
  label1.setBounds(160, 5, 90, 24);
  label2.setBounds(290, 5, 90, 24);
  this.add(label1);
  this.add(label2);

 }

}

class ChessPoint_black extends Canvas implements MouseListener {

 private static final long serialVersionUID = 1L;

 ChessPad chesspad=null;
 
 ChessPoint_black(ChessPad p) {
  this.setSize(20, 20);
  this.chesspad=p;
  this.addMouseListener(this);

 }
 
 public void paint(Graphics g){
  g.setColor(Color.black);
  g.fillOval(0, 0, 20, 20);
 }

 public void mouseClicked(MouseEvent e) {
  if(e.getClickCount()>=2){
   chesspad.remove(this);
  }

 }

 public void mouseEntered(MouseEvent e) {

 }

 public void mouseExited(MouseEvent e) {

 }

 public void mousePressed(MouseEvent e) {
  if(e.getModifiers()==InputEvent.BUTTON3_MASK){
   chesspad.remove(this);
   chesspad.color=1;
   chesspad.label1.setText("请黑棋下子");
   chesspad.label2.setText("");
  }

 }

 public void mouseReleased(MouseEvent e) {

 }

}

class ChessPoint_white extends Canvas implements MouseListener {

 private static final long serialVersionUID = 1L;

 ChessPad chesspad=null;
 
 ChessPoint_white(ChessPad p) {
  this.setSize(20, 20);
  this.chesspad=p;
  this.addMouseListener(this);
 }
 
 public void paint(Graphics g){
  g.setColor(Color.white);
  g.fillOval(0, 0, 20, 20);
 }


 public void mouseClicked(MouseEvent e) {
  if(e.getClickCount()>=2){
   chesspad.remove(this);
  }
 }

 public void mouseEntered(MouseEvent e) {

 }

 public void mouseExited(MouseEvent e) {

 }

 public void mousePressed(MouseEvent e) {
  if(e.getModifiers()==InputEvent.BUTTON3_MASK){
   chesspad.remove(this);
   chesspad.color=-1;
   chesspad.label2.setText("请白棋下子");
   chesspad.label1.setText("");
  }
 }

 public void mouseReleased(MouseEvent e) {

 }

}

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

历史上的今天

评论

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

页脚

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