Random Circle Printer Java: 图形未打印

huangapple go评论105阅读模式
英文:

Random Circle Printer Java: Graphics not printing

问题

我尝试创建一个代码,以在一个 500 x 500 的框架中打印 n 个随机圆,但它没有运行。

有人可以告诉我为什么这段代码不运行吗?

当我运行这段代码时,它让我输入我想要的随机圆的数量,但框架总是空白的 - 没有画出圆。

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.util.Scanner;

import javax.swing.JComponent;
import javax.swing.JFrame;

public class RandomCircles extends JComponent
{
    private int n;

    public RandomCircles(int N)
    {
        n = N;
    }

    public void PaintComponent(Graphics g)
    {
        Graphics2D g2 = (Graphics2D) g;

        double x = Math.random() * 500;
        double y = Math.random() * 500;
        double diameter = Math.random() * 500;

        // 确保圆圈保持在框架内
        for (int i = 0; i < n; i++)
        {
            while(x + diameter <= 500 || y + diameter <= 500)
            {
                Ellipse2D.Double circle 
                    = new Ellipse2D.Double(x, y, diameter, diameter);
                g2.draw(circle);
            }
        }
    }

    public static void main(String[]args)
    {
        Scanner in = new Scanner(System.in);

        System.out.println("在这里输入圆的数量:");
        int n = in.nextInt();

        JFrame frame = new JFrame();
        frame.setSize(500, 500);
        frame.setTitle("随机圆");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        RandomCircles circle = new RandomCircles(n);
        frame.add(circle);
        
        // 在这里添加 PaintComponent 方法?

        frame.setVisible(true);
    }
}

我有一种感觉,我需要在某个地方添加 public void PaintComponent(Graphics g) 方法来进行绘制,但我不确定如何添加。

英文:

I tried to create a code that printed n random circles in a 500 x 500 frame but it didn't work.

Can somebody tell me why this code isn't running?

When I run this code, it lets me enter the number of random circles I want but the frame always appear to be empty - no circles are drawn.

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.util.Scanner;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class RandomCircles extends JComponent
{
private int n;
public RandomCircles(int N)
{
n = N;
}
public void PaintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
double x = Math.random() * 500;
double y = Math.random() * 500;
double diameter = Math.random() * 500;
// Making sure the circle stays within the frame
for (int i = 0; i &lt; n; i++)
{
while(x + diameter &lt;= 500 || y + diameter &lt;= 500)
{
Ellipse2D.Double circle 
= new Ellipse2D.Double(x, y, diameter, diameter);
g2.draw(circle);
}
}
}
public static void main(String[]args)
{
Scanner in = new Scanner(System.in);
System.out.println(&quot;Enter number of circles here: &quot;);
int n = in.nextInt();
JFrame frame = new JFrame();
frame.setSize(500, 500);
frame.setTitle(&quot;Random Circles&quot;);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
RandomCircles circle = new RandomCircles(n);
frame.add(circle);
// Add PaintComponent method somewhere here? 
frame.setVisible(true);
}
}

I have a feeling that I need to add in the public void PaintComponent(Graphics g) somewhere to print it out, but I am not sure how.

答案1

得分: 1

这个问题在这一行:

public void paintComponent(Graphics g)

您试图重写paintComponent(Graphics)方法。您需要小心确保名称和参数正确。注意到您的方法名称首字母 P 大写了。

建议您在应该重写父类方法的方法上添加注解 @Override。这样,如果您签名不正确,就会收到通知。

所以您的方法应该像这样:

@Override
public void paintComponent(Graphics g)
{
    Graphics2D g2 = (Graphics2D) g;
    ...
}

== 在评论后进行编辑 ==

while 循环也会引起问题。试试这样做:

for (int i = 0; i < n; i++)
{
    double x = Math.random() * 500;
    double y = Math.random() * 500;
    double diameter = Math.random() * 500;
   
    Ellipse2D.Double circle = new Ellipse2D.Double(x, y, diameter, diameter);
    g2.draw(circle);
}

请注意,我将随机数生成移动到了循环内部。这并不保证圆形适合框架,但这是您稍后可以修改的内容。

英文:

The problem is on this line:

public void PaintComponent(Graphics g)

You are attempting to override the paintComponent(Graphics) method. You need to be careful that you get the name and the parameters right. Notice you spelled your method with an upper case P.

It is advisable that you add the annotation @Override on methods that are supposed to override a super class' method. That way you get a notification if you get the signature wrong.

So your method should look like this:

@Override
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
...
}

== Edit after comments ==

The while loop is also causing an issue. Try doing this instead.

for (int i = 0; i &lt; n; i++)
{
double x = Math.random() * 500;
double y = Math.random() * 500;
double diameter = Math.random() * 500;
Ellipse2D.Double circle
= new Ellipse2D.Double(x, y, diameter, diameter);
g2.draw(circle);
}

Notice that I moved the random number generation inside the loop. This does not guarantee the circle fitting in the frame but that is something you can modify later.

huangapple
  • 本文由 发表于 2020年7月23日 03:35:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/63041918.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定