Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 找不到符号方法drawImage(SlidingBlockModel,int,int,int,<;nulltype>;)_Java_Drawimage - Fatal编程技术网

Java 找不到符号方法drawImage(SlidingBlockModel,int,int,int,<;nulltype>;)

Java 找不到符号方法drawImage(SlidingBlockModel,int,int,int,<;nulltype>;),java,drawimage,Java,Drawimage,我想做一个滑块游戏。在这个类中,我使用了drawImage方法来显示“拼图”的块,使用了图形对象g2的drawImage方法。但是在paint类方法中,我得到了这个错误:找不到符号方法drawImage(SlidingBlockModel,int,int,int,int,)。有什么建议吗?我感谢你花时间读这篇文章。提前谢谢。:) 佐伊 守则如下: import java.awt.*; import java.awt.geom.*; import javax.swing.*; import jav

我想做一个滑块游戏。在这个类中,我使用了drawImage方法来显示“拼图”的块,使用了图形对象g2的drawImage方法。但是在paint类方法中,我得到了这个错误:找不到符号方法drawImage(SlidingBlockModel,int,int,int,int,)。有什么建议吗?我感谢你花时间读这篇文章。提前谢谢。:)

佐伊

守则如下:

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);


        }
    } 

}


}
import java.awt.*;
导入java.awt.geom.*;
导入javax.swing.*;
导入java.awt.event.MouseListener;
导入java.awt.event.MouseEvent;
导入java.awt.image.*;
导入java.awt.Graphics.*;
类SlidingBlockPanel扩展JPanel实现MouseListener
{
int numCols;
int numRows;
滑块模型;
公共静态void main(字符串[]args)
{
SlidingBlockFrame w=新的SlidingBlockFrame();
w、 setVisible(真);
}
公共滑动块面板(内部nc,内部nr)
{
numCols=nc;
numRows=nr;
addMouseListener(这个);
SBModel=新的SlidingBlockModel(numCols,numRows,“puzzle.jpg”);
}
int getCol(int x)
{
返回x*numCols/getWidth();
}
int getRow(int y)
{
返回y*numRows/getHeight();
}
公共无效MouseEvent事件(MouseEvent事件)
{
}
公共无效鼠标按下(鼠标事件)
{
}
公共void mouseClicked(MouseEvent事件)
{
int thisCol=getCol(event.getX());
System.out.println
(“您在“+thisCol”列中单击);
}
公共无效鼠标事件(鼠标事件)
{
}
public void mouseExited(MouseEvent事件)
{
}
矩形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”方法

对于(inti=0;i这意味着您的SBModel不是java.awt.Image类型

尝试以这种方式更改类SlidingBlockModel:

SlidingBlockModel extends Image {}

SlidingBlockModel
是否继承自
java.awt.Image
?必须继承,因为您调用的方法具有签名
void drawImage(Image,int,int,int,int,ImageObserver)
。这似乎是代码中唯一可能的问题。

SlidingBlockModel
需要以某种方式成为
java.awt.Image
的子类-直接或间接。因此,您需要从
Image
BuffereImage
VolatileImage
进行扩展


看起来不是现在,这就是为什么它抱怨找不到给定的方法类型,找不到具有匹配签名的方法。

您好,谢谢大家的回答。我发现了错误。毕竟是对象类导致了问题。您的回答很有价值。谢谢您帮助我学习. :)