Java 一个简单的滑块游戏(游戏逻辑和绘制组件方法)
我正在制作一个简单的滑块游戏。我使用repaint()方法绘制拼图图像的块,然后在BuffereImage数组的帮助下绘制空块(拼图[empty_row][empty_col]=null;)。空块是你通常在拼图中的空白框。 问题是,我“调用”绘画方法来绘制我的拼图的所有块,我有一块是空的。每次用户单击它旁边,我就将EMPTYYROW、EMPTYTICOL更改为鼠标事件的GETX()、GETY(),以便将空白框移动到所需位置。p> 什么。。。让我困惑的是:)之前的空白框在我画完新框后仍然出现。这对你有意义吗 使用repaint()时,我希望块会被重新绘制,只留下一个空白框 有什么想法吗?这是我在这里的第二篇文章,我看到人们非常愿意帮助我。我真的很感激。提前感谢您的回答 佐伊 SlidingBlockPanel类中的代码位:Java 一个简单的滑块游戏(游戏逻辑和绘制组件方法),java,logic,paint,repaint,Java,Logic,Paint,Repaint,我正在制作一个简单的滑块游戏。我使用repaint()方法绘制拼图图像的块,然后在BuffereImage数组的帮助下绘制空块(拼图[empty_row][empty_col]=null;)。空块是你通常在拼图中的空白框。 问题是,我“调用”绘画方法来绘制我的拼图的所有块,我有一块是空的。每次用户单击它旁边,我就将EMPTYYROW、EMPTYTICOL更改为鼠标事件的GETX()、GETY(),以便将空白框移动到所需位置。p> 什么。。。让我困惑的是:)之前的空白框在我画完新框后仍然出现。这对
public SlidingBlockPanel(int nc, int nr)
{
numCols = nc;
numRows = nr;
addMouseListener(this);
SBModel= new SlidingBlockModel(numCols, numRows, "puzzle.jpg");
}
public void mouseClicked(MouseEvent event)
{
int thisCol = getCol(event.getX());
int thisRow = getRow(event.getY());
System.out.println
("you clicked in row " + thisRow);
System.out.println
("you clicked in column " + thisCol);
if (SBModel.slideBlock(thisRow,thisCol)==true)
repaint();
}
Rectangle getRect(int thisCol, int thisRow)
{
// if input is out of range, return "null"
if(thisCol <0 || thisRow < 0)
return null;
if(thisCol>=numCols || thisRow>=numRows)
return null;
// otherwise, make and return the Rectangle
int w = getWidth()/numCols;
int h = getHeight()/numRows;
int x = thisCol*w;
int y = thisRow*h;
Rectangle myRect = new Rectangle(x,y,w,h);
return myRect;
}
public void paint(Graphics g)
{
//g.setColor(Color.gray);
g.fillRect(0,0,getWidth(), getHeight());
g.setColor(Color.black);
Graphics2D g2 = (Graphics2D)g;
// we'll use Graphics2D for it's "drawImage" method this time
for (int i = 0;i<numCols;i++)
{
for(int j = 0;j<numRows;j++)
{
Rectangle r = getRect(i, j);
g2.drawImage(SBModel.getSubimage(i, j),r.x,r.y,r.width,r.height,null);
}
}
}
公共滑动块面板(int-nc,int-nr)
{
numCols=nc;
numRows=nr;
addMouseListener(这个);
SBModel=新的SlidingBlockModel(numCols,numRows,“puzzle.jpg”);
}
公共void mouseClicked(MouseEvent事件)
{
int thisCol=getCol(event.getX());
int thisRow=getRow(event.getY());
System.out.println
(“您单击了第行”+第行);
System.out.println
(“您在“+thisCol”列中单击);
if(SBModel.slideBlock(thisRow,thisCol)==true)
重新油漆();
}
矩形getRect(int thisCol,int thisRow)
{
//如果输入超出范围,则返回“null”
如果(thisCol=numCols | | thisRow>=numRows)
返回null;
//否则,生成并返回矩形
int w=getWidth()/numCols;
int h=getHeight()/numRows;
int x=此列*w;
int y=该行*h;
矩形myRect=新矩形(x,y,w,h);
返回myRect;
}
公共空间涂料(图g)
{
//g、 setColor(颜色为灰色);
g、 fillRect(0,0,getWidth(),getHeight());
g、 设置颜色(颜色为黑色);
图形2d g2=(图形2d)g;
//这次我们将使用Graphics2D作为“drawImage”方法
对于(int i=0;i我认为你的问题是下一个:
if (SBModel.slideBlock(thisRow,thisCol)==true)
repaint();
调用幻灯片函数,然后重新绘制(这并不全是错的)在幻灯片中,你只需移动一个空白的片断,在任何时候你都可以用一张图片滑动该幻灯片。在幻灯片中,你需要先移动一个图像,然后移动空白的一个,然后重新绘制。< /P>你能给我们看一下你的代码吗?BTW,我是不是假设这是java代码?哦,对不起,恰克·巴斯!你应该假设这是Java代码。:)Dante,repaint()就是这么做的。还是我弄错了?看看我的paint()方法。
if (SBModel.slideBlock(thisRow,thisCol)==true)
repaint();