자바 초보자입니다. Graphics, JFrame에 무작위 점을 그려 넣으려고했지만 프레임이 없으면 컴파일 할 때 왜 논리가 괜찮습니까? + 아니오 오류.2D 그래픽을 사용하여 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);
}
}
당신의 고마워,하지만 각 반복에서 % 너비 또는 % 높이를 사용하여 새로운 무작위로 안돼 점점 더 고유 한, 그리고 전에 다음 반복에 대한 이전에 하나의 플롯, 그게 좋은 문제 PaintComponent – Sikander