java悔棋代码 java实现悔棋思路( 五 )


{
this.x=x;
this.y=y;
addMouseListener(this);
}
public void mousePressed(MouseEvent event)
{
if(game_state==Playing)
{
int button=event.getButton();
if(button==MouseEvent.BUTTON1)
{
if(value=https://www.04ip.com/post/=0)
{
steps.add(this);
setValue(1);
WinCheck();
turn=false;
if(game_state==Playing)
gobangAI();
}
}
else
control_menu.show(this, event.getX(), event.getY());
}
}
public void mouseEntered(MouseEvent event)
{
if(game_state==Playing)
setSelected(true);
}
public void mouseExited(MouseEvent event)
{
if(game_state==Playing)
setSelected(false);
}
public void mouseClicked(MouseEvent event)
{
}
public void mouseReleased(MouseEvent event)
{
}
public Dimension getPreferredSize()
{
return new Dimension(20, 20);
}
public Dimension getMinimumSize()
{
return getPreferredSize();
}
public int getLocX()
{
return x;
}
public int getLocY()
{
return y;
}
public int getValue()
{
return value;
}
public void Initial()
{
value=https://www.04ip.com/post/grid[x][y]=0;
selected=false;
repaint();
}
public void setValue(int new_value)
{
locX=x;
locY=y;
value=https://www.04ip.com/post/grid[x][y]=new_value;
repaint();
}
public void setSelected(boolean select)
{
selected=select;
repaint();
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if(game_state!=Startgame_state!=Select)
{
g.drawLine(0, 10, 19, 10);
g.drawLine(10, 0, 10, 19);
/* 礶娩絬 */
g.setColor(Color.BLUE);
if(x==0)
g.drawLine(0, 0, 19, 0);
else if(x==length-1)
g.drawLine(0, 19, 19, 19);
if(y==0)
g.drawLine(19, 0, 19, 19);
else if(y==length-1)
g.drawLine(0, 0, 0, 19);
if(selected)
setBackground(Color.WHITE);
else
setBackground(Color.LIGHT_GRAY);
}
//g.drawString(x+" "+y, 1, 10);
if(value!=0)
{
temp = value=https://www.04ip.com/post/=1? black: white;
g.drawImage(temp, 4, 4, this);
}
}
}
}
java五子棋悔棋代码根据我的代码加// 悔棋
public void goback() {
if (chessCount == 0)//判断剩余棋子数
return;
chessList[chessCount - 1] = null;//chessList是棋子数组
chessCount--;
if (chessCount0) {
xIndex = chessList[chessCount - 1].getX();// xIndex, yIndex 当前刚下棋子的索引
yIndex = chessList[chessCount - 1].getY();
}
isBlack = !isBlack;
repaint();
}
JAVA五子棋中如何实现重新开始和悔棋的功能一般来说是不应该像你这样写的java悔棋代码 , 应该是把要画的步骤写在组件的被覆写的paintComponent函数里,然后在你响应事件的函数里直接掉用该组件的repaint()函数即可 。repaint函数是会去掉该组件的paintComponent()函数的 。至于你这么写为啥不行,这个java悔棋代码我也不清楚,据java悔棋代码我猜测 , repaint函数要做的事情是刷新组件,并且重新去画一遍,不过这个步骤不一定是立即完成,可能有延迟 。总之在你画那些棋子之后,它又干了一些事情 , 导致你画的棋子没了(但这不会影响到它自己想去画的东西 , 即paintComponent()函数里的东西),另外不要忘了,覆写paintComponent时,第一行要写,super.paintComponent(g),这是去画它原来有的东西(比如加上去的按钮,文本框之类的东西)
例如下面:
12345678910111213141516171819202122232425262728293031323334353637class PanelM extends JPanel{ImageIcon icon = new ImageIcon("app.gif");JLabel jl = new JLabel(icon, SwingConstants.CENTER);int x, y;private ListInteger listX = new ArrayListInteger();private ListInteger listY = new ArrayListInteger();public PanelM(){add(jl);addMouseListener(new MouseAdapter(){public void mousePressed(MouseEvent m){x = m.getX();y = m.getY();listX.add(x);listY.add(y);repaint();}});}public void paintComponent(Graphics g){super.paintComponent(g);for (int i = 0; ilistX.size(); i++){g.fillOval(listX.get(i), listY.get(i), 20, 20);}}}

推荐阅读