public class Window{
private JFrame frame;
private JPanel panel = new JPanel();
Circle c = new Circle(frame, panel);
// some other buttons
.
.
// some code to set the panel grid bag constaraints and background then
frame.getContentPane().add(panel, gbc_panel);
}
然后是Circle类
public class Circle extends Shape implements ActionListener{
private JPanel Panel;
private GridBagConstraints gbc_btnCircle;
private JButton btnCircle;
public void setPanel(JPanel panel) {
Panel = panel;
}
public Circle(JFrame frame, JPanel panel){
btnCircle = new JButton("Circle");
// some code to set grid bag constraint then
frame.getContentPane().add(btnCircle, gbc_btnCircle);
setPanel(panel);
btnCircle.addActionListener(this);
}
public void paint(Graphics g) {
super.paintComponents(g);
g.setColor(Color.red);
g.fillOval(100, 100, 100, 100);
Panel.add(this);
}
public void actionPerformed(ActionEvent arg0) {
repaint();
}
}
#1 楼
您有点错误的主意。在绘图面板中,应该有一个List<Circle>
。在绘图面板的paintComponent
方法中,您应该遍历列表以绘制每个圆class Circle {
int x, int y, int width, int height;
public Circle (int x, int y, int width, int height) {
... set em
}
public void draw(Graphics g) {
g.fillOval(x, y, width, height);
}
}
class DrawingPanel extends JPanel {
List<Circle> circles = new ArrayList<>();
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for (Circle circle : circles) {
circle.draw(g);
}
}
// Dont forget to override public Dimension getPreferredSize()
}
要在列表中添加更多圆,只需使用
addCircle
方法在DrawingPanel
类中public void addCircle(Circle circle) {
circles.add(circle);
repaint();
}
就按钮而言,您应该在
Window
类中创建它。在ActionListener
中,只需创建new Circle
并通过调用DrawingPanel
方法将其添加到addCircle
中。另外,Circle
不需要扩展Shape
。 Shape
API已经具有Ellipse2D
类,您可以从以下类别创建圆:class DrawingPanel extends JPanel {
List<Ellipse2D> circles = new ArrayList<>();
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g.create();
for (Ellipse2D circle : circles) {
g2.fill(circle);
}
g2.dispose();
}
// Dont forget to override public Dimension getPreferredSize()
}
请参见2D图形
更新:完整示例
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class CirclesDemo {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
new CirclesDemo();
}
});
}
public CirclesDemo() {
JFrame frame = new JFrame();
frame.add(panel);
frame.add(createButton(), BorderLayout.PAGE_END);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private final DrawingPanel panel = new DrawingPanel();
private JButton createButton() {
JButton button = new JButton("Add Circle");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
int[] circleValues = generateRandomValues(300, 300, 50, 150);
int x = circleValues[0];
int y = circleValues[1];
int width = circleValues[2];
int height = width;
Circle circle = new Circle(x, y, width, height);
panel.addCircle(circle);
}
});
return button;
}
private int[] generateRandomValues(int maxX, int maxY,
int minSize, int maxSize) {
Random random = new Random();
int[] values = new int[3];
values[0] = random.nextInt(maxX);
values[1] = random.nextInt(maxY);
values[2] = Math.min(random.nextInt(maxSize) + minSize, maxSize);
return values;
}
class Circle {
int x, y, width, height;
public Circle(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public void draw(Graphics g) {
g.drawOval(x, y, width, height);
}
}
class DrawingPanel extends JPanel {
List<Circle> circles = new ArrayList<>();
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for (Circle circle : circles) {
circle.draw(g);
}
}
public void addCircle(Circle circle) {
circles.add(circle);
repaint();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
}
}
#2 楼
您将覆盖Circle的绘制方法。您需要重写面板的绘制方法。评论
请怎么做?
– Alaa Othman
2014年10月31日15:59
评论
您能否请看一下添加的编辑内容,直到我为什么我仍然无法使用?当我单击按钮时,面板上什么也没有。
– Alaa Othman
2014年10月31日17:22
1)我看不到按钮的创建位置和添加ActionListener的位置。我将假定完全放弃第一个Circle类2)是否重写了getPreferredSize? 3)我看不到您将DrawingPanel添加到框架。
– Paul Samsotha
2014年10月31日在17:25