gpt4 book ai didi

java - 为什么 java swing drawLine 不正确的宽度?

转载 作者:行者123 更新时间:2023-11-30 09:04:17 25 4
gpt4 key购买 nike

我正在编写一些简单的 Java 图形应用程序。这是我的问题:

package graphic;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class TestFrame extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel panel;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TestFrame frame = new TestFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public TestFrame() {
setTitle("Test Example");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
panel = new JPanel(){
private static final long serialVersionUID = 1L;

@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
final int MAX = 100;

//Draw grid
for (int i = 0; i < MAX; i++)
if (i % 2 == 0){
g.drawLine(0, i, MAX, i);
g.drawLine(i, 0, i, MAX);
}

g.setColor(Color.RED);
g.drawLine(0, 0, 0, 0);//width = 1
g.drawLine(0, 2, 1, 2);//width = 2
g.drawLine(0, 4, 2, 4);//width = 3
g.drawLine(0, 6, 3, 6);//width = 4


}
};

panel.setBorder(new EmptyBorder(5, 5, 5, 5));
panel.setLayout(new BorderLayout(0, 0));
setContentPane(panel);

}

}

我在 JPanel 中画了 4 条线。结果如下: enter image description here但是第二行取 3 个“正方形像素”,而 x1 = 0,x2 = 1 那么线宽只有 2 个正方形?第三行和第四行也有同样的问题。任何人都可以为我解释一下吗?谢谢

最佳答案

要么

  1. 您的图形系统出了点问题。也许您的屏幕正在进行某种放大或缩放。

  2. 您的运行时实现中存在错误。如果是这样,您可能想要提交错误并指定您的操作系统和运行时。

这是我系统上的结果:

enter image description here

关于java - 为什么 java swing drawLine 不正确的宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25324189/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com