我正在尝试使用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
}
谢谢!
保
答案 0 :(得分:5)
执行此操作的方法是从圆类中删除绘图方法,然后创建一个包含多个圆的单个面板:
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);
}
}
}
答案 1 :(得分:0)
您正在将每个Circle实现为JPanel。使用BorderLayout的默认LayoutManager,JFrame在使用add(circle)
调用时一次只能容纳一个Component。当您添加第二个圆时,第一个圆被删除。
要解决此问题,您可以实现绘制多个圆的MultiCircle类,并且只将其添加到JFrame一次。
答案 2 :(得分:0)
我不确定你真的希望你的圈子成为JPanels。他们真的应该看起来更像
class Circle {
double x,y,radius;
void draw(Graphics g) {
g.fillOval(...//etc/.
}
}
然后让一个JComponent有一堆Circles,并将它放在你的JFrame中。
话虽如此,如果你必须按照你的方式去做,那么你应该将JFrame contentPane的布局设置为null,并确保你的Circles不是不透明的。您还必须手动调整每个圆圈的大小以适合其容器。