使用getsubimage进行Java裁剪和绘制图像

时间:2013-10-09 14:35:19

标签: java image crop drawimage

在主类中,我正在绘制图形对象:

//FIELDS
double FPS = 1000 / 60;
BufferStrategy bs;
public String ip; 
private int cx = 0;
private Hero h;
private TileMap map;
private int speed = 12;



public mainCla (){

    setPreferredSize(new Dimension(WIDTH, HEIGHT));
    h = new Hero("Hero/res/hero.png",100,50);
    map = new TileMap ("Hero/res/jun.png");


}


private void draw() {
        bs = getBufferStrategy();
        if (bs == null) {
        createBufferStrategy(3);
        return;
    }

    Graphics g = bs.getDrawGraphics();
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, getWidth(), getHeight());
    g.setColor(Color.GREEN);
    g.drawImage(h.hi, h.hx, h.hy, this);
    g.fillOval(cx, 200, 40, 40);

    bs.show();

}

然后在另一个类中,我设置了加载图像的裁剪,如下所示:

public class TileMap {

//FIELDS
public String livpath;
BufferedImage mappa;
public int imgmapwidth;
public int imgmapheight;


public TileMap (String livpath ){
    this.livpath = livpath;
    BuildBlock();
    try {
        mappa = ImageIO.read (new File (livpath));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
}

public void BuildBlock () {
    for (int x = 0; x < 128; x++)
        { for (int y = 0; y < 128; y++) {
        mappa.getSubimage (0,0,x,y);
        }       
        }
}

之后,我尝试从主类中使用g.drawimage绘制提取的tile(显然我已经根据构造函数的请求创建了一个带有图像路径的实例)但是图像本身并不是展示。任何的想法?感谢

0 个答案:

没有答案