十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
这种报错,有两种解决办法;
成都创新互联是一家专注于网站设计、网站制作与策划设计,城区网站建设哪家好?成都创新互联做网站,专注于网站建设10余年,网设计领域的专业建站公司;建站业务涵盖:城区等地区。城区做网站价格咨询:13518219792
首先,你可以上网找一些黑白子落子的代码,基本上都是差不多的;
其次,报错,根据步骤看是哪行出问题了,
比如,运行的时候,界面没有显示,那你就到界面显示查看;
如果是黑白子落子问题,说明你的黑白子边代码设计错误;
遇到问题一步步来,这也是成长的过程。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;
import javax.swing.JOptionPane.*;
public class FiveChess
{
public static void main(String[] args)
{
JFrame myFrame = new JFrame("快乐五子棋");
myFrame.getContentPane().add(new MyPanel());
Toolkit mykit = myFrame.getToolkit();
Dimension mysize = mykit.getScreenSize();
myFrame.setBounds(0,0,mysize.width,mysize.height - 40);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.show();
}
}
class MyPanel extends JPanel implements MouseListener
{
private Point start = new Point(0,0);
private Point next = new Point(0,0);
boolean[][] chessBo = new boolean[19][19];
boolean chessCo = true; //判断颜色的,true 黑
boolean winchess = true; //true,为胜利
MyPanel(){
addMouseListener(this);
setBackground(Color.blue);
}
public boolean winkill(int x,int y) //win ?
{
int countBlack = 0,countWhite = 0;
boolean winchess01 = false;
//x,y 取到r01,r02的值
for(int i = 0;i 5;i++)//判断纵向下
{
if((x + i) 20)
break;
if(chessCo chessBo[x + i][y])
countBlack++;
else if(chessBo[x + i][y] (chessCo == false))
countWhite++;
}
for(int i = 0;i 5;i++)//判断纵向上
{
if((x - i) = 0)
break;
if(chessCo chessBo[x - i][y])
countBlack++;
else if(chessBo[x - i][y] (chessCo == false))
countWhite++;
}
if((countBlack == 5) || (countWhite == 5))
winchess01 = true;
return winchess01;
}
public void paint(Graphics g)//画棋盘
{
Graphics2D g2D = (Graphics2D)g;
g2D.setPaint(Color.black);
float pay = 60.0f,pbx = 60.0f;
float lett = 25.0f;
Point2D.Float p1 = new Point2D.Float(60.0f,pay);
Point2D.Float p2 = new Point2D.Float(510.0f,pay);
Point2D.Float p3 = new Point2D.Float(pbx,60.0f);
Point2D.Float p4 = new Point2D.Float(pbx,510.0f);
for(int i=0;i19;i++){
Line2D.Float lineH = new Line2D.Float(p1,p2);
Line2D.Float lineV = new Line2D.Float(p3,p4);
pay += lett;
p1 = new Point2D.Float(60.0f,pay);
p2 = new Point2D.Float(510.0f,pay);
pbx += lett;
p3 = new Point2D.Float(pbx,60.0f);
p4 = new Point2D.Float(pbx,510.0f);
g2D.draw(lineH);
g2D.draw(lineV);
}
}
public void mousePressed(MouseEvent evt)
{
}
public void mouseClicked(MouseEvent evt)
{
int x = evt.getX();
int y = evt.getY();
int clickCount = evt.getClickCount();
if(clickCount =1 ){
if((x 48 x 522) (y 48 y 522))
draw(x,y);
}
}
public void draw(int dx,int dy)
{
int r01 = 0,r02 = 0;
Graphics g = getGraphics();
start.x = dx;
start.y = dy;
r01 = (start.x + 13 - 60) / 25; //r01 、r02当前的格子
r02 = (start.y + 13 - 60) / 25;
//System.out.println(r01 + "-" + r02);
next.x = 25 * r01 + 60 - 11;
next.y = 25 * r02 + 60 - 11;
//System.out.println(next.x + "-" + next.y);
if(chessCo){
chessCo = false;
g.setColor(Color.black);
g.fillOval(next.x,next.y,20,20);
chessBo[next.x][next.y] = true;//用这个时,黑白子可交替出现,
//chessBo[r01][r02] = true;//用这个代替上面那个时黑白子不可交替了,不知道为什么会这样
//加入判断胜负的方法winkill()
if(winchess == winkill(r01,r02))
showMessage();
}
if(!chessCo){
chessCo = true;
g.setColor(Color.white);
g.fillOval(next.x,next.y,20,20);
chessBo[r01][r02] = true;
}
//g.drawOval(next.x,next.y,20,20);
g.dispose();
}
public void mouseReleased(MouseEvent evt)
{
}
public void mouseEntered(MouseEvent evt)
{
}
public void mouseExited(MouseEvent evt)
{
}
public void showMessage()
{
JOptionPane.showMessageDialog(null,
"You are win.",
"wide288 to Message!",
JOptionPane.INFORMATION_MESSAGE);
}
}
/**
* 实验课作业
* @author Administrator
* 这个实现允许重画
*/
package com.test;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.ImageObserver;
import java.text.AttributedCharacterIterator;
public class GameDemo2 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MainWindow mw=new MainWindow();
mw.setSize(400,400);
mw.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mw.setVisible(true);
//mw.setResizable(false);
}
}
class MainWindow extends JFrame implements ActionListener{
/**
*
*/
private static final long serialVersionUID = 1L;
NewPanel mp=null;
JButton jbt1;
JButton jbt2;
JButton jbt3;
JButton jbt4;
int flag=-1;
JPanel jp=null;
public MainWindow(){
//初始化组件
mp=new NewPanel();
mp.addMouseListener(mp);
jbt1=new JButton("O 先手");
jbt2=new JButton("X 先手 ");
jbt3=new JButton("RESET");
jbt4=new JButton("EXIT");
jp=new JPanel();
GridLayout gl=new GridLayout(4,1);
gl.setHgap(4);
gl.setVgap(5);
jp.setLayout(gl);
jp.add(jbt1);
jp.add(jbt2);
jp.add(jbt3);
jp.add(jbt4);
this.add(mp,BorderLayout.CENTER);
this.add(jp,BorderLayout.EAST);
jbt1.addActionListener(this);
jbt2.addActionListener(this);
jbt3.addActionListener(this);
jbt4.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==jbt1){
flag=1;
mp.setHand(0);
jbt1.setEnabled(false);
jbt2.setEnabled(false);
}
if(e.getSource()==jbt2){
flag=2;
mp.setHand(1);
jbt1.setEnabled(false);
jbt2.setEnabled(false);
}
if(e.getSource()==jbt3){
flag=3;
mp.getGraphics().dispose();
this.remove(mp);
mp=new NewPanel();
this.add(mp,BorderLayout.CENTER);
mp.addMouseListener(mp);
this.remove(jp);
jp=new JPanel();
GridLayout gl=new GridLayout(4,1);
gl.setHgap(4);
gl.setVgap(5);
jp.setLayout(gl);
jp.add(jbt1);
jp.add(jbt2);
jp.add(jbt3);
jp.add(jbt4);
this.add(jp,BorderLayout.EAST);
this.validate();
jbt1.setEnabled(true);
jbt2.setEnabled(true);
}
if(e.getSource()==jbt4){
flag=4;
jbt1.setEnabled(false);
jbt2.setEnabled(false);
System.exit(0);
}
}
public int getFlag(){
return flag;
}
}
//画板
class NewPanel extends JPanel implements MouseListener{
/**
*
*/
private static final long serialVersionUID = 1L;
//记录画图的位置
int xx,yy;
//flag表示是否是初始化
int flag;
//0画X,1画0,2不画
int kind;
//记录是有有图形的数组
//a[i]=0代表是空白可以画 a[i]=1代表X a[i]=2代表画O
int []a=null;
//记录当前鼠标位置所在方格,便于判断是否可以继续画
int curLoc;
//记录谁先手 0-0 1-X
int firstHand;
int times;
//0-O 1-X
int whoWin;
public void setFlag(int f){
flag=f;
}
public void setHand(int h){
firstHand=h;
}
public void setWhoWin(){
whoWin=-1;
}
public NewPanel(){
xx=0;
yy=0;
flag=0;
a=new int[9];
kind=-1;
curLoc=0;
whoWin=-1;
firstHand=-1;
times=1;
whoWin=-1;
}
public void paint(Graphics g){
//super.paint(g);
g.drawRect(0, 0, 300, 300);
g.drawRect(0, 100, 300, 100);
g.drawRect(100, 0, 100, 300);
if(flag==1){
if(isFull()==false){
if(testWin()==false){
if((times+firstHand)%2==0){
if(a[curLoc]==0){
g.drawLine(xx,yy,xx+60,yy+60);
g.drawLine(xx+60, yy, xx, yy+60);
a[curLoc]=1;
times++;
if(testWin()==true){
System.out.println("X赢了");
}
}
}
if((times+firstHand)%2==1){
if(a[curLoc]==0){
g.drawOval(xx,yy,60,60);
a[curLoc]=2;
times++;
if(testWin()==true){
System.out.println("O赢了");
}
}
}
}
}else{
System.out.println("it is FULL!!!");
}
}
}
//将鼠标的坐标转换为所画图形的基准坐标 ,返回当前鼠标在记录数组a中的下标
//圆形为外接矩形的左上角
//X为左上角起始点的坐标
public int Format(int x,int y,int kind){
//第一行第一列
int loc=-1;
if(x=0x100y=0y100){
xx=20;
yy=20;
loc= 0;
}
//第一行第二列
if(x=100x200y=0y100){
xx=120;
yy=20;
loc= 1;
}
//第一行第三列
if(x=200x300y=0y100){
xx=220;
yy=20;
loc= 2;
}
//第二行第一列
if(x=0x100y=100y200){
xx=20;
yy=120;
loc= 3;
}
//第二行第二列
if(x=100x200y=100y200){
xx=120;
yy=120;
loc= 4;
}
//第二行第三列
if(x=200x300y=100y200){
xx=220;
yy=120;
loc= 5;
}
//第三行第一列
if(x=0x100y=200y300){
xx=20;
yy=220;
loc= 6;
}
//第三行第二列
if(x=100x200y=200y300){
xx=120;
yy=220;
loc= 7;
}
//第三行第三列
if(x=200x300y=200y300){
xx=220;
yy=220;
loc= 8;
}
return loc;
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
xx=e.getX();
yy=e.getY();
flag=1;
//kind=new Random().nextInt(3);
curLoc=Format(xx,yy,kind);
this.repaint();
}
//判断是否可以继续画的函数
public boolean isFull(){
for(int i=0;i9;i++)
if(a[i]!=1a[i]!=2)
return false;
return true;
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
public boolean testWin(){
boolean isWin=false;
//a[0]=1
if(a[0]!=0a[0]==a[1]a[0]==a[2]){
whoWin=a[0];
isWin=true;
}
if(a[0]!=0a[3]==a[0]a[0]==a[6]){
whoWin=a[0];
isWin=true;
}
if(a[0]!=0a[0]==a[4]a[0]==a[8]){
whoWin=a[0];
isWin=true;
}
if(a[1]!=0a[1]==a[4]a[1]==a[7]){
whoWin=a[1];
isWin=true;
}
if(a[2]!=0a[2]==a[4]a[2]==a[6]){
whoWin=a[2];
isWin=true;
}
if(a[2]!=0a[2]==a[5]a[2]==a[8]){
whoWin=a[2];
isWin=true;
}
if(a[3]!=0a[3]==a[4]a[3]==a[5]){
whoWin=a[3];
isWin=true;
}
if(a[6]!=0a[6]==a[7]a[6]==a[8]){
whoWin=a[6];
isWin=true;
}
return isWin;
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
//重新设置初始值
public void reset(){
this.getGraphics().dispose();
xx=0;
yy=0;
flag=0;
a=new int[9];
kind=-1;
curLoc=0;
whoWin=-1;
firstHand=-1;
times=1;
whoWin=-1;
}
}
就是这个了吧
额们实验课的作业
一个小程序 简单的21点
package com.citicbank.other;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;
public class gameTest extends JFrame {
private JLabel label_2;
private int number;
private int sum;
final JLabel label = new JLabel();
final JLabel label_1 = new JLabel();
public static void main(String[] args) {
new gameTest();
}
public gameTest() {
super("21点?!");
getContentPane().setLayout(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JButton button = new JButton();
button.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent arg0) {
onClick();
}
});
button.setText("出牌");
button.setBounds(170, 350, 106, 28);
getContentPane().add(button);
label.setBorder(new LineBorder(Color.black, 1, false));
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setFont(new Font("", Font.BOLD, 26));
label.setText("背面");
label.setBounds(158, 81, 137, 153);
getContentPane().add(label);
label_1.setText("你已经拥有的牌:");
label_1.setBounds(109, 22, 270, 45);
getContentPane().add(label_1);
this.setBounds(200, 300, 501, 528);
this.setVisible(true);
getContentPane().add(getLabel_2());
}
public int randNumber() {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
return (int) (Math.random() * 10 + 1);
}
public void onClick() {
number = this.randNumber();
this.sum += number;
label.setText("" + number);
String strTemp = this.label_1.getText();
strTemp += "" + number + " ";
label_1.setText(strTemp);
String temp = "合计:" + sum;
label_2.setText(temp);
isWin();
}
public void isWin() {
if (sum 21) {
JOptionPane.showMessageDialog(this, "你输了");
clear();
return;
} else if (sum == 21) {
JOptionPane.showMessageDialog(this, "你赢了");
clear();
return;
} else {
int i = JOptionPane.showOptionDialog(this, "是否继续?", "提示",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.INFORMATION_MESSAGE, null, null, null);
if (i == JOptionPane.OK_OPTION) {
onClick();
} else
return;
}
}
private void clear() {
label_2.setText("合计:");
sum = 0;
number = 0;
label_1.setText("你已经拥有的牌:");
}
/**
* @return
*/
protected JLabel getLabel_2() {
if (label_2 == null) {
label_2 = new JLabel();
label_2.setText("合计:");
label_2.setBounds(313, 35, 66, 18);
}
return label_2;
}
}