Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: Problem with saving image to a file

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem with saving image to a file

    This code my code, but it does not save the image just blank page. PLEASE HELP


    import java.awt.Graphics2D;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import javax.imageio.ImageIO;
    import javax.swing.JFrame;




    class SaveButtonAction implements ActionListener {


    DataContainer dcRef = null;
    ActionContainer ac = null;
    DrawingCanvas drawingPanel = null;
    String cmd = null;
    JFrame f = new JFrame();


    SaveButtonAction(DataContainer dc, ActionContainer ac, DrawingCanvas drawingPanel) {

    dcRef = dc;
    this.ac = ac;
    this.drawingPanel = drawingPanel;

    }


    public void actionPerformed(ActionEvent e) {
    System.out.println("[savebutton] pushed");

    //sets up the action similar to plot
    drawingPanel.plotPie(cmd); // plot pie charts


    //set up graphics 2d

    try {

    BufferedImage image =
    new BufferedImage( 120, 120, BufferedImage.TYPE_INT_RGB);
    Graphics2D graphics2D = image.createGraphics();
    f.paint( graphics2D );
    graphics2D.dispose();
    ImageIO.write(image, "png", new File("AA.png"));
    } catch (Exception ex) {
    ex.printStackTrace();
    }

    //call the drawpanel.repaint();

    drawingPanel.repaint(); // draw
    }

    }

    public class SaveButton extends GButton {

    SaveButton(String name, DataContainer dcRef, ActionContainer ac, DrawingCanvas drawingPanel) {
    super(name, dcRef);

    SaveButtonAction action = new SaveButtonAction(dcRef, ac, drawingPanel);
    addActionListener(action);
    }

    }
    Last edited by Wise girl; June 14th, 2012 at 01:41 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Problem with saving image to a file

    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting.

    The code works for me when I put it in a simple GUI program.
    Last edited by Norm; June 14th, 2012 at 09:33 AM.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jun 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with saving image to a file

    Yes, but this code does not work maybe it requires JPanel.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Problem with saving image to a file

    If you want help with the code, Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jun 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with saving image to a file

    < import java.awt.Graphics2D;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import javax.imageio.ImageIO;
    import javax.swing.JFrame;
     
     
     
     
    class SaveButtonAction implements ActionListener {
     
     
    DataContainer dcRef = null;
    ActionContainer ac = null;
    DrawingCanvas drawingPanel = null;
    String cmd = null;
    JFrame f = new JFrame();
     
     
    SaveButtonAction(DataContainer dc, ActionContainer ac, DrawingCanvas drawingPanel) {
     
    dcRef = dc;
    this.ac = ac;
    this.drawingPanel = drawingPanel;
     
    }
     
     
    public void actionPerformed(ActionEvent e) {
    System.out.println("[savebutton] pushed"); 
     
    //sets up the action similar to plot
    drawingPanel.plotPie(cmd); // plot pie charts
     
     
    //set up graphics 2d
     
    try {
     
    BufferedImage image = 
    new BufferedImage( 120, 120, BufferedImage.TYPE_INT_RGB);
    Graphics2D graphics2D = image.createGraphics();
    f.paint( graphics2D );
    graphics2D.dispose();
    ImageIO.write(image, "png", new File("AA.png"));
    } catch (Exception ex) {
    ex.printStackTrace();
    }
     
    //call the drawpanel.repaint();
     
    drawingPanel.repaint(); // draw
    }
     
    }
     
    public class SaveButton extends GButton {
     
    SaveButton(String name, DataContainer dcRef, ActionContainer ac, DrawingCanvas drawingPanel) {
    super(name, dcRef);
     
    SaveButtonAction action = new SaveButtonAction(dcRef, ac, drawingPanel);
    addActionListener(action);
    }
     
    } >

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Problem with saving image to a file

    Why do all the statements begin in first column? The code needs to be properly formatted with indentations. The posted code is not more readable in code tags because it is not indented properly.

    Please copy the code from your editor with its indentations and post it with the indentations.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jun 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with saving image to a file

    <
    import java.awt.Graphics2D;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import javax.imageio.ImageIO;
    import javax.swing.JFrame;
     
     
     
     
    class SaveButtonAction implements ActionListener {
     
     
    	DataContainer dcRef = null;
    	ActionContainer ac = null;
    	DrawingCanvas drawingPanel = null;
    	String cmd = null;
    	JFrame f = new JFrame();
     
     
    	SaveButtonAction(DataContainer dc, ActionContainer ac, DrawingCanvas drawingPanel) {
     
    		dcRef = dc;
    		this.ac = ac;
    		this.drawingPanel = drawingPanel;
     
    	}
     
     
    	public void actionPerformed(ActionEvent e) {
    		System.out.println("[savebutton] pushed"); 
    		//sets up the action  similar to plot
    	    drawingPanel.plotPie(cmd);
     
     
    		//set up graphics 2d
     
    		try {
     
    			BufferedImage image = 
    			new BufferedImage( 120, 120, BufferedImage.TYPE_INT_RGB);
    			Graphics2D graphics2D = image.createGraphics();
    			f.paint( graphics2D );
    			graphics2D.dispose();
    			ImageIO.write(image, "gif", new File("AA.gif"));
    			} catch (Exception ex) {
    			ex.printStackTrace();
    			}
     
    	   //call the drawpanel.repaint();
     
    		drawingPanel.repaint();
    			}
     
    	}
     
    public class SaveButton extends GButton {
     
    	    SaveButton(String name, DataContainer dcRef, ActionContainer ac, DrawingCanvas drawingPanel) {
    		super(name, dcRef);
     
    		SaveButtonAction action = new SaveButtonAction(dcRef, ac, drawingPanel);
    		addActionListener(action);
    	}
     
    }
    >

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Problem with saving image to a file

    Quote Originally Posted by Wise girl View Post
    <
    import java.awt.Graphics2D;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import javax.imageio.ImageIO;
    import javax.swing.JFrame;
     
     
     
     
    class SaveButtonAction implements ActionListener {
     
     
    	DataContainer dcRef = null;
    	ActionContainer ac = null;
    	DrawingCanvas drawingPanel = null;
    	String cmd = null;
    	JFrame f = new JFrame();
     
     
    	SaveButtonAction(DataContainer dc, ActionContainer ac, DrawingCanvas drawingPanel) {
     
    		dcRef = dc;
    		this.ac = ac;
    		this.drawingPanel = drawingPanel;
     
    	}
     
     
    	public void actionPerformed(ActionEvent e) {
    		System.out.println("[savebutton] pushed"); 
    		//sets up the action  similar to plot
    	    drawingPanel.plotPie(cmd);
     
     
    		//set up graphics 2d
     
    		try {
     
    			BufferedImage image = 
    			new BufferedImage( 120, 120, BufferedImage.TYPE_INT_RGB);
    			Graphics2D graphics2D = image.createGraphics();
    			f.paint( graphics2D );
    			graphics2D.dispose();
    			ImageIO.write(image, "gif", new File("AA.gif"));
    			} catch (Exception ex) {
    			ex.printStackTrace();
    			}
     
    	   //call the drawpanel.repaint();
     
    		drawingPanel.repaint();
    			}
     
    	}
     
    public class SaveButton extends GButton {
     
    	    SaveButton(String name, DataContainer dcRef, ActionContainer ac, DrawingCanvas drawingPanel) {
    		super(name, dcRef);
     
    		SaveButtonAction action = new SaveButtonAction(dcRef, ac, drawingPanel);
    		addActionListener(action);
    	}
     
    }
    >

    How do you test this code? It does not have a main() method.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Jun 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with saving image to a file

    The main method is in another class (GUIApp class)

    <import java.awt.BorderLayout;
     
     
    import java.awt.Color;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.BorderFactory;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
     
     
     
    class EntryAction implements ActionListener{
     
    	JTextField tRef = null;
    	public EntryAction(JTextField tRef ){
     
    	this.tRef= tRef;
    	}
     
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		System.out.println("key event action:"+ tRef.getText());
    		tRef.setText( " ");
    		//push in stack
    		//String cmd1 = set dataname 1 "Apple";
    	}
     
     
     
    }
     
    public class GuiApp extends JFrame {
     
    	DataContainer dc = null;
    	ActionContainer ac = null;
     
     
     
     
    	GuiApp() {
    		super();
     
    		dc = new DataContainer();
    		ac = new ActionContainer();
     
     
    		DrawingCanvas drawingPanel = new DrawingCanvas( 600, 500, ac, dc);
    		drawingPanel.setBackground(Color.white);
     
    		JPanel controlPanel = new JPanel();
    		JPanel controlPanel1 = new JPanel();
     
    		controlPanel.setLayout( new GridLayout(4, 1, 2, 2));
    		controlPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
     
    		LoadButton loadButton = new LoadButton("load", dc);
    		PlotButton plotButton = new PlotButton("plot!", dc, ac, drawingPanel);
    		SaveButton saveButton = new SaveButton("save",dc, ac, drawingPanel);
     
    		setLayout( new BorderLayout (5, 10) );
    		JTextField entryField = new JTextField(55);	
    		controlPanel1.add(entryField );
    		entryField.addActionListener(new EntryAction(entryField));
     
     
    		controlPanel.add( loadButton );
    		controlPanel.add( plotButton );
    		controlPanel.add(saveButton );
     
     
    		add( drawingPanel, BorderLayout.CENTER );
    		add( controlPanel, BorderLayout.EAST );
    		add( controlPanel1, BorderLayout.SOUTH);
     
    		setTitle("GUI-App");
    		setSize(700,500);
    		setLocationRelativeTo(null);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setVisible(true);
     
    	}
     
    	public static void main(String[] args) {
    		new GuiApp();
    	}
    }>

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Problem with saving image to a file

    The code you posted does not compile without many errors.
    Can you make a small simpler program that compiles, executes and shows the problem?

    There are too many parts to the code you are posting to work with.

    This code from your first post worked for me:


    try {
     BufferedImage image =  new BufferedImage( 120, 120, BufferedImage.TYPE_INT_RGB); 
     Graphics2D graphics2D = image.createGraphics();
    f.paint( graphics2D );
     graphics2D.dispose();
     ImageIO.write(image, "png", new File("AA.png"));
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    Last edited by Norm; June 14th, 2012 at 03:45 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Jun 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with saving image to a file

    So, can I send my application as a jar file to your email?

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Problem with saving image to a file

    No, for testing, I would like a small, simple program that compiles, executes and shows the problem.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Jun 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with saving image to a file

    Thank you a lot. It works now

    The problem was

    <f.paint( graphics2D );
    The correct is

    <drawingPanel.paint( graphics2D );>

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Problem with saving image to a file

    Glad you got it working.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Getting pixels from an image, and saving them in an array?
    By jtvd78 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: April 14th, 2011, 08:48 PM
  2. Problem in reading image file
    By ramhanuman in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 3rd, 2011, 02:46 PM
  3. saving xml file
    By LOL in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 18th, 2010, 05:45 AM
  4. Saving an image file (PGM format)
    By Mickey2315 in forum Algorithms & Recursion
    Replies: 3
    Last Post: September 12th, 2009, 01:18 AM
  5. [SOLVED] Saving A File?
    By MysticDeath in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: August 1st, 2009, 11:56 AM