我正在尝试进行滑块游戏。在这个类中,我使用了drawImage方法,使用Graphics对象g2的drawImage方法显示“puzzle”的块。但是在paint类方法中我得到了这个错误:找不到符号方法drawImage(SlidingBlockModel,int,int,int,int,)。有什么建议?感谢您抽出宝贵时间阅读本文。先感谢您。 :)
ZOI
代码如下:
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.image.*;
import java.awt.Graphics.*;
class SlidingBlockPanel extends JPanel implements MouseListener
{
int numCols;
int numRows;
SlidingBlockModel SBModel;
public static void main(String[] args)
{
SlidingBlockFrame w = new SlidingBlockFrame();
w.setVisible(true);
}
public SlidingBlockPanel(int nc, int nr)
{
numCols = nc;
numRows = nr;
addMouseListener(this);
SBModel= new SlidingBlockModel(numCols, numRows, "puzzle.jpg");
}
int getCol(int x)
{
return x*numCols/getWidth();
}
int getRow(int y)
{
return y*numRows/getHeight();
}
public void mouseReleased(MouseEvent event)
{
}
public void mousePressed(MouseEvent event)
{
}
public void mouseClicked(MouseEvent event)
{
int thisCol = getCol(event.getX());
System.out.println
("you clicked in column " + thisCol);
}
public void mouseEntered(MouseEvent event)
{
}
public void mouseExited(MouseEvent event)
{
}
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++)
{
SBModel.getSubimage(i, j);
Rectangle r = getRect(i, j);
g2.drawImage(SBModel,r.x,r.y,r.width,r.height,null);
}
}
}
}
答案 0 :(得分:1)
这意味着您的SBModel不是java.awt.Image类型。
尝试以这种方式更改类SlidingBlockModel:
SlidingBlockModel extends Image {}
答案 1 :(得分:1)
SlidingBlockModel
是否继承自java.awt.Image
?它必须,因为您调用的方法具有签名void drawImage(Image, int, int, int, int, ImageObserver)
。这似乎是代码唯一可能出现的问题。
答案 2 :(得分:0)
SlidingBlockModel
需要以某种方式成为java.awt.Image
的子类 - 直接或间接。因此,您需要延伸Image
,BufferedImage
或VolatileImage
。
看起来现在不是为什么它抱怨它无法找到给定的方法类型,它找不到具有匹配签名的方法。