2011-08-11 41 views
0

我一直在嘗試將較大的圖片切碎,並將其用作遊戲的切片。我有程序使用fillRect()來模擬圖像。但是,當我替換fillRect代碼時,它崩潰。下面是我一直使用的是什麼:當我嘗試裁剪圖片時,Java applet崩潰

buffer.drawImage(section[i][j].getSectionImage(i, j), 
    sectionSize * i + OFFSETx, 
    sectionSize * j + OFFSETy, 
    this); 

public class Section{ 
    private static ImageIcon ii; 
    private static Image mainImage; 
    private Image sectionImage; 

    public Section(){ 
     if (ii == null){ 
     ii = new ImageIcon(this.getClass().getResource("images/Mossy_rocks.png")); 
     mainImage = ii.getImage(); 
     } 
    } 
    public Image getSectionImage(int x, int y){ 
     sectionImage = createImage(new FilteredImageSource(mainImage.getSource(), 
     new CropImageFilter(1,1,20,20))); //test values 
     return sectionImage; 
    } 
} 

我想「擴展JApplet的/的JFrame/JComponent的」爲科級,但它似乎並沒有幫助。

編輯:我也想提一下,如果我只是從getSectionImage()返回mainImage,我會得到圖像。我認爲最大的問題是該函數的其餘部分......但我不確定,所以在替換fillRect()時包含了我需要添加的所有內容。

+1

試過調試嗎? – Tom

+0

任何異常是控制檯? – home

+0

它撞上了哪條線?變量中的值是什麼? – Randy

回答

0

我發現了一個更好的方式來裁剪此圖像。這是我是如何做到的:

screenImage.drawImage(Image sprite, 
       int (x position on screen), 
       int (y position on screen), 
       int (x position on screen + width), 
       int (y position on screen + height), 
       int (x position from sprite), 
       int (y position from sprite), 
       int (x position from sprite + width), 
       int (y position from sprite + height), 
       null); 
相關問題