Java2D不可接受的帧速率

时间:2014-04-02 06:03:08

标签: java performance graphics2d java-2d frame-rate

Java2D游戏运行20-30 FPS

我正在编写一个java2D游戏,截至目前,该游戏的内存占用率几乎达到了4GB可用RAM空间的2%左右。问题是游戏只是在渲染角色和周围环境时以大约20-30 fps的速度运行。我已经阅读了一些关于利用GraphicsConfiguration的内容,但当我尝试使用时,我的fps飙升至120-130 fps,而我的内存增加到70-80%,即使我移动了角色,框架也被冻结在一个地方。此时我被困住了。我认为它与我调用g2d.fillRect()的次数有关,因为在删除了被引用的区域之后,游戏似乎加速了。游戏似乎以20-30 fps的速度运行,但我需要解决这个问题,否则我的可视游戏对象画家循环会将我的游戏减少到只有滞后。

具有剪辑区域的渲染方法

private void renderEnvironment(Graphics2D g2d){

    for(int paint_index = 0; paint_index < paint_textures.size(); paint_index++){



        TexturePaint current_paint = paint_textures.get(paint_index);
        //Area a = new Area(new Rectangle(0, 0, host_frame.getWidth(), host_frame.getHeight()));
        //a.intersect(new Area(AffineTransform.getTranslateInstance(CamX, CamY).createTransformedShape(paint_regions.get(current_paint))));
        g2d.setPaint(new TexturePaint(current_paint.getImage(), new Rectangle(CamX, CamY, current_paint.getImage().getWidth(), current_paint.getImage().getHeight())));
        g2d.setClip(AffineTransform.getTranslateInstance(CamX, CamY).createTransformedShape(paint_regions.get(current_paint)));
        //g2d.fill(a);

        g2d.fillRect(0, 0, host_frame.getWidth(), host_frame.getHeight());

    }

    for(int paint_index = 0; paint_index < animated_textures.size(); paint_index++){

        TextureAnimation current_paint = animated_textures.get(paint_index);
        //Area a = new Area(new Rectangle(0, 0, host_frame.getWidth(), host_frame.getHeight()));
        //a.intersect(new Area(AffineTransform.getTranslateInstance(CamX, CamY).createTransformedShape(paint_regions.get(current_paint))));
        g2d.setPaint(new TexturePaint(current_paint.animatedPaint().getImage(), new Rectangle(current_paint.animatedPaint().getAnchorRect().getBounds().x + CamX, current_paint.animatedPaint().getAnchorRect().getBounds().y + CamY, current_paint.animatedPaint().getImage().getWidth(), current_paint.animatedPaint().getImage().getHeight())));
        g2d.setClip(AffineTransform.getTranslateInstance(CamX, CamY).createTransformedShape(paint_regions.get(current_paint)));
        //map_graphics.fill(a);
        g2d.fillRect(0, 0, host_frame.getWidth(), host_frame.getHeight()); 

    }g2d.setClip(null);
    g2d.drawImage(character_sprite.spriteImage(), host_frame.getWidth() / 2, host_frame.getHeight() / 2, host_frame);

    for(int paint_index = 0; paint_index < global_textures.size(); paint_index++){

        TextureAnimation current_paint = global_textures.get(paint_index);
        g2d.setPaint(new TexturePaint(current_paint.animatedPaint().getImage(), new Rectangle(current_paint.animatedPaint().getAnchorRect().getBounds().x + CamX, current_paint.animatedPaint().getAnchorRect().getBounds().y + CamY, current_paint.animatedPaint().getImage().getWidth(), current_paint.animatedPaint().getImage().getHeight())));
        //g2d.setClip(new Rectangle(0, 0, host_frame.getWidth(), host_frame.getHeight()));

        g2d.fillRect(0, 0, host_frame.getWidth(), host_frame.getHeight());

    }

}

编辑:TextureAnimation.java

public class TextureAnimation implements ActionListener {

private final Action behavior;
private final BufferedImage image;
private final Component host;

private TexturePaint paint;
private final Timer t;
private final boolean isGlobal;

public int x;
public int y;

public TextureAnimation(Action a, BufferedImage i, Component c, boolean global){

    isGlobal = global;
    behavior = a;
    image = i;
    host = c;

    paint = new TexturePaint(image, new Rectangle(0, 0, image.getWidth(), image.getHeight()));

    t = new Timer(300, this);
    t.setInitialDelay(0);


}

public void playAnimation(){

    t.start();

}

public Boolean isGlobal(){

   return isGlobal; 

}

public void delayAnimation(int ms_delay){

    t.setInitialDelay(ms_delay);
    t.restart();

}

public void setIntervalDelay(int ms_delay){

    t.setDelay(ms_delay);

}

public void setTilePosition(int x, int y){

    paint = new TexturePaint(image, new Rectangle(Gscale.setX(x), Gscale.setY(y), Gscale.setWidth(image.getWidth()), Gscale.setHeight(image.getHeight())));

}

public TexturePaint animatedPaint(){

    return paint;

}

@Override
public void actionPerformed(ActionEvent e) {

    behavior.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, null));
    //host.repaint(0,0, host.getWidth(), host.getHeight());
}

}

研究链接

Java2D Performance Issues

注意:Graphic2D参数采用JPanel本身的图形。所以java不应该为我创建一个兼容的图像吗?

0 个答案:

没有答案