I am working on a java gui application using a Canvas on a JFrame. I tried to draw thin rects on a Jframe and noticed, that they not allways have the same width displayed.
This is the rusult of a test class I wrote. The left side is drawn by using fillRect()with the width of 1 and the right side is drawn using drawLine.
This is the code of my test class.
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferStrategy;
public class test {
public static void main(String[]args){
new test();
}
public test(){
JWindow frame = new JWindow();
frame.setSize(500,500);
frame.setLocation(200,200);
Canvas c = new Canvas();
frame.add(c);
frame.setVisible(true);
new Thread(() -> {
while(true) {
BufferStrategy bs = c.getBufferStrategy();
if (bs == null) {
c.createBufferStrategy(2);
bs = c.getBufferStrategy();
}
Graphics g = bs.getDrawGraphics();
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.black);
for(int i = 0; i < 50; i++){
if(i % 4 == 0){
g2.fillRect(20 + i, 50, 1, 100);
}
else if(i % 4 == 1){
g2.fillRect(20 + i, 150, 1, 100);
}
else if(i % 4 == 2){
g2.fillRect(20 + i, 250, 1, 100);
}
else{
g2.fillRect(20 + i, 350, 1, 100);
}
}
for(int i = 0; i < 50; i++){
if(i % 4 == 0){
g2.drawLine(100 + i, 50, 100 + i, 150);
}
else if(i % 4 == 1){
g2.drawLine(100 + i, 150, 100 + i, 250);
}
else if(i % 4 == 2){
g2.drawLine(100 + i, 250, 100 + i, 350);
}
else{
g2.drawLine(100 + i, 350, 100 + i, 450);
}
}
g.dispose();
bs.show();
}
}).start();
}
}
Does someone know how that works or maby even a way to fix that?