Java2D Unacceptable Frame Rate

前提是你 提交于 2019-12-11 09:19:59

问题


Java2D Game Running 20-30 FPS

I am writing a java2D game that as of now has an almost perfect memory footprint running only at around 2% of about a 4GB usable RAM space. The problem is the game only is running at around 20-30 fps just from rendering the character and the surrounding environment. I read already something about utilizing the GraphicsConfiguration, but when I tried that my fps soared right up to 120-130 fps while my memory increased to 70-80% and the frame was frozen in one place even if i moved the character. At this point I am stuck. I assume it has something to do with how many times i call g2d.fillRect() because after removing an area that's being referenced the game seems to speed up. The game seems to run smoothly at 20-30 fps, but I need to fix this otherwise my viewable gameobject painter loop will reduce my game to nothing but lag.

Rendering Method With Clipping Areas

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

    }

}

EDIT : 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());
}

}

Researched Link

Java2D Performance Issues

Note : The Graphic2D parameter is taking on the graphics of the JPanel itself. So shouldn't java already create a Compatible image for me?

来源:https://stackoverflow.com/questions/22802772/java2d-unacceptable-frame-rate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!