在透明Graphics2D上用线绘制形状以获取png图像

在透明Graphics2D上用线绘制形状以获取png图像

本文介绍了在透明Graphics2D上用线绘制形状以获取png图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的目标是要打开一个窗口,您只需单击即可在白色背景上绘制一些线条.问题在于,当尝试保存时,它总是以png的形式返回,但以正方形图像的形式出现.如果我用线条画一个三角形,我会在一个白色正方形内得到一个三角形,但我只想要该三角形.我真的很感谢您的帮助

So my goal is to have a window that opens where you can draw some lines on a white background by just clicking. The problem is that when it try to save it always comes back as a png, but it comes as a square image. If I draw a triangle with my lines I get a triangle inside a white square but I want the triangle only. I would really appreciate any help

我尝试了我在stackoverflow上遇到的所有解决方案,并尝试深入了解Graphics2D,但不幸的是失败了

I tried every solution I came accross on stackoverflow and I tried to understand Graphics2D in depth but sadly failed

    public class Draw{
    public static void main(String[] args){
        Icon iconB = new ImageIcon("blue.gif");
        Icon iconM = new ImageIcon("magenta.gif");
        Icon iconR = new ImageIcon("red.gif");
        Icon iconBl = new ImageIcon("black.gif");
        Icon iconG = new ImageIcon("green.gif");

        JFrame frame = new JFrame("Paint It");
        //Creates a frame with a title of "Paint it"

        Container content = frame.getContentPane();
        //Creates a new container
        content.setLayout(new BorderLayout());
        //sets the layout

        final PadDraw drawPad = new PadDraw();
        //creates a new padDraw, which is pretty much the paint program

        content.add(drawPad, BorderLayout.CENTER);
        //sets the padDraw in the center

        JPanel panel = new JPanel();
        panel.setOpaque(false);
        //creates a JPanel
        panel.setPreferredSize(new Dimension(32, 68));
        panel.setMinimumSize(new Dimension(32, 68));
        panel.setMaximumSize(new Dimension(32, 68));
        //This sets the size of the

        content.add(panel, BorderLayout.SOUTH);
        //sets the panel to the left

        frame.setSize(480, 360);
        //sets the size of the frame
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //makes it so you can close
        frame.setVisible(true);
        //makes it so you can see it
    }
}

class PadDraw extends JComponent{
Image image;
//this is gonna be your image that you draw on
Graphics2D graphics2D;
//this is what we'll be using to draw on
int currentX, currentY, oldX, oldY;
//these are gonna hold our mouse coordinates
int firstX;
int firstY;

//Now for the constructors
//will draw from tail to head
public PadDraw(){
    setDoubleBuffered(false);
    addMouseListener(new MouseAdapter(){
        public void mousePressed(MouseEvent e){
            if (currentX == 0 && currentY == 0) {
                firstX= e.getX();
                firstY = e.getY();
                oldX = e.getX();
                oldY = e.getY();
            }
            currentX = e.getX();
            currentY = e.getY();
            graphics2D.drawLine(oldX, oldY, currentX, currentY);
            repaint();
            oldX = currentX;
            oldY = currentY;
        }
    });
}

public void paintComponent(Graphics g){
    if(image == null){
        image = createImage(getSize().width, getSize().height);
        graphics2D = (Graphics2D) image.getGraphics();
        graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        graphics2D.setComposite(AlphaComposite.Clear);
        graphics2D.fillRect(0, 0, getSize().width, getSize().height);
        graphics2D.setComposite(AlphaComposite.Src);
        clear();
    }
    g.drawImage(image, 0, 0, null);
}
//this is the painting bit
//if it has nothing on it then
//it creates an image the size of the window
//sets the value of Graphics as the image
//sets the rendering
//runs the clear() method
//then it draws the image

public void clear(){
    currentX = 0;
    currentY = 0;

    graphics2D.setPaint(Color.white);
    graphics2D.fillRect(0, 0, getSize().width, getSize().height);
    graphics2D.setPaint(Color.black);
    repaint();
}
//this is the clear
//it sets the colors as white
//then it fills the window with white
//thin it sets the color back to black

public void save(){
    repaint();
    if (currentX != 0 && currentY != 0) {
        graphics2D.drawLine(oldX, oldY, firstX, firstY);
        currentX = 0;
        currentY = 0;
    }

    try {
        BufferedImage bfrdImage = new BufferedImage
    (image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
        // Draw the image on to the buffered image
        Graphics2D bGr = bfrdImage.createGraphics();

        bGr.setComposite(AlphaComposite.Clear);
        bGr.fillRect(0, 0, getSize().width, getSize().height);
        bGr.setComposite(AlphaComposite.Src);
        bGr.drawImage(image, 0, 0, null);

        javax.imageio.ImageIO.write(bfrdImage, "PNG", new File("Drawing.PNG"));
        bGr.dispose();
    } catch (Exception ex) {
        Logger.getLogger(PadDraw.class.getName()).log(Level.SEVERE, null, ex);
    }
}
//saves and also comes back to the first point to finalize the shape}

推荐答案

这里是一个示例.

    public class PaintDemo extends JPanel {

       JFrame frame = new JFrame();

       public static void main(String[] args) {
          SwingUtilities.invokeLater(() -> new PaintDemo());
       }

       public PaintDemo() {
          setPreferredSize(new Dimension(500, 500));
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.add(this);
          frame.pack();
          frame.setLocationRelativeTo(null);
          setBackground(Color.white);
          frame.setVisible(true);
       }

       public void paintComponent(Graphics g) {
          super.paintComponent(g);
          Graphics2D g2d = (Graphics2D) g.create();
          g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
          g2d.setColor(Color.blue);
          g2d.fillOval(200, 200, 100, 100);

          // triangle
          g2d.setColor(Color.red);
          g2d.drawLine(100, 200, 200, 100);
          g2d.drawLine(200, 100, 300, 400);
          g2d.drawLine(300, 400, 100, 200);

          g2d.setColor(Color.magenta);
          g2d.fillRect(100, 100, 200, 50);
       }
    }

这篇关于在透明Graphics2D上用线绘制形状以获取png图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 22:31