开发者

Java applet crashes when I try to crop a picture

I've been trying to chop up a larger picture and use it as tiles for a game. I had the program working using fillRect() to simulate the image. However, when I replace the fillRect code, it crashes. Here is what I've been using:

buffer.drawImage(s开发者_开发百科ection[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;
   }
}

I tried "extends JApplet/JFrame/JComponent" for the Section Class, but it didn't seem to help.

Edit: I would also like to mention that if I would just return mainImage from getSectionImage(), I get the image. I think the biggest problem is the rest of that function... but I'm not sure, so I included everything I needed to add when replacing fillRect().


I found a much better way to crop this image. Here is how I did it:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜