您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何在Java swing应用程序中保留和删除多个图形对象?

如何在Java swing应用程序中保留和删除多个图形对象?

不要使用getGraphics,这不是自定义绘画的工作方式,有关更多详细信息,请参见“AWT中的绘画”和“摇摆执行自定义绘画

基本思想是,您需要某种List,您可以在其中添加每个形状。mouseClicked发生这种情况时,您遍历列表并检查单击的鼠标是否出现了其中一种形状,如果是,则将其从中删除List,如果没有,则在单击时创建一个新形状并添加它到List

然后,您可以ListpaintComponent方法内部使用它来物理绘制形状。

本示例扩展了您ImagePanel自定义绘画中的添加

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Shape;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new test();
    }

    public test() {
        EventQueue.invokelater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | illegalaccessexception | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new DrawPane("/Volumes/Disk02/Drop@R_417_2419@/MegaTokyo/thumnails/0.jpg"));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class ImagePanel extends JPanel {

        private Image img;

        public ImagePanel(String img, String str) {
            //this(new ImageIcon(img).getImage());    
        }

        public ImagePanel(String path) {
            Image img = new ImageIcon(path).getImage();
            this.img = img;
            try {
                BufferedImage image = ImageIO.read(new File(path));
                int rgb = image.getRGB(66, 52);
                System.out.println("Colour is: " + rgb);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @Override
        public Dimension getPreferredSize() {
            return img == null ? new Dimension(200, 200) : new Dimension(img.getWidth(this), img.getHeight(this));          
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(img, 0, 0, null);
        }
    }

    public class DrawPane extends ImagePanel {

        private List<Shape> shapes;

        public DrawPane(String img, String str) {
            super(img, str);
            init();
        }

        public DrawPane(String path) {
            super(path);
            init();
        }

        protected void init() {
            shapes = new ArrayList<>(25);
            addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    boolean clicked = false;
                    Iterator<Shape> it = shapes.iterator();
                    while (it.hasNext()) {
                        Shape shape = it.next();
                        if (shape.contains(e.getPoint())) {
                            it.remove();
                            clicked = true;
                            break;
                        }
                    }
                    if (!clicked) {
                        shapes.add(new Ellipse2D.Double(e.getX() - 10, e.getY() - 10, 20, 20));
                    }
                    repaint();
                }

            });
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g); 
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setColor(Color.RED);
            for (Shape shape : shapes) {
                g2d.draw(shape);
            }
            g2d.dispose();
        }

    }

}
java 2022/1/1 18:27:50 有352人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶