gpt4 book ai didi

java - 使用 2D 图形在 Java 中绘制随机点

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:31:40 25 4
gpt4 key购买 nike

我是 java Graphics 的初学者,我一直在尝试在 JFrame 上绘制随机点,但不知道为什么当我编译时 Frame 上没有任何东西,对我来说逻辑很好 + 没有错误。谁能帮我看看这是怎么回事

public class parent extends JPanel {    

public void PaintComponent (Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.blue);
Dimension size = getSize();
Insets insets= getInsets();
int w = size.width - insets.left - insets.right;
int h = size.height - insets.top - insets.bottom;
Random r = new Random();

for (int i=0; i<1000; i++) {
int x = Math.abs(r.nextInt()) % w;
int y = Math.abs(r.nextInt()) % h;
g2d.drawLine(x, y, x, y);
}
}


public static void main(String[] args)
{
JFrame frame = new JFrame("Points");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new parent());
frame.setSize(250, 200);
frame.setVisible(true);
}
}

最佳答案

它是 PaintComponent 还是应该是 paintComponent?我想我知道 Java 开发人员会选择哪一个。

请注意,这是一个完美的错误,注释中的 @Override 会在 Eclipse 中捕获。

关于java - 使用 2D 图形在 Java 中绘制随机点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13166399/

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