1

I have been wanting to program a 2D game from scratch in Java for a while. The pixel aesthetic is one of my favorites, so I am aiming for a pixel 2D game. However, whenever I try to use BufferedImage to draw my tiles, the tiles become extremely distorted.

The tile drawn is actually bigger than the real tile and it seems like it has been stretched. Basically, say I have a 16x16 tile and I draw it. I can visually tell it is distorted when I run the program, and when I take a screenshot, I can measure the pixels and it has somehow become a 20x20.

I have also noticed that when I set a JFrame or a JPanel in the JFrame to a certain size, it is not the actual size that is produced. In my program I create a 320x320 JPanel and put it in a JFrame, but when I take a screenshot and measure the window, it comes up to about 399x399.

Can someone please tell me how to fix this. I stop every game project because the graphics keep looking like rubbish.

This is the Main class:

package main;

import javax.swing.SwingUtilities;

public class Main {

    public static void main(String args[]) {
        
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                Engine e = new Engine();
                e.start();
            }
            
        });
        
    }
    
}

This is the Engine class:

package main;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;

public class Engine {

    public JFrame f;

    public void initFrame() {
        f = new JFrame();
        f.setTitle("Something");
        f.setResizable(false);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLocationRelativeTo(null);
    }

    public void start() {

        initFrame();

        BufferedImage tree;
        try {
            tree = ImageIO.read(new File("res/boy_down_1.png"));
            Panel p = new Panel(tree);
            f.add(p);
            f.pack();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        f.setVisible(true);

    }

}

This is the Panel class:

package main;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import javax.swing.JPanel;

public class Panel extends JPanel {

    BufferedImage i;

    public Panel(BufferedImage image) {
        i = image;
        this.setDoubleBuffered(true);
        this.setPreferredSize(new Dimension(320, 320));
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.drawImage(i, 20, 20, null);
        g2d.dispose();
    }

}

This is the 16x16 I am trying to draw boy_down_1

This is what my computer shows me Screenshot of desktop

I have tried multiple ways to specify the size of the image, but Java seems to distort my image no matter what I do. Thank you in advance.

INeedHelp
  • 23
  • 5
  • 3
    I would guess your desktop has scaling set to 125%. If you don't want your application scaled then check out: https://stackoverflow.com/questions/65742162/how-to-fix-the-blurry-image-when-i-run-my-java-swing-project/65742492#65742492 – camickr Mar 28 '22 at 04:41
  • 1
    Access denied to those linkes. You should be able to paste the images into the post directly. Don't dispose of the graphics context at the end of your paint – Charlie Mar 28 '22 at 04:45
  • `g2d.dispose();` <- Don't dispose of a resource you didn't create – MadProgrammer Mar 28 '22 at 04:57
  • In addition to not disposing of the graphics object: 1) `g2d.drawImage(i, 20, 20, null);` should be `g2d.drawImage(i, 20, 20, this);`. Every `JComponent` **is an** `ImageObserver`. 2) Why not display the image in a `JLabel` with a 20 px left and top border? 3) `this.setDoubleBuffered(true);` any `JComponent` (or ancestor) is double buffered by default. 4) Good call on invoking the super method in the paint process. – Andrew Thompson Mar 28 '22 at 05:40
  • @camickr Thank you so flipping much. I went to system settings and sure enough, the scale value was 125%. This problem had made me quit so many projects, so thank you very much. – INeedHelp Mar 29 '22 at 02:38
  • @Charlie I will remember that thank you. – INeedHelp Mar 29 '22 at 02:39
  • @AndrewThompson I will try out the tips you gave me. I'm not really good at UI design in Java, so I've just been doing things I know how to do. To answer your second and third question, I just put some random numbers in because I was running tests to see how I could fix my problem. And lastly, thank you for the compliment, although I just know how to do it because I read about it on here. – INeedHelp Mar 29 '22 at 02:42

0 Answers0