/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package bigwork;import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;class MyWork extends JFrame implements ActionListener, MouseListener, MouseMotionListener, ItemListener, WindowListener { int i, j; ; void Init() {
this.setSize(new Dimension(660, 670));
this.setLocation(15, 15);
this.setVisible(true); } @Override
public void paint(Graphics g) {
g.setColor(Color.pink);
g.fill3DRect(10, 40, 490, 490, true);
g.setColor(Color.black);
for (i = 0; i < 16; i++) {
g.drawLine(30 + 30 * i, 60, 30 + 30 * i, 510);
g.drawLine(30, 60 + 30 * i, 480, 60 + 30 * i);
} } public void button() {
Container con = getContentPane();
JPanel panel = new JPanel();
con.add(panel);
panel.setLayout(null);
JRadioButton b1 = new JRadioButton("黑子先行");
JRadioButton b2 = new JRadioButton("白子先行");
JButton b3 = new JButton("重置游戏");
JButton b4 = new JButton("开始游戏");
b1.setBounds(500, 20, 80, 30);
panel.add(b1);
b2.setBounds(500, 60, 80, 30);
panel.add(b2);
b3.setBounds(500, 100, 80, 30);
panel.add(b3);
b4.setBounds(500, 140, 80, 30);
panel.add(b4);
JLabel Lab1 = new JLabel("白子赢了!");
JLabel Lab2 = new JLabel("黑子赢了!");
Font fnt = new Font("Serief", Font.ITALIC + Font.BOLD, 28);
Lab1.setBounds(400, 550, 280, 30);
Lab1.setFont(fnt);
panel.add(Lab1);
Lab2.setBounds(100, 550, 280, 30);
Lab2.setFont(fnt);
panel.add(Lab2);
} @Override
public synchronized void addMouseMotionListener(MouseMotionListener l) {
super.addMouseMotionListener(l);
} @Override
public synchronized void addMouseListener(MouseListener l) {
super.addMouseListener(l);
} public void actionPerformed(ActionEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
} public void mouseClicked(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
} public void mousePressed(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
} public void mouseReleased(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
} public void mouseEntered(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
} public void mouseExited(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
} public void mouseDragged(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
} public void mouseMoved(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
} public void itemStateChanged(ItemEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
} public void windowClosed(WindowEvent e) {
} public void windowOpened(WindowEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
} public void windowClosing(WindowEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
} public void windowIconified(WindowEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
} public void windowDeiconified(WindowEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
} public void windowActivated(WindowEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
} public void windowDeactivated(WindowEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
}public class Main { public static void main(String[] args) {
MyWork MY = new MyWork();
MY.Init();
MY.button();
}
}
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package bigwork;import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;class MyWork extends JFrame implements ActionListener, MouseListener, MouseMotionListener, ItemListener, WindowListener { int i, j; ; void Init() {
this.setSize(new Dimension(660, 670));
this.setLocation(15, 15);
this.setVisible(true); } @Override
public void paint(Graphics g) {
g.setColor(Color.pink);
g.fill3DRect(10, 40, 490, 490, true);
g.setColor(Color.black);
for (i = 0; i < 16; i++) {
g.drawLine(30 + 30 * i, 60, 30 + 30 * i, 510);
g.drawLine(30, 60 + 30 * i, 480, 60 + 30 * i);
} } public void button() {
Container con = getContentPane();
JPanel panel = new JPanel();
con.add(panel);
panel.setLayout(null);
JRadioButton b1 = new JRadioButton("黑子先行");
JRadioButton b2 = new JRadioButton("白子先行");
JButton b3 = new JButton("重置游戏");
JButton b4 = new JButton("开始游戏");
b1.setBounds(500, 20, 80, 30);
panel.add(b1);
b2.setBounds(500, 60, 80, 30);
panel.add(b2);
b3.setBounds(500, 100, 80, 30);
panel.add(b3);
b4.setBounds(500, 140, 80, 30);
panel.add(b4);
JLabel Lab1 = new JLabel("白子赢了!");
JLabel Lab2 = new JLabel("黑子赢了!");
Font fnt = new Font("Serief", Font.ITALIC + Font.BOLD, 28);
Lab1.setBounds(400, 550, 280, 30);
Lab1.setFont(fnt);
panel.add(Lab1);
Lab2.setBounds(100, 550, 280, 30);
Lab2.setFont(fnt);
panel.add(Lab2);
} @Override
public synchronized void addMouseMotionListener(MouseMotionListener l) {
super.addMouseMotionListener(l);
} @Override
public synchronized void addMouseListener(MouseListener l) {
super.addMouseListener(l);
} public void actionPerformed(ActionEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
} public void mouseClicked(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
} public void mousePressed(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
} public void mouseReleased(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
} public void mouseEntered(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
} public void mouseExited(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
} public void mouseDragged(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
} public void mouseMoved(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
} public void itemStateChanged(ItemEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
} public void windowClosed(WindowEvent e) {
} public void windowOpened(WindowEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
} public void windowClosing(WindowEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
} public void windowIconified(WindowEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
} public void windowDeiconified(WindowEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
} public void windowActivated(WindowEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
} public void windowDeactivated(WindowEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
}public class Main { public static void main(String[] args) {
MyWork MY = new MyWork();
MY.Init();
MY.button();
}
}
public void paint(Graphics g) {
g.setColor(Color.pink);
g.fill3DRect(10, 40, 490, 490, true);
g.setColor(Color.black);
for (i = 0; i < 16; i++) {
g.drawLine(30 + 30 * i, 60, 30 + 30 * i, 510);
g.drawLine(30, 60 + 30 * i, 480, 60 + 30 * i);
} }把这注释掉以后,那个白子赢还是黑子赢,才会出来,这是为什??求解!!
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package bigwork;import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;class MyWork extends JFrame implements ActionListener, MouseListener, MouseMotionListener, ItemListener, WindowListener { int i, j;
Checkbox ckbHB[] = new Checkbox[2];
CheckboxGroup ckgHB = new CheckboxGroup();
int color = 0;// 旗子的颜色标识 0:白子 1:黑子
boolean isStart = false;// 游戏开始标志
int bodyArray[][] = new int[16][16]; // 设置棋盘棋子状态 0 无子 1 白子 2 黑子
JButton b3 = new JButton("Restare");
JButton b4 = new JButton("Begin"); public void gameInit() // 游戏开始初始化
{
isStart = false;
enableGame(true);
b3.setEnabled(false);
ckbHB[0].setState(true);
for (i = 0; i < 16; i++) {
for (j = 0; j < 16; j++) {
bodyArray[i][j] = 0;
}
}
// lblWin.setText(""); } public void enableGame(boolean e) // 设置组件状态
{
b4.setEnabled(e);
b3.setEnabled(e);
ckbHB[0].setEnabled(e);
ckbHB[1].setEnabled(e);
} void Init() {
this.setSize(new Dimension(660, 670));
this.setLocation(15, 15);
this.setVisible(true); } @Override
public void paint(Graphics g) {
g.setColor(Color.pink);
g.fill3DRect(10, 40, 490, 490, true);
g.setColor(Color.black);
for (i = 0; i < 16; i++) {
g.drawLine(30 + 30 * i, 60, 30 + 30 * i, 510);
g.drawLine(30, 60 + 30 * i, 480, 60 + 30 * i);
} } public void setDown(int x, int y) // 落子
{
if (!isStart) // 判断游戏未开始
{
return;
}
if (bodyArray[x / 20][y / 20] != 0) {
return;
}
Graphics g = getGraphics();
if (color == 1)// 判断黑子还是白子
{
g.setColor(Color.black);
color = 0;
} else {
g.setColor(Color.white);
color = 1;
}
g.fillOval(x - 15, y - 15, 30, 30);
bodyArray[x / 20][y / 20] = color + 1;
/* if (gameWin1(x / 20, y / 20)) // 判断输赢
{
lblWin.setText(startColor(color) + "赢了!");
isStart = false;
}
if (gameWin2(x / 20, y / 20)) // 判断输赢
{
lblWin.setText(startColor(color) + "赢了!");
isStart = false;
}
if (gameWin3(x / 20, y / 20)) // 判断输赢
{
lblWin.setText(startColor(color) + "赢了!");
isStart = false;
}
if (gameWin4(x / 20, y / 20)) // 判断输赢
{
lblWin.setText(startColor(color) + "赢了!");
isStart = false;
}*/
} public void button() {
Container con = getContentPane();
JPanel panel = new JPanel();
con.add(panel);
panel.setLayout(null); ckbHB[0] = new Checkbox("white first", ckgHB, false);
ckbHB[0].setBounds(500, 20, 80, 30);
ckbHB[1] = new Checkbox("black first", ckgHB, false);
ckbHB[1].setBounds(500, 60, 80, 30);
panel.add(ckbHB[0]);
panel.add(ckbHB[1]);
ckbHB[0].addItemListener(this);
ckbHB[1].addItemListener(this); b3.setBounds(500, 100, 80, 30);
panel.add(b3);
b4.setBounds(500, 140, 80, 30);
panel.add(b4);
JLabel Lab1 = new JLabel("白子赢了!");
JLabel Lab2 = new JLabel("黑子赢了!");
Font fnt = new Font("Serief", Font.ITALIC + Font.BOLD, 28);
Lab1.setBounds(400, 550, 280, 30);
Lab1.setFont(fnt);
panel.add(Lab1);
Lab2.setBounds(100, 550, 280, 30);
Lab2.setFont(fnt);
panel.add(Lab2);
addMouseListener(this);
} public void gameStart() // 游戏开始
{
isStart = true;
enableGame(false);
b3.setEnabled(true);
} public void reStart() // 游戏重新开始
{
repaint();
gameInit();
} public void actionPerformed(ActionEvent e) {
if (e.getSource() == b4) {
gameStart();
} else {
reStart();
}
} @Override
public synchronized void addMouseMotionListener(MouseMotionListener l) {
super.addMouseMotionListener(l);
} @Override
public synchronized void addMouseListener(MouseListener l) {
super.addMouseListener(l);
} /* public void actionPerformed(ActionEvent e) {
if (e.getSource() == b4) {
gameStart();
} else {
reStart();
}
}*/
public void mouseClicked(MouseEvent e) {
int x1, y1;
x1 = e.getX();
y1 = e.getY();
if (e.getX() < 16 || e.getX() > 494 || e.getY() < 46 || e.getY() > 524) {
return;
}
if (x1 % 30 > 15) {
x1 += 30;
}
if (y1 % 30 > 15) {
y1 += 30;
}
x1 = x1 / 30 * 30;
y1 = y1 / 30 * 30;
setDown(x1, y1);
} public void mousePressed(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
} public void mouseReleased(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
} public void mouseEntered(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
} public void mouseExited(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
} public void mouseDragged(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
} public void mouseMoved(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
} public void itemStateChanged(ItemEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
} public void windowClosed(WindowEvent e) {
} public void windowOpened(WindowEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
} public void windowClosing(WindowEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
} public void windowIconified(WindowEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
} public void windowDeiconified(WindowEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
} public void windowActivated(WindowEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
} public void windowDeactivated(WindowEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
}public class Main { public static void main(String[] args) {
MyWork MY = new MyWork();
MY.Init();
MY.button();
}
}
想实现落子,为什么不行啊 ,快崩溃了
import java.awt.CheckboxGroup;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;class MyWork extends JFrame implements ActionListener, MouseListener, MouseMotionListener, ItemListener, WindowListener { int i, j;
Checkbox ckbHB[] = new Checkbox[2];
CheckboxGroup ckgHB = new CheckboxGroup();
int color = 0;// 旗子的颜色标识 0:白子 1:黑子
boolean isStart = false;// 游戏开始标志
int bodyArray[][] = new int[16][16]; // 设置棋盘棋子状态 0 无子 1 白子 2 黑子
JButton b3 = new JButton("Restare");
JButton b4 = new JButton("Begin"); public void gameInit() // 游戏开始初始化
{
isStart = false;
enableGame(true);
b3.setEnabled(false);
ckbHB[0].setState(true);
for (i = 0; i < 16; i++) {
for (j = 0; j < 16; j++) {
bodyArray[i][j] = 0;
}
}
// lblWin.setText(""); } public void enableGame(boolean e) // 设置组件状态
{
b4.setEnabled(e);
b3.setEnabled(e);
ckbHB[0].setEnabled(e);
ckbHB[1].setEnabled(e);
} void Init() {
this.setSize(new Dimension(660, 670));
this.setLocation(15, 15);
this.setVisible(true); } @Override
public void paint(Graphics g) {
g.setColor(Color.pink);
g.fill3DRect(10, 40, 490, 490, true);
g.setColor(Color.black);
for (i = 0; i < 16; i++) {
g.drawLine(30 + 30 * i, 60, 30 + 30 * i, 510);
g.drawLine(30, 60 + 30 * i, 480, 60 + 30 * i);
} } public void setDown(int x, int y) // 落子
{
if (!isStart) // 判断游戏未开始
{
return;
}
if (bodyArray[x / 20][y / 20] != 0) {
return;
}
Graphics g = getGraphics();
if (color == 1)// 判断黑子还是白子
{
g.setColor(Color.black);
color = 0;
} else {
g.setColor(Color.white);
color = 1;
}
g.fillOval(x - 15, y - 15, 30, 30);
bodyArray[x / 20][y / 20] = color + 1;
if (gameWin1(x / 20, y / 20)) // 判断输赢
{
// lblWin.setText(startColor(color) + "赢了!");
isStart = false;
}
if (gameWin2(x / 20, y / 20)) // 判断输赢
{
// lblWin.setText(startColor(color) + "赢了!");
isStart = false;
}
if (gameWin3(x / 20, y / 20)) // 判断输赢
{
// lblWin.setText(startColor(color) + "赢了!");
isStart = false;
}
if (gameWin4(x / 20, y / 20)) // 判断输赢
{
// lblWin.setText(startColor(color) + "赢了!");
isStart = false;
}
} public void button() {
Container con = getContentPane();
JPanel panel = new JPanel();
con.add(panel);
panel.setLayout(null); ckbHB[0] = new Checkbox("white first", ckgHB, false);
ckbHB[0].setBounds(500, 20, 80, 30);
ckbHB[1] = new Checkbox("black first", ckgHB, false);
ckbHB[1].setBounds(500, 60, 80, 30);
panel.add(ckbHB[0]);
panel.add(ckbHB[1]);
ckbHB[0].addItemListener(this);
ckbHB[1].addItemListener(this); b3.setBounds(500, 100, 80, 30);
panel.add(b3);
b4.setBounds(500, 140, 80, 30);
panel.add(b4);
JLabel Lab1 = new JLabel("白子赢了!");
JLabel Lab2 = new JLabel("黑子赢了!");
Font fnt = new Font("Serief", Font.ITALIC + Font.BOLD, 28);
Lab1.setBounds(400, 550, 280, 30);
Lab1.setFont(fnt);
panel.add(Lab1);
Lab2.setBounds(100, 550, 280, 30);
Lab2.setFont(fnt);
panel.add(Lab2);
addMouseListener(this);
} public void gameStart() // 游戏开始
{
isStart = true;
enableGame(false);
b3.setEnabled(true);
} public void reStart() // 游戏重新开始
{
repaint();
gameInit();
} public void actionPerformed(ActionEvent e) {
if (e.getSource() == b4) {
gameStart();
} else {
reStart();
}
} @Override
public synchronized void addMouseMotionListener(MouseMotionListener l) {
super.addMouseMotionListener(l);
} @Override
public synchronized void addMouseListener(MouseListener l) {
super.addMouseListener(l);
} public void mouseClicked(MouseEvent e) {
int x1, y1;
x1 = e.getX();
y1 = e.getY();
if (e.getX() < 16 || e.getX() > 494 || e.getY() < 46 || e.getY() > 524) {
return;
}
if (x1 % 30 > 15) {
x1 += 30;
}
if (y1 % 30 > 15) {
y1 += 30;
}
x1 = x1 / 30 * 30;
y1 = y1 / 30 * 30;
setDown(x1, y1);
} public void mousePressed(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
} public void mouseReleased(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
} public void mouseEntered(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
} public void mouseExited(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
} public void mouseDragged(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
} public void mouseMoved(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
} public void itemStateChanged(ItemEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
} public void windowClosed(WindowEvent e) {
} public void windowOpened(WindowEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}