单击按钮即可将形状添加到JPanel

单击按钮即可将形状添加到JPanel,第1张

单击按钮即可将形状添加到JPanel

您有点错误的主意。在绘图面板中,您应该有一个

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
,只需创建一个
newCircle
,然后
DrawingPanel
通过调用
addCircle
方法将其添加


顺便说一句,

Circle
不需要extend
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);        }    }}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5561876.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-14
下一篇 2022-12-14

发表评论

登录后才能评论

评论列表(0条)

保存