就好像画图工具一样,可以先导入一张图片,然后再用鼠标在图片上作图。

解决方案 »

  1.   

    1. 导入一张图片:
    Image image = new ImageIcon("image.png").getImage();2. 创建一个BufferedImage,  然后把导入的图片先画到这个BufferedImage.
            BufferedImage bufImage = new BufferedImage(image.getWidth(null), image.getHeight(null), 
                    BufferedImage.TYPE_INT_RGB);        Graphics2D imageGraphics = bufImage.createGraphics();
            imageGraphics.drawImage(image, 0, 0, null);
            imageGraphics.close();        第一第二步可以在构造函数中完成.3. 在JPanel的paintComponent(Graphics g) 中绘制图形
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);        Graphics2D imageGraphics = bufImage.createGraphics();
            // 画图形到BufferedImage上
            imageGraphics.drawOval(0, 0, 40, 40);
            // ...
            imageGraphics.close();
            
            // 把图形显示到屏幕上
            g.drawImage(bufImage, 0, 0, this);
        }