Results 1 to 11 of 11
  1. #1
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default How to save an Image Drawn with Graphics2D to a file?

    How do I save a screen image drawn with Graphics2D to a file? Some of my program is shown below

    Java Code:
        public void paintComponent(Graphics g)
        {
            Graphics2D g1 = (Graphics2D) g;
            g1.translate(400, 400);
            for (double i = 0; i <= 1.0; i = i + 0.001)
            {
                 g1.rotate(i); 
                 g1.drawImage(image, 0, 0, 200, 200, this);
            }
    My program reads the image with the following line of code:

    Java Code:
    image = Toolkit.getDefaultToolkit().getImage(getClass().getClassLoader().getResource("Tomato1.gif"));

  2. #2
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: How to save an Image Drawn with Graphics2D to a file?

    Ok, I kind of figured out how to do it. See the code below.

    Does anyone know why I get a black background in my saved image?

    Java Code:
    package rotateimage;
    
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.imageio.ImageIO;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class RotateImage extends JPanel
    {
    
        private Image TomatoImage;
        
        public RotateImage() throws IOException
        {
            super();
    
            File outputfile2 = new File("RotatedTomato.gif");
    
            TomatoImage = ImageIO.read(getClass().getClassLoader().getResource("Tomato1.gif"));
    
            BufferedImage Buf_Image = new BufferedImage(800, 800, BufferedImage.TYPE_INT_RGB);
    
            Graphics gT = Buf_Image.createGraphics();
            
            drawImage(gT);
    
            gT.dispose();
    
            try
            {
                ImageIO.write(Buf_Image, "gif", outputfile2);
            } catch (IOException ex)
            {
                Logger.getLogger(RotateImage.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    
        @Override
        public void paintComponent(Graphics g)
        {
            drawImage(g);
        }
    
        private void drawImage(Graphics g)
        {
            Graphics2D g2d = (Graphics2D) g;
    
            g2d.translate(400, 400);
    
            for (double i = 0; i <= 1.0; i = i + 0.001)
            {
                g2d.rotate(i);
                g2d.drawImage(TomatoImage, 0, 0, 200, 200, this);
            }
          
        }
    
        public static void main(String arg[]) throws IOException
        {
            JFrame frame = new JFrame("RotateImage");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(800, 800);
    
            RotateImage panel = new RotateImage();
    
            frame.setContentPane(panel);
            frame.setVisible(true);
        }
    }

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default Re: How to save an Image Drawn with Graphics2D to a file?

    Did you call the super method or do a fill of the Graphics context area first?
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: How to save an Image Drawn with Graphics2D to a file?

    What super method? How do I fill the graphics context area?

    I tried to call a different drawImage method: public abstract boolean drawImage(Image img, int x, int y, int width, int height, Color bgcolor, ImageObserver observer), which specified the background color, but the save image's background was still black. Why is that?

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,197
    Rep Power
    19

    Default Re: How to save an Image Drawn with Graphics2D to a file?

    Moved from New to Java

    Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    Don't run loops in a painting method. Only the final result of all the painting will be seen.

    If you want to anuimate a rotating image, use a javax.swing.Timer.

    Also, read the API to learn what the bgColor parameter is really used for. Guessing rarely pays off.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: How to save an Image Drawn with Graphics2D to a file?

    I noticed that the image that is being painted only takes up part of the screen. So the black portion of the BufferedImage that does not include the image.

    Do you know how I can set the color of that portion?

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default Re: How to save an Image Drawn with Graphics2D to a file?

    Use the Graphics class's fillRect method.
    or Make the image the size you want so there are no unused areas.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: How to save an Image Drawn with Graphics2D to a file?

    Quote Originally Posted by DarrylBurke View Post
    Moved from New to Java

    Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    Don't run loops in a painting method. Only the final result of all the painting will be seen.

    db
    How would I generate the final result before it is seen and then paint it in the paint method? I didn't see anything in the link about not running loops in the paint method.

  9. #9
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: How to save an Image Drawn with Graphics2D to a file?

    The following code in the drawImage method worked for the saved image, however, the area outside the image on the jpanel now has a grey color. Why is that so?

    Java Code:
        private void drawImage(Graphics g)
        {
            Graphics2D g2d = (Graphics2D) g;
     
            g2d.setBackground(Color.white);
            
            g2d.fillRect(0, 0, 800, 800);
    
            ....
         }

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default Re: How to save an Image Drawn with Graphics2D to a file?

    You can use loops in the painting method in many cases. Your problem could be that the x,y locations where you are drawing the images does not change. If you draw many images at the same location, ony the last one will be viewable. The other ones will have been drawn over. Maybe the later images are transparent and you are looking for a see through effect.


    Use the setColor() method vs setBackground()
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: How to save an Image Drawn with Graphics2D to a file?

    Perfect - calling the setColor() method works. Thanks.

Similar Threads

  1. Replies: 1
    Last Post: 03-28-2012, 04:58 PM
  2. Replies: 0
    Last Post: 12-14-2011, 02:30 PM
  3. Replies: 1
    Last Post: 11-30-2011, 04:18 PM
  4. get the min X and min Y of an image drawn on the window
    By bizzareshishir in forum New To Java
    Replies: 3
    Last Post: 01-22-2011, 01:03 PM
  5. How do erase image drawn
    By fernando in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 05:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •