围棋源代码Java Applet小程序.docx
《围棋源代码Java Applet小程序.docx》由会员分享,可在线阅读,更多相关《围棋源代码Java Applet小程序.docx(15页珍藏版)》请在冰点文库上搜索。
![围棋源代码Java Applet小程序.docx](https://file1.bingdoc.com/fileroot1/2023-7/23/425aebe2-9825-4856-be69-4673ed2fc5e1/425aebe2-9825-4856-be69-4673ed2fc5e11.gif)
围棋源代码JavaApplet小程序
/*一个Java围棋程序源代码(2010-04-2621:
20:
19)转载标签:
java初步*/
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.JOptionPane;
publicclassChessextendsFrame
{
ChessPadchesspad=newChessPad();
Chess()
{
add(chesspad);
chesspad.setBounds(70,90,440,440);
Labellabel=newLabel("clicktopoint,doubled_clicktoremove,rightclicktoback",Label.CENTER);
add(label);
label.setBounds(70,55,440,24);
label.setBackground(Color.orange);
addWindowListener
(
newWindowAdapter()
{
publicvoidwindowClosing(WindowEvente)
{
System.exit(0);
}
}
);
setLayout(null);
setVisible(true);
setSize(600,550);
}
publicstaticvoidmain(Stringargs[])
{
Chesschess=newChess();
}
}
classChessPadextendsPanelimplementsMouseListener,ActionListener,FocusListener
{
intx=-1,y=-1,chessColor=1;
Stringblackname="",whitename="";
Buttonbutton=newButton("start");
ButtoninputButton=newButton("input");
TextFieldtext_1=newTextField("blackplease"),
text_2=newTextField(""),//whiteplease
text_3=newTextField("black'name"),
text_4=newTextField("white'name");
ChessPad()
{
add(inputButton);
inputButton.setBounds(35,5,60,26);
inputButton.addActionListener(this);
inputButton.addFocusListener(this);
add(text_3);
text_3.setBounds(115,5,90,24);
text_3.addFocusListener(this);
text_3.setEditable(true);
add(text_4);
text_4.setBounds(315,5,90,24);
text_4.addFocusListener(this);
text_4.setEditable(true);
add(button);
button.setBounds(35,36,60,26);
button.setEnabled(false);
button.addActionListener(this);
add(text_1);
text_1.setBounds(115,36,90,24);
text_1.setEnabled(false);
text_1.setEditable(false);
add(text_2);
text_2.setBounds(315,36,90,24);
text_2.setEnabled(false);
text_2.setEditable(false);
setSize(440,440);
setLayout(null);
setBackground(Color.orange);
addMouseListener(this);
}
publicvoidpaint(Graphicsg)
{
for(inti=80;i<=400;i=i+20)
{
g.drawLine(40,i,400,i);
}
g.drawLine(40,420,400,420);
for(intj=40;j<=380;j=j+20)
{
g.drawLine(j,80,j,420);
}
g.drawLine(400,80,400,420);
g.fillOval(97,137,6,6);
g.fillOval(337,137,6,6);
g.fillOval(97,377,6,6);
g.fillOval(337,377,6,6);
g.fillOval(217,257,6,6);
}
publicvoidfocusGained(FocusEvente)
{
Componentcom=(Component)e.getSource();
if(com==text_3)
{
text_3.setText("");
}
elseif(com==text_4)
{
text_4.setText("");
}
}
publicvoidfocusLost(FocusEvente)
{
}
publicvoidmousePressed(MouseEvente)
{
if(blackname.length()==0||whitename.length()==0)
{
JOptionPane.showMessageDialog(this,"pleaseinputtheplayer'sname,andclickinputbuttonbeforeyoustartchess!
","reminder",JOptionPane.WARNING_MESSAGE);
}
elseif(blackname.equals("black'name"))
{
JOptionPane.showMessageDialog(this,"pleaseinputtheblackplayer'sname,andclickinputbutton","reminder",JOptionPane.WARNING_MESSAGE);
}
elseif(whitename.equals("white'name"))
{
JOptionPane.showMessageDialog(this,"pleaseinputthewhiteplayer'sname,andclickinputbutton","reminder",JOptionPane.WARNING_MESSAGE);
}
elseif(e.getModifiers()==InputEvent.BUTTON1_MASK)
{
x=(int)e.getX();
y=(int)e.getY();
inta=(x+10)/20,b=(y+10)/20;
ChessPoint_blackchesspoint_black=newChessPoint_black(this);
ChessPoint_whitechesspoint_white=newChessPoint_white(this);
if(x/20<2||y/20<4||x/20>19||y/20>20)
{
}
else
{
if(chessColor==1)
{
this.add(chesspoint_black);
chesspoint_black.setBounds(a*20-10,b*20-10,20,20);
chessColor=chessColor*(-1);
text_2.setText(this.whitename+"please");
text_1.setText("");
}
elseif(chessColor==-1)
{
this.add(chesspoint_white);
chesspoint_white.setBounds(a*20-10,b*20-10,20,20);
chessColor=chessColor*(-1);
text_1.setText(this.blackname+"please");
text_2.setText("");
}
}
}
}
publicvoidmouseReleased(MouseEvente)
{
}
publicvoidmouseEntered(MouseEvente)
{
}
publicvoidmouseExited(MouseEvente)
{
}
publicvoidmouseClicked(MouseEvente)
{
}
publicvoidactionPerformed(ActionEvente)
{
if(e.getSource()==inputButton)
{
blackname=text_3.getText();
whitename=text_4.getText();
if(blackname.length()==0||whitename.length()==0)
{
JOptionPane.showMessageDialog(this,"youdidnotcompletetheinformation!
","reminder",JOptionPane.WARNING_MESSAGE);
}
elseif(blackname.equals("black'name"))
{
JOptionPane.showMessageDialog(this,"pleaseinputtheblackplayer'sname","reminder",JOptionPane.WARNING_MESSAGE);
}
elseif(whitename.equals("white'name"))
{
JOptionPane.showMessageDialog(this,"pleaseinputthewhiteplayer'sname","reminder",JOptionPane.WARNING_MESSAGE);
}
else
{
inputButton.setEnabled(false);
text_3.removeFocusListener(this);
text_3.setEnabled(false);
text_4.setEnabled(false);
button.setEnabled(true);
text_1.setEnabled(true);
text_1.setText(blackname+"please");
text_2.setEnabled(true);
}
}
elseif(e.getSource()==button)
{
inputButton.setEnabled(true);
text_3.setEnabled(true);
text_4.setEnabled(true);
button.setEnabled(false);
text_1.setEnabled(false);
text_2.setEnabled(false);
this.removeAll();
chessColor=1;
add(button);
button.setBounds(35,36,60,26);
add(text_1);
text_1.setBounds(115,36,90,24);
text_1.setText("blackplease");
add(text_2);
text_2.setBounds(315,36,90,24);
text_2.setText("");
add(inputButton);
inputButton.setBounds(35,5,60,26);
add(text_3);
text_3.setText("black'name");
text_3.addFocusListener(this);
text_3.setBounds(115,5,90,24);
add(text_4);
text_4.setText("white'name");
text_4.setBounds(315,5,90,24);
blackname="";
whitename="";
}
}
}
classChessPoint_blackextendsCanvasimplementsMouseListener
{
ChessPadchesspad=null;
ChessPoint_black(ChessPadp)
{
setSize(20,20);
chesspad=p;
addMouseListener(this);
}
publicvoidpaint(Graphicsg)
{
g.setColor(Color.black);
g.fillOval(0,0,20,20);
}
publicvoidmousePressed(MouseEvente)
{
if(e.getModifiers()==InputEvent.BUTTON3_MASK)
{
chesspad.remove(this);
chesspad.chessColor=1;
chesspad.text_2.setText("");
chesspad.text_1.setText(chesspad.blackname+"please");
}
}
publicvoidmouseReleased(MouseEvente)
{
}
publicvoidmouseEntered(MouseEvente)
{
}
publicvoidmouseExited(MouseEvente)
{
}
publicvoidmouseClicked(MouseEvente)
{
if(e.getClickCount()>=2)
{
chesspad.remove(this);
}
}
}
classChessPoint_whiteextendsCanvasimplementsMouseListener
{
ChessPadchesspad=null;
ChessPoint_white(ChessPadp)
{
setSize(20,20);
chesspad=p;
addMouseListener(this);
}
publicvoidpaint(Graphicsg)
{
g.setColor(Color.white);
g.fillOval(0,0,20,20);
}
publicvoidmousePressed(MouseEvente)
{
if(e.getModifiers()==InputEvent.BUTTON3_MASK)
{
chesspad.remove(this);
chesspad.chessColor=-1;
chesspad.text_1.setText("");
chesspad.text_2.setText(chesspad.whitename+"please");
}
}
publicvoidmouseReleased(MouseEvente)
{
}
publicvoidmouseEntered(MouseEvente)
{
}
publicvoidmouseExited(MouseEvente)
{
}
publicvoidmouseClicked(MouseEvente)
{
if(e.getClickCount()>=2)
{
chesspad.remove(this);
}
}
}
/*PS:
功能介绍
游戏操作:
左击下子,右击悔棋,双击吃子(本程序还无程序判断能力)
输入:
输入选手名,然后点击“input”即可开始游戏,点击“Start”从新开始。
*/