我是Java初学者图形,我一直试图在JFrame上绘制随机点,但不知道为什么当我编译时,框架上没有任何东西,对我来说逻辑很好+没有错误。有人可以帮助我什么是错在这里使用二维图形在Java中绘制随机点
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);
}
}
感谢您的rpely,但我在每次迭代中使用%width或%height得到新的随机数,它甚至更独特,并且在进行下一次迭代之前,先绘制一个图,那就是使用PaintComponent – Sikander