Saving a Java 2d graphics image as .png file

后端 未结 4 1535
刺人心
刺人心 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: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.

提交回复
热议问题