2011-03-12 98 views
0

我正在嘗試製作滑塊遊戲。在這個類中,我使用drawImage方法來顯示「拼圖」的塊,使用Graphics對象g2的drawImage方法。但在paint class方法中,我得到這個錯誤:找不到符號方法drawImage(SlidingBlockModel,int,int,int,int,)。有什麼建議麼?我很感謝你在閱讀本文時的時間。先謝謝你。 :)找不到符號方法drawImage(SlidingBlockModel,int,int,int,int,<nulltype>)

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


     } 
    } 

} 


} 

回答

1

這意味着你的SBModel不java.awt.Image中的類型。

嘗試改變你的類SlidingBlockModel以這種方式:

SlidingBlockModel extends Image {} 
1

是否SlidingBlockModel繼承java.awt.Image?這將不得不,因爲你打電話的方法有簽名void drawImage(Image, int, int, int, int, ImageObserver)。這似乎是代碼唯一可能的問題。

+0

大家好,謝謝大家的回答。我發現了錯誤。畢竟是導致問題的對象類。你的回答很有價值。謝謝你幫助我學習。 :) – 2011-03-13 13:31:10

0

SlidingBlockModel需要以某種方式直接或間接地成爲java.awt.Image的子類。所以你需要延長Image,BufferedImageVolatileImage

看起來它不是現在這就是爲什麼它抱怨說它無法找到給定的方法類型,它找不到具有該匹配簽名的方法。

相關問題