How to create an Image from Graph?


Can somebody please give me some advice about how to save an image (PNG) from my created graph?

This is the java program:

public class GraphingData extends JPanel { int[] data = { 110, 535, 0, 459, 380, 199, 212, 722, 332, 836, 149, 10, 656, 465, 100, 173, 277, 381, 685, 988, 89, 585, 381, 779, 378, 769, 265, 10 }; final int PAD = 20; protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); int w = getWidth(); int h = getHeight(); ....... } public static void main(String[] args) { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(new GraphingData()); f.setSize(1000,500); f.setLocation(200,200); f.setVisible(true); } }


To store a panel how it currently shown on screen (sizes etc.) to a file just do

BufferedImage bi = new BufferedImage(panel.getSize().width, panel.getSize().height, BufferedImage.TYPE_INT_RGB); panel.paint(bi.createGraphics()); ImageIO.write(bi, "PNG", new File("path/to/file.png"));

Besides that, do not use the Graphics that is given as parameter to paintComponent directly but call g.create() on it and if you are finished with that new instance call dispose() on it. You are expected to not change the Graphics object that is put in regarding colors, transformations and so on and with the create() and dispose() you make sure this is the case.


  • Why doesn't the Graphics' draw method respect stroke attributes?
  • Undo method for a paint program [duplicate]
  • Error 2061 - Class Becomes “Undefined” When I Include A Header
  • How to change image with the click of a button in java
  • CardLayout() in java GUI
  • how to increase opacity in gaussian blur
  • Java Swing drawing PlayButton for a basic music player
  • Pie chart using graphics 2d having trouble using arrays [duplicate]
  • How do I make a JFrame a certain size, not including the border?
  • fixing JScrollPane with JLabel
  • Java - get Graphics
  • Storing “path” from a 2D array grid into a list
  • Is there a method that generates a timer in a JFrame?
  • Use a timer when a key is pressed
  • increase height of a jlabel in positive y direction?
  • JPanel repaint from another class
  • Custom Swing look and feel vs. custom components?
  • java.lang.IllegalStateException: Not on the main thread Google Maps
  • Scrolling JComponent in JScrollPane
  • Can you add a mouse listener to an object in paintComponent
  • panel with image background and mouse draw
  • NullPointerException at javax.swing.plaf.synth.SynthContext.getPainter
  • Overlapping components in JPanel
  • How to use a Java GUI panel?
  • Get exact match of the word using Regex in C#
  • jPanel is not refreshing until I resize the app window
  • Disk gets full during file write. How can I get notified about this?
  • Painting Graphics2D in a JFrame
  • query large table
  • How to overlap java graphics and an Image and make it look nice?
  • Better way to make a thread sleep
  • Java - Change the color of some squares created with Graphics2D
  • Game (with KeyListener) does not respond to user input?
  • Excel comparison with multiple IFs
  • EXCEL Multiple Ranges - need different answers for each range
  • Mathematica: FindRoot for common tangent
  • Powershell Decimal Format without comma
  • Repainting JPanel after button click
  • App Memory Usage differs between Devices
  • calculate gradient output for Theta update rule