使用 Java2D 绘制多个圆

发布于 2024-08-17 06:51:10 字数 2347 浏览 5 评论 0原文

我正在尝试使用Java2D 进行一些简单的图形编程。我开始很简单,只是尝试在 JFrame 中显示几个圆圈。我成功显示了一个圆圈,但是当添加第二个圆圈时,仅显示添加到 JFrame 的最后一个圆圈。我使用 Circle 类来定义我的圆并重写用于显示它的 PaintComponent 方法。任何有关我可能做错的事情的建议将不胜感激。下面提供了我的 Circle 和 DancingCircles 类的代码以供参考。

import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;
/**
 *
 * @author Paul
 */
public class Circle extends JPanel {

// Data members for Circle center and radius
private double centerX, centerY;
private double radius;

// No-argument constructor
Circle() {
    centerX = 200;
    centerY = 200;
    radius = 10;
}

// Full-argument constructor
Circle( double x, double y, double r) {
    centerX = x;
    centerY = y;
    radius = r;
}

// Draw a Circle
protected void paintComponent(Graphics g) {
    super.paintComponent(g);

    //Convert to Java2D Object
    Graphics2D g2 = (Graphics2D) g;

    // Create the circle
    Ellipse2D circle = new Ellipse2D.Double();
    circle.setFrameFromCenter(centerX, centerY, centerX + radius, centerY + radius);

    // Draw it
    g2.draw(circle);
}// end paintComponent

// Get/set data members
public void setCenterX(double x){this.centerX = x;}
public void setCenterY(double y){this.centerY = y;}
public void setRadius(double r){radius = r;}

public double getCenterX(){return centerX;}
public double getCenterY(){return centerY;}
public double getRadius(){return radius;}
}// end class Circle


import java.awt.*;
import javax.swing.*;

/**
 *
 * @author Paul
 */
public class DancingCircles extends JFrame{

// Display Dimensions
public static final int DEFAULT_WIDTH = 400;
public static final int DEFAULT_HEIGHT = 400;

// Default constructor
private DancingCircles() {
    setTitle("Dancing Circles");
    setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

    // Add Circles to JFrame
    Circle myCircle = new Circle(200.0, 200.0, 20.0);
    add(myCircle);          // Add circle to frame
    Circle myCircle2 = new Circle(100.0, 100.0, 30.0);
    add(myCircle2);        // Add circle to frame
}// end DancingCircles

public static void main(String[] args) {

    EventQueue.invokeLater(new Runnable()
    {
        public void run()
        {
            DancingCircles dc = new DancingCircles();
            dc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            dc.setVisible(true);
        }
    });

 }// end main
}

谢谢!

保罗

I am trying to use Java2D to do some simple graphics programming. I've started easy, just trying to display a couple of circles in a JFrame. I was successful displaying a single circle, but when adding a second circle, only the last circle added to the JFrame is displayed. I use class Circle to define my circle and to override the paintComponent method used to display it. Any suggestions on what I might be doing wrong would be greatly appreciated. Code for my classes Circle and DancingCircles is provided below for reference.

import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;
/**
 *
 * @author Paul
 */
public class Circle extends JPanel {

// Data members for Circle center and radius
private double centerX, centerY;
private double radius;

// No-argument constructor
Circle() {
    centerX = 200;
    centerY = 200;
    radius = 10;
}

// Full-argument constructor
Circle( double x, double y, double r) {
    centerX = x;
    centerY = y;
    radius = r;
}

// Draw a Circle
protected void paintComponent(Graphics g) {
    super.paintComponent(g);

    //Convert to Java2D Object
    Graphics2D g2 = (Graphics2D) g;

    // Create the circle
    Ellipse2D circle = new Ellipse2D.Double();
    circle.setFrameFromCenter(centerX, centerY, centerX + radius, centerY + radius);

    // Draw it
    g2.draw(circle);
}// end paintComponent

// Get/set data members
public void setCenterX(double x){this.centerX = x;}
public void setCenterY(double y){this.centerY = y;}
public void setRadius(double r){radius = r;}

public double getCenterX(){return centerX;}
public double getCenterY(){return centerY;}
public double getRadius(){return radius;}
}// end class Circle


import java.awt.*;
import javax.swing.*;

/**
 *
 * @author Paul
 */
public class DancingCircles extends JFrame{

// Display Dimensions
public static final int DEFAULT_WIDTH = 400;
public static final int DEFAULT_HEIGHT = 400;

// Default constructor
private DancingCircles() {
    setTitle("Dancing Circles");
    setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

    // Add Circles to JFrame
    Circle myCircle = new Circle(200.0, 200.0, 20.0);
    add(myCircle);          // Add circle to frame
    Circle myCircle2 = new Circle(100.0, 100.0, 30.0);
    add(myCircle2);        // Add circle to frame
}// end DancingCircles

public static void main(String[] args) {

    EventQueue.invokeLater(new Runnable()
    {
        public void run()
        {
            DancingCircles dc = new DancingCircles();
            dc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            dc.setVisible(true);
        }
    });

 }// end main
}

Thanks!

Paul

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

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

发布评论

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

评论(3

亢潮 2024-08-24 06:51:10

执行此操作的方法是从圆类中删除绘图方法,并创建一个包含多个圆的单个面板:

import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;
import java.util.*;

public class Circles extends JPanel
{
    ArrayList<Circle> circles = new ArrayList<Circle>();

    public void add(Circle circle) {
        circles.add(circle);
    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;

        for (Circle circle: circles) {
            Ellipse2D circle2D = new Ellipse2D.Double();
            circle2D.setFrameFromCenter(
                circle.getCenterX(),
                circle.getCenterY(),
                circle.getCenterX() + circle.getRadius(),
                circle.getCenterY() + circle.getRadius());
            g2.draw(circle2D);
        }
    }
}

The way to do this is to remove the drawing methods from the circle class and create a single panel with multiple circles on it instead:

import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;
import java.util.*;

public class Circles extends JPanel
{
    ArrayList<Circle> circles = new ArrayList<Circle>();

    public void add(Circle circle) {
        circles.add(circle);
    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;

        for (Circle circle: circles) {
            Ellipse2D circle2D = new Ellipse2D.Double();
            circle2D.setFrameFromCenter(
                circle.getCenterX(),
                circle.getCenterY(),
                circle.getCenterX() + circle.getRadius(),
                circle.getCenterY() + circle.getRadius());
            g2.draw(circle2D);
        }
    }
}
隐诗 2024-08-24 06:51:10

您将每个 Circle 实现为 JPanel。使用 BorderLayout 的默认 LayoutManager,当使用 add(circle) 调用时,JFrame 一次只能容纳一个 Component。添加第二个圆时,第一个圆将被删除。

为了解决这个问题,您可以实现一个绘制多个圆圈的 MultiCircle 类,并且只将其添加到 JFrame 一次。

You are implementing each Circle as a JPanel. With the default LayoutManager of BorderLayout, the JFrame can only hold a single Component at a time when called with add(circle). When you add the second Circle, the first is removed.

To solve this, you can implement a MultiCircle class that draws multiple circles, and only add that to the JFrame once.

铁轨上的流浪者 2024-08-24 06:51:10

我不确定您是否真的希望您的 Circles 成为 JPanel。它们确实应该看起来更像

class Circle {
    double x,y,radius;
    void draw(Graphics g) {
        g.fillOval(...//etc/.
    }
}

然后有一个包含一堆圆圈的 JComponent,并将其放入您的 JFrame 中。

话虽如此,如果您必须按照自己的方式进行操作,那么您应该将 JFrame contentPane 的布局设置为 null,并确保您的 Circles 不是不透明的。您还必须手动调整每个圆的大小以适合其容器。

I'm not sure you really want your Circles to be JPanels. They really ought to look more like

class Circle {
    double x,y,radius;
    void draw(Graphics g) {
        g.fillOval(...//etc/.
    }
}

Then have a JComponent that has a bunch of Circles, and put that in your JFrame.

Having said that, if you must do it the way you've got it, then you should set the JFrame contentPane's layout to null, and make sure your Circles are not opaque. You'll also have to manually resize each Circle to fit its container.

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