import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class game extends JFrame{
int score=0;
String[]s2=new String[7];
int xdraw;int ydraw;Color firstclick;//这个部分
int start=0;final Color c1=getBackground();
Color []c2=new Color [3];Color c3;
int xl;int yl;int xw;int yw;
property [][]a=new property [9][9];
public game()
{
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JPanel p3=new JPanel();
JButton b1=new JButton("Restart Game");
start=0;
for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
{
property p0=new property();
p0.setXlocate(i);p0.setYlocate(j);
p0.setBcolor(c1);a[i][j]=p0;
}
}
rancolor();
final JLabel jl = new JLabel("Next:"+s2[0]+","+s2[1]+","+s2[2]);
p1.setLayout(new BorderLayout());
p3.add(b1);
p1.add(p3,BorderLayout.NORTH);
p1.add(p2,BorderLayout.SOUTH);
panel pa1=new panel();
pa1.addMouseListener(new MouseAdapter(){//问题主要出现在
public void mouseClicked(MouseEvent e)
{
xw=e.getX()/xl;
yw=e.getY()/yl;
start=1;
if(a[xw][yw].getBcolor()!=c1)
{
firstclick=a[xw][yw].getBcolor();
repaint();
}
else if(a[xw][yw].getBcolor()==c1)
{
a[xw][yw].setBcolor(firstclick);
repaint();
}
}
});//这部分
p2.setLayout(new GridLayout(1,2));
p2.add(new JLabel("score:"+score));
p2.add(jl);
p1.add(pa1,BorderLayout.CENTER);
add(p1);
}
Color rancolor()
{
for(int i=0;i<3;i++)
{
int rand=(int)(Math.random()*7);
switch(rand)
{
case 0:s2[i]="red";c2[i]=Color.RED;c3=Color.RED;break;
case 1:s2[i]="blue";c2[i]=Color.BLUE;c3=Color.BLUE;break;
case 2:s2[i]="black";c2[i]=Color.BLACK;c3=Color.BLACK;break;
case 3:s2[i]="white";c2[i]=Color.WHITE;c3=Color.WHITE;break;
case 4:s2[i]="orange";c2[i]=Color.ORANGE;c3=Color.ORANGE;break;
case 5:s2[i]="yellow";c2[i]=Color.YELLOW;c3=Color.YELLOW;break;
case 6:s2[i]="green";c2[i]=Color.GREEN;c3=Color.GREEN;break;
}
}
return c3;
}
void init()
{
for(int j=0;j<5;j++)
{
int xrandom;int yrandom;
do{xrandom=(int)(Math.random()*9);
yrandom=(int)(Math.random()*9);}while(a[xrandom][yrandom].getBcolor()!=c1);
a[xrandom][yrandom].setBcolor(rancolor());
}
}
class panel extends JPanel
{
public void paintComponent(Graphics g)
{
int x=getWidth();
int y=getHeight();
xl=x/9;yl=y/9;
for(int i=0;i<9;i++)
{
g.drawLine(i*xl, 0, i*xl, y);
g.drawLine(0,i*yl,x,i*yl);
}
if(start==0)init();
for(int k1=0;k1<9;k1++)
{
for(int k2=0;k2<9;k2++)
{
a[k1][k2].draw(g, xl, yl);
}
}
if(a[xw][yw].getBcolor()!=c1)//和这个if
{
g.setColor(Color.BLACK);
g.drawLine(xw*xl,yw*yl+yl/2,xw*xl+xl/6,yw*yl+yl/2);
g.drawLine(xw*xl+xl/6*5,yw*yl+yl/2,xw*xl+xl,yw*yl+yl/2);
g.drawLine(xw*xl+xl/2,yw*yl,xw*xl+xl/2,yw*yl+yl/6);
g.drawLine(xw*xl+xl/2,yw*yl+yl/6*5,xw*xl+xl/2,yw*yl+yl);
a[xw][yw].setBcolor(c1);
}
}
}
public static void main(String[] args)
{
game f1=new game();
f1.setTitle("game");
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f1.setSize(447,510);
f1.setVisible(true);
}
}
class property 
{
private int xlocate;
private int ylocate;
private Color Bcolor;Color b;
public void draw(Graphics g,int m,int n) 

g.setColor(Bcolor);
g.fillOval(xlocate*m+m/6,ylocate*n+n/6,m/3*2,n/3*2);

public int getXlocate() 
{
return xlocate;
}
public void setXlocate(int x) {
this.xlocate = x;
}
public int getYlocate() {
return ylocate;
}
public void setYlocate(int y) {
this.ylocate = y;
}
public Color getBcolor() {
return Bcolor;
}
public void setBcolor(Color Bcolor) {
this.Bcolor = Bcolor;
}
}输出时移动有色小球会有问题:1.移动到新位置还带十字,我想不带;2.移动第二个小球,上一步的小球会消失

解决方案 »

  1.   

    你这个游戏还差很多么。
    如,下子和判分的逻辑尚未实现,现在想实现移动是不是?我猜,你说的移动,是指第一次单击选中一个有色球(选中便带十字),第二次单击到任意空格则将该球移动过去(移动过去后不带十字)?如果我猜的是对的,这样改(仅列出修改过的地方)//1
    int start = 0;  //0表示未开始玩,1表示已选定某个,2刚移动完一个因而处于未选定状态
    //2
    public void mouseClicked(MouseEvent e) {
    int xwBak = xw;
    int ywBak = yw;
    xw = e.getX() / xl;
    yw = e.getY() / yl;
    // start = 1;
    if (a[xw][yw].getBcolor() != c1) {
    firstclick = a[xw][yw].getBcolor(); //单击有色球选定该球
    start = 1;
    repaint();
    } else if (start==1 && a[xw][yw].getBcolor() == c1) {
    a[xw][yw].setBcolor(a[xwBak][ywBak].getBcolor());
    a[xwBak][ywBak].setBcolor(c1);
    start = 2;
    repaint();
    }
    }
    });// 这部分
    //3
    if (start==1 && a[xw][yw].getBcolor() != c1)// 和这个if
    {
    g.setColor(Color.BLACK);
    g.drawLine(xw * xl, yw * yl + yl / 2, xw * xl + xl / 6, yw * yl
    + yl / 2);
    g.drawLine(xw * xl + xl / 6 * 5, yw * yl + yl / 2,
    xw * xl + xl, yw * yl + yl / 2);
    g.drawLine(xw * xl + xl / 2, yw * yl, xw * xl + xl / 2, yw * yl
    + yl / 6);
    g.drawLine(xw * xl + xl / 2, yw * yl + yl / 6 * 5, xw * xl + xl
    / 2, yw * yl + yl);
    // a[xw][yw].setBcolor(c1);
    }修改了三处地方,其中第一处仅仅是语义上的修改