Saving a Java 2d graphics image as .png file

后端 未结 4 1536
刺人心
刺人心 2020-11-30 06:54

I am drawing a graphical representation of information my simulation is generating. I have the graph displaying but the problem i am running into is to be able to save it a

相关标签:
4条回答
  • 2020-11-30 07:20

    See this example: Draw an Image and save to png.


    import java.awt.BasicStroke;
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.FontMetrics;
    import java.awt.GradientPaint;
    import java.awt.Graphics2D;
    import java.awt.geom.Ellipse2D;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    
    public class WriteImageType {
      static public void main(String args[]) throws Exception {
        try {
          int width = 200, height = 200;
    
          // TYPE_INT_ARGB specifies the image format: 8-bit RGBA packed
          // into integer pixels
          BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    
          Graphics2D ig2 = bi.createGraphics();
    
    
          Font font = new Font("TimesRoman", Font.BOLD, 20);
          ig2.setFont(font);
          String message = "www.java2s.com!";
          FontMetrics fontMetrics = ig2.getFontMetrics();
          int stringWidth = fontMetrics.stringWidth(message);
          int stringHeight = fontMetrics.getAscent();
          ig2.setPaint(Color.black);
          ig2.drawString(message, (width - stringWidth) / 2, height / 2 + stringHeight / 4);
    
          ImageIO.write(bi, "PNG", new File("c:\\yourImageName.PNG"));
          ImageIO.write(bi, "JPEG", new File("c:\\yourImageName.JPG"));
          ImageIO.write(bi, "gif", new File("c:\\yourImageName.GIF"));
          ImageIO.write(bi, "BMP", new File("c:\\yourImageName.BMP"));
    
        } catch (IOException ie) {
          ie.printStackTrace();
        }
    
      }
    }
    
    0 讨论(0)
  • 2020-11-30 07:23

    It appears that you never actually paint to the BufferedImage in your saveGraph(..) routine.

    After you create your BufferedImage and retrieve the Graphics object for that image, call the paintComponent method of your main class passing that graphics context. You also are create two GraphDisplay objects but never use either one.

        GraphDisplay graphImg = new GraphDisplay(p);
    
       //You don't need this one, you created one above named graphImg
       // Object graph = new GraphDisplay(p);
        BufferedImage buffGraph = new BufferedImage(500,500, BufferedImage.TYPE_INT_RGB);
    
        //get the graphics context for the BufferedImage
        Graphics2D graph = buffGraph.createGraphics();
    
        //now tell your main class to draw the image onto the BufferedImage
        graphImg.paintComponent(graph);
    

    At this point your BufferedImage should now have the same drawing that your panel had and you should be able to save the contents.

    0 讨论(0)
  • 2020-11-30 07:40
    JPanel dPanel;
    ...     
    public void save()
    {
        BufferedImage bImg = new BufferedImage(dPanel.getWidth(), dPanel.getHeight(), BufferedImage.TYPE_INT_RGB);
        Graphics2D cg = bImg.createGraphics();
        dPanel.paintAll(cg);
        try {
                if (ImageIO.write(bImg, "png", new File("./output_image.png")))
                {
                    System.out.println("-- saved");
                }
        } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
        }
    }
    
    0 讨论(0)
  • 2020-11-30 07:41

    Screen Image will create a buffered image of the panel and write the image to a file.

    0 讨论(0)
提交回复
热议问题