I am having an issue with a custom drawn image rendered on the JPanel with Graphics2D. If I try to draw a 1x1 pixel for every x/y the width and length of the panel with fillRect it will create a bunch of different sized rects of 1x1, 1x2, and 2x2 in a pattern. I've tried playing with the Graphics2D rendering hints but could not figure out what is causing this to happen.
Here is a zoomed in view of what is happening, the rect sizing appears to create a pattern as well.
Here is a minimally viable example that should be printing 1x1 rects.
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
class Scratch {
public static final int WIDTH = 400;
public static final int HEIGHT = 400;
public static void main(String[] args) {
BufferedImage img = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D g2d = img.createGraphics();
JPanel panel = new JPanel() {
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D panel2d = (Graphics2D) g;
// Black bg
g2d.setColor(Color.BLACK);
g2d.fillRect(0, 0, getWidth(), getHeight());
int c = 0;
for (int x = 0; x < getWidth(); x++) {
for (int y = 0; y < getHeight(); y++) {
g2d.setColor(new Color((c += 13) % 255, (c += 17) % 255, (c += 23) % 255));
g2d.fillRect(x, y, 1, 1);
}
}
panel2d.drawImage(img, null, 0, 0);
}
};
JFrame frame = new JFrame("Scratch");
frame.setLayout(new BorderLayout());
frame.add(panel, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setVisible(true);
frame.getContentPane().setPreferredSize(new Dimension(WIDTH, HEIGHT));
frame.pack();
}
}
Or should I be using something other than Graphics2D to do this?
-
1What image are you expecting from this, and why?Hovercraft Full Of Eels– Hovercraft Full Of Eels2024年08月02日 15:10:38 +00:00Commented Aug 2, 2024 at 15:10
-
In the example I am expecting all the drawn rects to be the same exact size (1x1). The why is because I am working on a 2d game drawing those pixels more meaningfully but this example is just to show the problem behavior I was seeing.Matthew Wright– Matthew Wright2024年08月02日 15:21:37 +00:00Commented Aug 2, 2024 at 15:21
-
They are the same size, but your algorithm creates a pattern of rectangles of the same colors.Hovercraft Full Of Eels– Hovercraft Full Of Eels2024年08月02日 15:26:29 +00:00Commented Aug 2, 2024 at 15:26
-
If you were to change the color to be completely random you would see the same rect sizing.Matthew Wright– Matthew Wright2024年08月02日 15:29:19 +00:00Commented Aug 2, 2024 at 15:29
-
3I am very certain it is not caused by the same colors occurring side by side. It may actually be caused by something with windows scaling and dpi? I just checked and my monitor was set to 125% in display settings and when changing that to 100% the pixels are now 1x1 as I expectMatthew Wright– Matthew Wright2024年08月02日 15:48:37 +00:00Commented Aug 2, 2024 at 15:48
1 Answer 1
I don't think the issue is that it's creating rectangles of different sizes, but rather that multiple rectangles of the same color are being created. That means your color function is likely the cause of the issue. If your objective is to have the colors transition from black to white then skip to yellow and red (that's what it looks like), you could test for a uniform value for the R, G and B value like this:
g2d.setColor(new Color((c += 15) % 255, (c += 15) % 255, (c += 15) % 255));
You could keep changing the value being incremented to get different patterns. If possible could you send the pattern you're trying to create?