java围棋程序
package cn.edu.swufe.game.chess;import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
class ChessPad extends Panel implements MouseListener,ActionListener,FocusListener
{
int x = -1;
int y = -1;
int color = 1;
String blackplayer="";
String whiteplayer="";
Button inbutton = new Button("INPUT");
Button button = new Button("restart");
TextField text_1 = new TextField();
TextField text_2 = new TextField();
TextField text_3 = new TextField("black player's name");
TextField text_4 = new TextField("white player's name");
ChessPad()
{
setSize(440,440);
setLayout(null);
setBackground(Color.orange);
addMouseListener(this);
add(inbutton);
inbutton.setBounds(35,5,60,26);
inbutton.addActionListener(this);
inbutton.addFocusListener(this);
add(text_3);
text_3.setBounds(115,5,130,28);
text_3.addFocusListener(this);
text_3.setEditable(true);
add(text_4);
text_4.setBounds(280,5,130,28);
text_4.addFocusListener(this);
text_4.setEditable(true);
add(button);
button.setBounds(35,36,60,26);
button.addActionListener(this);
button.setEnabled(false);
add(text_1);
text_1.setBounds(115,36,130,28);
text_1.setEditable(false);
text_1.setEnabled(false);
add(text_2);
text_2.setBounds(280,36,130,28);
text_2.setEditable(false);
text_2.setEnabled(false);
}
public void paint(Graphics g)
{
for(int i=80 ; i<=400 ; i=i+20)
{
g.drawLine(40,i,400,i);
}
g.drawLine(40,420,400,420);
for(int k=40 ; k<=380 ; k=k+20)
{
g.drawLine(k,80,k,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);
}
public void focusGained(FocusEvent e)
{
Component com=(Component)e.getSource();
if(com==text_3)
{
text_3.setText("");
}
else if(com==text_4)
{
text_4.setText("");
}
}
public void focusLost(FocusEvent e)
{
}
public void mousePressed(MouseEvent e)
{
if(blackplayer.length()==0||whiteplayer.length()==0)
{
JOptionPane.showMessageDialog(this,"you haven't input player's name yet","reminder",JOptionPane.WARNING_MESSAGE);
}
else if(blackplayer.equals("black player's name"))
{
JOptionPane.showMessageDialog(this,"you didn't input black player's name","reminder",JOptionPane.WARNING_MESSAGE);
}
else if(whiteplayer.equals("white player's name"))
{
JOptionPane.showMessageDialog(this,"you didn't input white player's name","reminder",JOptionPane.WARNING_MESSAGE);
}
else 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+20)/20;
int b = (y+20)/20;
if(x/20<2||y/20<4||x/20>19||y/20>20)
{
}
else
{
if(color == 1)
{
this.add(chesspoint_black);
chesspoint_black.setBounds(a*20-10,b*20-10,20,20);
color = color*(-1);
text_1.setText("");
text_2.setText(this.whiteplayer+",play white Please");
}
else if(color == -1)
{
this.add(chesspoint_white);
chesspoint_white.setBounds(a*20-10,b*20-10,20,20);
color = color*(-1);
text_1.setText(this.blackplayer+",play black Please");
text_2.setText("");
}
}
}
}
public void mouseReleased(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
public void mouseClicked(MouseEvent e)
{
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==inbutton)
{
blackplayer=text_3.getText();
whiteplayer=text_4.getText();
if(blackplayer.length()==0||whiteplayer.length()==0)
{
JOptionPane.showMessageDialog(this,"you haven't input player's name yet","reminder",JOptionPane.WARNING_MESSAGE);
}
else if(blackplayer.equals("black player's name"))
{
JOptionPane.showMessageDialog(this,"you didn't input black player's name","reminder",JOptionPane.WARNING_MESSAGE);
}
else if(whiteplayer.equals("white player's name"))
{
JOptionPane.showMessageDialog(this,"you didn't input white player's name","reminder",JOptionPane.WARNING_MESSAGE);
}
else
{
inbutton.setEnabled(false);
button.setEnabled(true);
text_3.removeFocusListener(this);
text_3.setEnabled(false);
text_1.setEnabled(true);
text_1.setText(blackplayer+",play black please");
text_2.setEnabled(true);
text_4.setEnabled(false);
}
}
else if(e.getSource()==button)
{
inbutton.setEnabled(true);
text_3.setEnabled(true);
text_4.setEnabled(true);
button.setEnabled(false);
text_1.setEnabled(false);
text_2.setEnabled(false);
this.removeAll();
color = 1;
add(button);
button.setBounds(35,36,60,26);
add(text_1);
text_1.setBounds(115,36,130,28);
text_1.setText("");
add(text_2);
text_2.setBounds(280,36,130,28);
text_2.setText("");
add(inbutton);
inbutton.setBounds(35,5,60,26);
add(text_3);
text_3.setText("black player's name");
text_3.addFocusListener(this);
text_3.setBounds(115,5,130,28);
add(text_4);
text_4.setText("white player's name");
text_4.setBounds(280,5,130,28);
blackplayer="";
whiteplayer="";
}
}
}
class ChessPoint_black extends Canvas implements MouseListener
{
ChessPad chesspad = null;
ChessPoint_black(ChessPad p)
{
setSize(20,20);
chesspad = p;
addMouseListener(this);
}
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.color = 1;
chesspad.text_2.setText("");
chesspad.text_1.setText(chesspad.blackplayer+",play black please");
}
}
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);
}
}
class ChessPoint_white extends Canvas implements MouseListener
{
ChessPad chesspad = null;
ChessPoint_white(ChessPad p)
{
setSize(20,20);
addMouseListener(this);
chesspad = p;
}
public void paint(Graphics g)
{
g.setColor(Color.white);
g.fillOval(0,0,20,20);
}
public void mousePressed(MouseEvent e)
{
if(e.getModifiers() == InputEvent.BUTTON1_MASK)
{
chesspad.remove(this);
chesspad.color = -1;
chesspad.text_2.setText(chesspad.whiteplayer+",play white Please");
chesspad.text_1.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 Chessplayer extends Frame
{
ChessPad chesspad = new ChessPad();
Chessplayer()
{
setVisible(true);
setLayout(null);
Label label = new Label("left-click to point, double-click to eat one, right-click to back",Label.CENTER);
add(label);
label.setBounds(70,55,440,24);
label.setBackground(Color.orange);
add(chesspad);
chesspad.setBounds(70,90,440,440);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
setLayout(null);
setVisible(true);
pack();
setSize(600,550);
}
public static void main(String[] args) {
Chessplayer chess = new Chessplayer();
}
}
改进程序
增加一行组件如下,并隐藏棋盘其它组件,在输入ID和Password之后,如果从未登录,按“Register”按钮,如果不是第一次登录,按“Login”按钮。
当ID不是合法的电子邮件地址时,弹出警告对话框,显示“You input an illegal ID.”。
当ID是合法的电子邮件地址时:
如果按“Register”按钮,创建account.txt文件,存入
<ID>ID文本框内容</ID>
<Password>Password文本框内容</Password>
然后显示棋盘其它组件
如果按“Login”按钮,打开account.txt文件,读出文件内容,比较<ID></ID>中的部分与ID文本框内容,<Password></Password>文本框内容,如果不一致,弹出警告对话框,显示“Wrong ID/Password combination.”,如果一致,则显示棋盘其它组件。
棋盘下方增加一个“Logout”按钮,当按动此按钮时,棋盘消失,显示登录页面。
补充:Java , Java相关