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 4 of 4

Thread: Need help to save Canvas content to a file

  1. #1
    Member
    Join Date
    Jul 2011
    Posts
    53
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Need help to save Canvas content to a file

    I have worked for a while to make a paint-like program. The area that i use to draw is a Canvas. Now, i want to save whatever i draw on it ...to a file on my harddisk. I have tried like this:

    public class SaveJPEG extends Thread
    {
    	SuprafataDesenare plansa;     // this is the class which extends Canvas
    	String url;                 // this is a string which is the path to the file
    	FereastraPrincipala fp;     // this is the main Frame of my application
     
    	public SaveJPEG(SuprafataDesenare plansa, String url,FereastraPrincipala fp)
    	{
    		this.plansa = plansa;
    		this.url = url;
    		this.fp = fp;
    	}
     
    	public void run()
    	{
     
    		try
    		{
     
    			BufferedImage image = new BufferedImage(plansa.getWidth(), plansa.getHeight(), BufferedImage.TYPE_INT_RGB);
    			BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(url)) ;
     
    			Graphics2D graphics = image.createGraphics();
    			plansa.paintAll(graphics);
    			ImageIO.write(image, "jpg", fos);
     
    			fos.close();
     
    		}
    		catch(Exception e)
    		{
    			e.printStackTrace();
    		}
     
     
     
    	}
     
     
    }


    But it looks like it does not work . I have seen a code like this on a site, and there was said that it should save the content of my canvas to a .jpg file. Sadly, it creates a blank .jpg file. Is there something that i have to change at my code to make it work? Is there any other way that i can save the content of my paint ?


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,607
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: Need help to save Canvas content to a file

    What exactly do you mean when you say that it does not work? Do you get an exception? Some kind of strange behavior? What exactly happens versus what you expect to happen? If you posted an SSCCE that we could copy, paste, and run, it would be easier to help you out.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Jul 2011
    Posts
    53
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Need help to save Canvas content to a file

    I made a smaller program to make it easier to put here:
    Here is a class which extends Canvas. Inside the method paint i draw a String. And, i add mouselisteners to make it possible to draw on the Canvas...using the mouse...just as with the pencil tool of the paint:

    import java.awt.Canvas;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
     
    public class SuprafataDesenare extends Canvas implements MouseListener, MouseMotionListener
     
    {
    	int x1;
    	int y1;
    	int x2;
    	int y2;
    	Dimension dim = new Dimension (300,300);
     
    	SuprafataDesenare()
    	{
    		this.setBackground(Color.white);
    		this.setSize(dim);
    		this.addMouseListener(this);
    		this.addMouseMotionListener(this);
    	}
    	public void paint(Graphics g)
     
    	{
    		g.setColor(Color.black);
    		g.setFont(new Font("Arial", Font.PLAIN, 60));
    		g.drawString("HELLO!!!", 50, 50);
     
    	}
     
    	public void mouseClicked(MouseEvent arg0) {}
    	public void mouseEntered(MouseEvent arg0) {}
    	public void mouseExited(MouseEvent arg0) {}
     
    	public void mousePressed(MouseEvent e)
    	{
    		x1 = e.getX();
    		y1 = e.getY();
     
    	}
     
    	public void mouseReleased(MouseEvent e){}
     
    	public void mouseDragged(MouseEvent e)
    	{
    		x2 = e.getX();
    		y2 = e.getY();
     
    		Graphics g = this.getGraphics();
    		g.setColor(Color.black);
    		g.drawLine(x1, y1, x2, y2);
    		x1=x2;
    		y1=y2;
    		g.dispose();
     
    	}
     
    	public void mouseMoved(MouseEvent arg0) {}
     
     
    }

    Here i have the Frame, on which i place the Canvas and i also add a menu with a menuitem to save the content of the file to a jpg file:

    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
     
    public class FereastraPrincipala extends JFrame implements ActionListener
    {
    	SuprafataDesenare desen = new SuprafataDesenare();
     
    	public FereastraPrincipala(String titlu)
     
    	{
    		super(titlu);
    		this.setLayout(new BorderLayout());
    		this.add(desen, BorderLayout.CENTER);
    		JMenuBar menuBar = new JMenuBar();
    		menuBar.setBounds(0, 0, 530, 21);
    		this.getContentPane().add(menuBar);
    		JMenu mnFile = new JMenu("File");
    		menuBar.add(mnFile);
    		JMenuItem mntmSave = new JMenuItem("Save");
    		mnFile.add(mntmSave);
     
    		mntmSave.addActionListener(this);
     
    		this.pack();
     
    	}
     
     
    	public void actionPerformed(ActionEvent e)
    	{
    		new SaveJPEG(this.desen, "C:\\Users\\nikipiulitza\\Desktop\\poze\\paint.jpg", this).start();
     
    	}
     
    }

    And here i have the Thread which saves the file:

     
    import java.awt.Canvas;
    import java.awt.Graphics2D;
    import java.awt.image.BufferedImage;
    import java.io.BufferedOutputStream;
    import java.io.FileOutputStream;
     
    import javax.imageio.ImageIO;
     
     
     
    public class SaveJPEG extends Thread
    {
    	SuprafataDesenare plansa;     // this is the class which extends Canvas
    	String url;                 // this is a string which is the path to the file
    	FereastraPrincipala fp;     // this is the main Frame of my application
     
    	public SaveJPEG(SuprafataDesenare plansa, String url,FereastraPrincipala fp)
    	{
    		this.plansa = plansa;
    		this.url = url;
    		this.fp = fp;
    	}
     
    	public void run()
    	{
     
    		try
    		{
     
    			BufferedImage image = new BufferedImage(plansa.getWidth(), plansa.getHeight(), BufferedImage.TYPE_INT_RGB);
    			BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(url)) ;
     
    			Graphics2D graphics = image.createGraphics();
    			plansa.paintAll(graphics);
    			image.getGraphics();
    			ImageIO.write(image, "jpg", fos);
     
    			fos.close();
     
    		}
    		catch(Exception e)
    		{
    			e.printStackTrace();
    		}
     
     
     
    	}
     
     
    }

    I also have a class with the main method:

    public class MainClass 
    {
    	public static void main(String[] args)
    	{
    		FereastraPrincipala fp = new FereastraPrincipala("String");
    		fp.show();
     
     
    	}
     
    }

    My problem is the following:
    When i save the drawing to a file, i get a .jpg file on which i have drawn the String (the one that is drawn inside the paint() method of the Canvas class). But, if i draw a line using the mouse (using the "pencil" too), i only get the String saved to the file, and not what is drawn after. But i want my program to save all i draw ! What am i doing wrong ?

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,607
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: Need help to save Canvas content to a file

    Ah, I see. Don't call getGraphics() on a Component like that. Instead, store everything you need to draw in a Collection, and write a method that iterates through the Collection and draws each item. Then you can call that method from your paintComponent() method or from your save() method.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. trying to save data onto a file
    By DanTheSand in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: December 13th, 2011, 09:58 AM
  2. Canvas in JFrame, does not show content
    By Nesh108 in forum Java Theory & Questions
    Replies: 6
    Last Post: October 31st, 2011, 09:58 PM
  3. Content in text file to MS Word file help!
    By ComputerSaysNo in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: October 27th, 2011, 12:39 PM
  4. Save form as .pdf file?
    By Taron in forum AWT / Java Swing
    Replies: 3
    Last Post: November 14th, 2010, 02:20 PM
  5. Saving .jsp page as .pdf file while generating report for struts based web application
    By ravindra_kumar_tiwari in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: August 12th, 2008, 10:32 AM