java 使用 Java2D 绘制多个圆圈

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2023236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 18:58:47  来源:igfitidea点击:

Drawing multiple circles with Java2D

javajava-2d

提问by Paul

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.

我正在尝试使用 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
}

Thanks!

谢谢!

Paul

保罗

回答by Mark Byers

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:

这样做的方法是从 circle 类中删除绘图方法,并创建一个带有多个圆圈的面板:

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);
        }
    }
}

回答by Adam Goode

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.

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

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

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

回答by Jonathan Feinberg

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

我不确定您是否真的希望您的圈子成为 JPanels。他们真的应该看起来更像

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.

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

Having said that, if you mustdo 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.

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