如何创建 Ellipse2D 数组?

发布于 11-24 23:29 字数 664 浏览 1 评论 0原文

我有一个我一直在工作的程序:它从用户那里获取数据,用它做一些数学运算,然后在屏幕上显示一个椭圆,当输入新数据时,旧的椭圆消失,新的椭圆取代它。但是,我需要该程序将旧椭圆和新椭圆保留在屏幕上,以便我可以比较大小。我对此的解决方案是拥有它,以便在创建椭圆时将其存储在数组中,然后将椭圆数组绘制到屏幕上,我还需要它以便用户可以清除数组并重新开始。但是我无法让代码工作。你愿意帮忙吗?

下面是我用来创建和绘制椭圆的代码,所有使用的变量都只是数字。

public void paint(Graphics g) {
    super.paint(g);
    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setPaint(Color.white);
   g2.draw(new Ellipse2D.Double(((Background.getWidth()) / 2) - (gblSemiMajaxis / 2), ((Background.getHeight()) / 2) - (gblsemiMinoraxis / 2), gblSemiMajaxis, gblsemiMinoraxis));
}

I have a program that I've been working: it takes data from the user does some maths with it and then displays an ellipse to the screen, when new data is entered the old ellipses disappears and the new one replaces it. However I need the program to keep the old ellipse on the screen as well as the new ones so I can compare sizes. My solution to this is to have it so that when an ellipse is created it is stored in an array, and then the array of ellipse is drawn onto the screen, I also need it so that the user can clear the array and start over. However I cannot get the code to work. Will you please help?

Below is the code that I used to create and draw the ellipse all of the variables used are just numbers.

public void paint(Graphics g) {
    super.paint(g);
    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setPaint(Color.white);
   g2.draw(new Ellipse2D.Double(((Background.getWidth()) / 2) - (gblSemiMajaxis / 2), ((Background.getHeight()) / 2) - (gblsemiMinoraxis / 2), gblSemiMajaxis, gblsemiMinoraxis));
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

享受孤独2024-12-01 23:29:06

只是为了扩展伊恩·麦克拉里德的答案:

// imports
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;

...

ArrayList<Ellipse2D> ellipseList = new ArrayList<Ellipse2D>();

public void createEllipse(double gblSemiMajaxis, double gblSemiMinoraxis) {
    Ellipse2D e = new Ellipse2D.Double(((Background.getWidth()) / 2) - (gblSemiMajaxis / 2), ((Background.getHeight()) / 2) - (gblSemiMinoraxis / 2), gblSemiMajaxis, gblSemiMinoraxis);
    ellipseList.add(e);
}

public void paint(Graphics g) {
    super.paint(g);
    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setPaint(Color.white);

    for (Ellipse2D e : ellipseList) {
        g2.draw(e);
    }
}

Just to expand on Ian McLarid's answer:

// imports
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;

...

ArrayList<Ellipse2D> ellipseList = new ArrayList<Ellipse2D>();

public void createEllipse(double gblSemiMajaxis, double gblSemiMinoraxis) {
    Ellipse2D e = new Ellipse2D.Double(((Background.getWidth()) / 2) - (gblSemiMajaxis / 2), ((Background.getHeight()) / 2) - (gblSemiMinoraxis / 2), gblSemiMajaxis, gblSemiMinoraxis);
    ellipseList.add(e);
}

public void paint(Graphics g) {
    super.paint(g);
    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setPaint(Color.white);

    for (Ellipse2D e : ellipseList) {
        g2.draw(e);
    }
}
往事风中埋2024-12-01 23:29:06

我建议为您的类提供一个 ArrayList 类型的成员变量。当您的用户输入其输入时,创建 Ellipse2D 并将其添加到列表中。在您的绘制函数中,您可以迭代列表并绘制您已经制作的每个椭圆。当用户想要清除所有椭圆时,可以使用 ArrayList 的 clear() 方法。

I would suggest giving your class a member variable of type ArrayList<Ellipse2D>. When your user enters their input, create the Ellipse2D and add it to the list. In your paint function, you could iterate over the list and paint each of the Ellipses you've already made. When the user wants to clear all of the Ellipses, you could use the ArrayList's clear() method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文