1 year ago

#343196

test-img

INeedHelp

Java BufferedImage is not draw correctly

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.

java

swing

pixel

bufferedimage

0 Answers

Your Answer

Accepted video resources