Drawing multiple circles with Java2D

大憨熊 提交于 2020-01-04 06:07:49

问题


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


回答1:


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



回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/2023236/drawing-multiple-circles-with-java2d

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!