Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

The reason is that background is a rather large image, and the g2d is performing a scale and a rotate every time. Enabling OpenGL via -Dsun.java2d.opengl=True as suggested heresuggested here might help, but I haven't been able to enable it on my machine.

Do note as well that it may be worth just using an existing game engineit may be worth just using an existing game engine.

The reason is that background is a rather large image, and the g2d is performing a scale and a rotate every time. Enabling OpenGL via -Dsun.java2d.opengl=True as suggested here might help, but I haven't been able to enable it on my machine.

Do note as well that it may be worth just using an existing game engine.

The reason is that background is a rather large image, and the g2d is performing a scale and a rotate every time. Enabling OpenGL via -Dsun.java2d.opengl=True as suggested here might help, but I haven't been able to enable it on my machine.

Do note as well that it may be worth just using an existing game engine.

added 180 characters in body
Source Link
Justin
  • 3.3k
  • 1
  • 21
  • 39

In a proof-of-concept style, I added another volatile BufferedImage drawnBackground as a field, and I added this to the end of your Main constructor:

new Thread(() -> {
    while(true) {
        if (background == null) continue;
        // A new BufferedImage to avoid concurrency issues; in this way,
        // the reference to this.drawnBackground is always a valid image
        // to be drawn. If we reassign this.drawnBackground = drawnBackground
        // while paintComponent() is running, that's fine, as it will simply
        // draw the old image.
        // It's also important to note that assignment to references
        // is an atomic operation, sobut we don'tstill have to declare the field
        // as volatile
        BufferedImage drawnBackground = new BufferedImage(background.getWidth(), background.getHeight(), background.getType());
        Graphics2D g2d = (Graphics2D) drawnBackground.getGraphics();

        g2d.setColor(Color.WHITE);
        g2d.fillRect(0, 0, background.getWidth(), background.getHeight());
        float xscale = (float) SCREENWIDTH / WIDTH;
        float yscale = (float) SCREENHEIGHT / HEIGHT;
        g2d.scale(xscale, yscale);
        g2d.rotate(Math.toRadians(45), WIDTH / 2, HEIGHT / 2);
        g2d.drawImage(background, 0, 0, this);

        this.drawnBackground = drawnBackground;
    }
}).start();

In a proof-of-concept style, I added another BufferedImage drawnBackground as a field, and I added this to the end of your Main constructor:

new Thread(() -> {
    while(true) {
        if (background == null) continue;
        // A new BufferedImage to avoid concurrency issues; in this way,
        // the reference to this.drawnBackground is always a valid image
        // to be drawn. If we reassign this.drawnBackground = drawnBackground
        // while paintComponent() is running, that's fine, as it will simply
        // draw the old image.
        // It's also important to note that assignment to references
        // is an atomic operation, so we don't have to declare the field
        // as volatile
        BufferedImage drawnBackground = new BufferedImage(background.getWidth(), background.getHeight(), background.getType());
        Graphics2D g2d = (Graphics2D) drawnBackground.getGraphics();

        g2d.setColor(Color.WHITE);
        g2d.fillRect(0, 0, background.getWidth(), background.getHeight());
        float xscale = (float) SCREENWIDTH / WIDTH;
        float yscale = (float) SCREENHEIGHT / HEIGHT;
        g2d.scale(xscale, yscale);
        g2d.rotate(Math.toRadians(45), WIDTH / 2, HEIGHT / 2);
        g2d.drawImage(background, 0, 0, this);

        this.drawnBackground = drawnBackground;
    }
}).start();

In a proof-of-concept style, I added another volatile BufferedImage drawnBackground as a field, and I added this to the end of your Main constructor:

new Thread(() -> {
    while(true) {
        if (background == null) continue;
        // A new BufferedImage to avoid concurrency issues; in this way,
        // the reference to this.drawnBackground is always a valid image
        // to be drawn. If we reassign this.drawnBackground = drawnBackground
        // while paintComponent() is running, that's fine, as it will simply
        // draw the old image.
        // It's also important to note that assignment to references
        // is an atomic operation, but we still have to declare the field
        // as volatile
        BufferedImage drawnBackground = new BufferedImage(background.getWidth(), background.getHeight(), background.getType());
        Graphics2D g2d = (Graphics2D) drawnBackground.getGraphics();

        g2d.setColor(Color.WHITE);
        g2d.fillRect(0, 0, background.getWidth(), background.getHeight());
        float xscale = (float) SCREENWIDTH / WIDTH;
        float yscale = (float) SCREENHEIGHT / HEIGHT;
        g2d.scale(xscale, yscale);
        g2d.rotate(Math.toRadians(45), WIDTH / 2, HEIGHT / 2);
        g2d.drawImage(background, 0, 0, this);

        this.drawnBackground = drawnBackground;
    }
}).start();
added 180 characters in body
Source Link
Justin
  • 3.3k
  • 1
  • 21
  • 39

The reason is that background is a rather large image, and the g2d is performing a scale and a rotate every time. If you enabledEnabling OpenGL via -Dsun.java2d.opengl=True as suggested here might help, but I haven't been able to enable it on my machine.

new Thread(() -> {
    while(true) {
        if (background == null) continue;
        // A new BufferedImage to avoid concurrency issues; in this way,
        // the reference to this.drawnBackground is always a valid image
        // to be drawn. If we reassign this.drawnBackground = drawnBackground
        // while paintComponent() is running, that's fine, as it will simply
        // draw the old image.
        // It's also important to note that assignment to references
        // is an atomic operation, so we don't have to declare the field
        // as volatile
        BufferedImage drawnBackground = new BufferedImage(background.getWidth(), background.getHeight(), background.getType());
        Graphics2D g2d = (Graphics2D) drawnBackground.getGraphics();

        g2d.setColor(Color.WHITE);
        g2d.fillRect(0, 0, background.getWidth(), background.getHeight());
        float xscale = (float) SCREENWIDTH / WIDTH;
        float yscale = (float) SCREENHEIGHT / HEIGHT;
        g2d.scale(xscale, yscale);
        g2d.rotate(Math.toRadians(45), WIDTH / 2, HEIGHT / 2);
        g2d.drawImage(background, 0, 0, this);

        this.drawnBackground = drawnBackground;
    }
}).start();

The reason is that background is a rather large image, and the g2d is performing a scale and a rotate every time. If you enabled OpenGL via -Dsun.java2d.opengl=True as suggested here might help, but I haven't been able to enable it on my machine.

new Thread(() -> {
    while(true) {
        if (background == null) continue;
        // A new BufferedImage to avoid concurrency issues; in this way,
        // the reference to this.drawnBackground is always a valid image
        // to be drawn. If we reassign this.drawnBackground = drawnBackground
        // while paintComponent() is running, that's fine, as it will simply
        // draw the old image.
        BufferedImage drawnBackground = new BufferedImage(background.getWidth(), background.getHeight(), background.getType());
        Graphics2D g2d = (Graphics2D) drawnBackground.getGraphics();

        g2d.setColor(Color.WHITE);
        g2d.fillRect(0, 0, background.getWidth(), background.getHeight());
        float xscale = (float) SCREENWIDTH / WIDTH;
        float yscale = (float) SCREENHEIGHT / HEIGHT;
        g2d.scale(xscale, yscale);
        g2d.rotate(Math.toRadians(45), WIDTH / 2, HEIGHT / 2);
        g2d.drawImage(background, 0, 0, this);

        this.drawnBackground = drawnBackground;
    }
}).start();

The reason is that background is a rather large image, and the g2d is performing a scale and a rotate every time. Enabling OpenGL via -Dsun.java2d.opengl=True as suggested here might help, but I haven't been able to enable it on my machine.

new Thread(() -> {
    while(true) {
        if (background == null) continue;
        // A new BufferedImage to avoid concurrency issues; in this way,
        // the reference to this.drawnBackground is always a valid image
        // to be drawn. If we reassign this.drawnBackground = drawnBackground
        // while paintComponent() is running, that's fine, as it will simply
        // draw the old image.
        // It's also important to note that assignment to references
        // is an atomic operation, so we don't have to declare the field
        // as volatile
        BufferedImage drawnBackground = new BufferedImage(background.getWidth(), background.getHeight(), background.getType());
        Graphics2D g2d = (Graphics2D) drawnBackground.getGraphics();

        g2d.setColor(Color.WHITE);
        g2d.fillRect(0, 0, background.getWidth(), background.getHeight());
        float xscale = (float) SCREENWIDTH / WIDTH;
        float yscale = (float) SCREENHEIGHT / HEIGHT;
        g2d.scale(xscale, yscale);
        g2d.rotate(Math.toRadians(45), WIDTH / 2, HEIGHT / 2);
        g2d.drawImage(background, 0, 0, this);

        this.drawnBackground = drawnBackground;
    }
}).start();
Source Link
Justin
  • 3.3k
  • 1
  • 21
  • 39
Loading