使用 Java2D 绘制多个圆
我正在尝试使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
执行此操作的方法是从圆类中删除绘图方法,并创建一个包含多个圆的单个面板:
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 实现为 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.
我不确定您是否真的希望您的 Circles 成为 JPanel。它们确实应该看起来更像
然后有一个包含一堆圆圈的 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
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.