r/CodingHelp • u/piengepool • 13d ago
[Java] Swing help. Image gets distorted when partially outside window.
I have a JPanel class that draws an image on screen with camera panning. The camera is zoomed in. The image being drawn is a 16x16 Minecraft dirt.
When the image is entirely inside the window, it looks fine. When partially outside the window, it distorts and makes the image narrower to fit it inside the window. Eventually it snaps to the next pixel.
Does anyone know why this happens and how to fix it?
I'm using:
MacBook Air M1
Java 25
IntelliJ
The entire class is below.
public class CameraPanel extends JPanel{
public static void main(String[] args) throws IOException{
JFrame frame = new JFrame("Camera");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new CameraPanel());
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private double cameraX, cameraY;
private double zoom = 10.0;
private Point lastMousePosition;
private BufferedImage image = ImageIO.read(new File("src/test/resources/dirt.png"));
public CameraPanel() throws IOException{
addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
lastMousePosition = e.getPoint();
}
public void mouseReleased(MouseEvent e){
lastMousePosition = null;
}
});
addMouseMotionListener(new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e){
if(lastMousePosition == null){
return;
}
cameraX += e.getX() - lastMousePosition.x;
cameraY += e.getY() - lastMousePosition.y;
lastMousePosition = e.getPoint();
repaint();
}
});
}
protected void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(
RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR
);
AffineTransform transform = new AffineTransform();
transform.translate(cameraX, cameraY);
transform.scale(zoom, zoom);
g2.drawImage(image, transform, null);
}
}7
u/gofuckadick
13d ago
•
Alright, so I figured it would be easiest to just grab your code, add the missing imports, and test it locally (using Java 25.0.1 on Windows 11 with a 16×16 Minecraft dirt texture). And turns out, I wasn't able to reproduce the distortion, even when dragging the image partially outside the panel all around the window like you showed in your clip.
I also tried running it with Java2D's default rendering pipeline, explicitly enabling OpenGL (-Dsun.java2d.opengl=true), and disabling both Direct3D and OpenGL to test software rendering. But, everything rendered correctly in all three cases. That makes me think this could be related to something specific to your macOS environment, particularly Java2D's Metal rendering pipeline.
One thing worth trying would be to disable Metal when launching your application:
java -Dsun.java2d.metal=false CameraPanelThis should make Java2D fall back to software rendering. If the distortion disappears, that would be a pretty good indication that the Metal pipeline is involved.
If disabling Metal doesn't change anything, other possibilities include Retina/HiDPI scaling, differences between JDK builds, or some other macOS-specific rendering behavior. Your M1 Mac's display scaling could affect how Java2D maps logical coordinates to physical pixels, potentially introducing rounding or sampling differences that don't happen on my Windows setup. And while we're both using Java 25, there could still be differences between the specific JDK distributions or builds.
Another useful test would be to replace your affine-transform drawing call with the explicit-coordinate version of drawImage():
int x = (int) Math.round(cameraX);
int y = (int) Math.round(cameraY);
g2.drawImage(image, x, y,
image.getWidth() * 10,
image.getHeight() * 10, null);That preserves the intended 10× scaling while changing how the drawing operation is submitted to Java2D. If this works correctly while the original affine-transform version doesn't, it would help narrow the issue to the rendering path used for transformed images.
10
2
3
u/grosswindowthrowaway 13d ago
It's probably a quirk of the Graphics2D implementation and how it handles drawImage() when the rectangle goes out of bounds.
I went down a rabbit hole and ended up reading the openjdk source's drawImage impl, which led me to sun.java2d.pipe.DrawImage, which seems to be where the transformation is actually calculated.
Maybe something in there will give you a hint? It's clear some sort of snapping to the original pixels is happening, so perhaps some rounding is being done prematurely.
•
u/AutoModerator 13d ago
Thank you for posting on r/CodingHelp!
Please check our Wiki for answers, guides, and FAQs: https://coding-help.vercel.app
Our Wiki is open source - if you would like to contribute, create a pull request via GitHub! https://github.com/DudeThatsErin/CodingHelp
We are accepting moderator applications: https://forms.fillout.com/t/ua41TU57DGus
We also have a Discord server: https://discord.gg/geQEUBm
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.