Cannot find symbol method drawImage(SlidingBlockModel, int, int, int, int, <nulltype>)
I'm trying to make a sliding block game. In this class I've used the drawImage method to display the blocks of the "puzzle", using the drawImage method of the Graphics object g2. But at开发者_StackOverflow社区 the paint class method I get this error: Cannot find symbol method drawImage(SlidingBlockModel, int, int, int, int, ). Any suggestions? I appreciate your time in reading this. Thank you in advance. :)
Zoi
The code follows:
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);
}
}
}
}
It's mean that your SBModel isn't java.awt.Image type.
Try to change your class SlidingBlockModel in that way:
SlidingBlockModel extends Image {}
Does SlidingBlockModel
inherit from java.awt.Image
? It would have to, since the method you are calling has the signature void drawImage(Image, int, int, int, int, ImageObserver)
. That seems to be the only possible problem with the code.
SlidingBlockModel
needs to be a subclass of java.awt.Image
in some way - either directly or indirectly. So you either need to extend from Image
, BufferedImage
or VolatileImage
.
It looks like it's not at the moment which is why it's complaining that it can't find the given method type, it can't find a method with that matching signature.
精彩评论