User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 426,509 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,161 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 496 | Replies: 4 | Solved
Reply
Join Date: Jan 2008
Posts: 1,771
Reputation: VernonDozier is just really nice VernonDozier is just really nice VernonDozier is just really nice VernonDozier is just really nice VernonDozier is just really nice 
Rep Power: 8
Solved Threads: 218
VernonDozier VernonDozier is offline Offline
Posting Virtuoso

How do you save a JPanel as a GIF file?

  #1  
Jul 25th, 2008
I've made a small program that draws black rectangles on a JPanel at random places. I would like to give the user the option to save this JPanel as a .gif file, but I'm not sure where to start. The function I need to write is SaveJPanelAsGIF below on line 36. Can anyone point me in the right direction please? Thanks.

  1. // SaveAsGIF.java
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.io.*;
  6. import javax.swing.*;
  7.  
  8. public class SaveAsGIF extends JFrame
  9. {
  10. ButtonPanel buttonPanel;
  11. CanvasPanel canvasPanel;
  12.  
  13. public static void main(String[] args)
  14. {
  15. SaveAsGIF sag = new SaveAsGIF();
  16. }
  17.  
  18.  
  19. public SaveAsGIF()
  20. {
  21. this.setTitle("Save JPanel As GIF");
  22. this.setVisible(true);
  23. this.setSize(600, 600);
  24. this.setLayout(new BorderLayout());
  25. canvasPanel = new CanvasPanel(this);
  26. buttonPanel = new ButtonPanel(this, canvasPanel);
  27. this.add(buttonPanel, BorderLayout.NORTH);
  28. buttonPanel.setPreferredSize(new Dimension(600, 100));
  29. this.add(buttonPanel, BorderLayout.NORTH);
  30. this.add(canvasPanel, BorderLayout.SOUTH);
  31. this.pack();
  32. this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  33. }
  34.  
  35.  
  36. public void SaveJPanelAsGIF (JPanel jp)
  37. {
  38. // not sure what to do here.
  39. }
  40. }

  1. // ButtonPanel.java
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.io.*;
  6. import javax.swing.*;
  7.  
  8.  
  9. public class ButtonPanel extends JPanel implements ActionListener
  10. {
  11. JButton drawButton;
  12. JButton saveButton;
  13. CanvasPanel cp;
  14. SaveAsGIF savegif;
  15.  
  16. public ButtonPanel(SaveAsGIF se, CanvasPanel canPanel)
  17. {
  18. savegif = se;
  19. cp = canPanel;
  20. this.setBackground(Color.RED);
  21. this.setPreferredSize(new Dimension(600, 100));
  22. LayoutManager lm = new GridLayout(1, 2);
  23. drawButton = new JButton ("Draw New Rectangle");
  24. saveButton = new JButton ("Save Canvas Panel as GIF");
  25. drawButton.addActionListener(this);
  26. saveButton.addActionListener(this);
  27. this.setLayout(lm);
  28. this.add(drawButton);
  29. this.add(saveButton);
  30. }
  31.  
  32. public void actionPerformed(ActionEvent e)
  33. {
  34. Object obj = e.getSource();
  35. if (obj == drawButton)
  36. cp.NewDrawing();
  37. else if (obj == saveButton)
  38. savegif.SaveJPanelAsGIF(cp);
  39. }
  40. }

  1. // CanvasPanel.java
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.io.*;
  6. import javax.swing.*;
  7. import java.util.*;
  8.  
  9.  
  10. class CanvasPanel extends JPanel
  11. {
  12. int x;
  13. int y;
  14. int length;
  15. int width;
  16. Random random;
  17. SaveAsGIF savegif;
  18.  
  19.  
  20. public CanvasPanel(SaveAsGIF se)
  21. {
  22. savegif = se;
  23. x = 100;
  24. y = 100;
  25. length = 200;
  26. width = 100;
  27. random = new Random();
  28. this.setBackground(Color.GREEN);
  29. this.setPreferredSize(new Dimension(600, 500));
  30. }
  31.  
  32.  
  33. public void paintComponent(Graphics g)
  34. {
  35. super.paintComponent(g);
  36. g.setColor(Color.BLACK);
  37. g.fillRect(x, y, width, length);
  38. }
  39.  
  40.  
  41. public void NewDrawing()
  42. {
  43. x = random.nextInt(100) + 50;
  44. y = random.nextInt(100) + 50;
  45. width = random.nextInt(100) + 50;
  46. length = random.nextInt(100) + 150;
  47. repaint();
  48. }
  49. }
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2008
Location: WA, USA
Posts: 776
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Rep Power: 4
Solved Threads: 76
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Master Poster

Re: How do you save a JPanel as a GIF file?

  #2  
Jul 25th, 2008
I found some links that rendered Images to .gifs, which seemed useless at first but then I looked into the Image class and all of the "helper" classes for its methods.

I think I'll try to look into how to read the pixels on a screen into an array (or double array) of ints to produce a valid Image so that you can use the Java Image I/O API located here--

http://java.sun.com/j2se/1.4.2/docs/...ideTOC.fm.html

--If successful then anything like a Graphics-rendered Image, or something that yields pixels on the screen (like the acm.GraphicsProgram GObjects, etc) can be written to a valid Image extension.

There are other links I found for converting strictly "Images" to .gif, etc but it's only useful if you can produce an Image out of the drawn unit.

http://www.gif4j.com/java-gif4j-pro-...ncode-save.htm

http://www.acme.com/java/software/Ac...ifEncoder.html

Hopefully someone can find a more reasonable solution to this though. I hate re-inventing the wheel!
Reply With Quote  
Join Date: May 2007
Location: USA
Posts: 2,843
Reputation: Ezzaral is a name known to all Ezzaral is a name known to all Ezzaral is a name known to all Ezzaral is a name known to all Ezzaral is a name known to all Ezzaral is a name known to all 
Rep Power: 12
Solved Threads: 283
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Posting Maven

Re: How do you save a JPanel as a GIF file?

  #3  
Jul 25th, 2008
Here is the code I used to dump a "colorscan" display of some sensor data to an image file so I could fiddle with it in an external image processing package. Our component was using an offscreen Image buffer to draw on and then rendering with g.drawImage() in the paintComponent method, so I already had an Image of the panel, but I fiddled with the code just a bit and the following should work for you if you don't need to save the frame decorations (title bar, etc), just the contents .
The action listener code to save:
  1. public void actionPerformed(java.awt.event.ActionEvent e){
  2. try {
  3. File saveFile = new File(jobFile+"_colorscan@"+centerSampleIndex+".gif");
  4. Image img = createImage(getWidth(),getHeight());
  5. Graphics g = img.getGraphics();
  6. paint(g);
  7. ImageIO.write(toBufferedImage(img),"gif",saveFile);
  8. JOptionPane.showMessageDialog(null,"Image saved to "+saveFile.toString());
  9. g.dispose();
  10. } catch (Exception ex){
  11. ex.printStackTrace();
  12. }
  13. }
The method to convert the Image to a BufferedImage (which I found in some example somewhere):
  1. // This method returns a buffered image with the contents of an image
  2. private BufferedImage toBufferedImage(Image image) {
  3. if (image instanceof BufferedImage) {
  4. return (BufferedImage)image;
  5. }
  6.  
  7. // This code ensures that all the pixels in the image are loaded
  8. image = new ImageIcon(image).getImage();
  9.  
  10. // Determine if the image has transparent pixels; for this method's
  11. // implementation, see e661 Determining If an Image Has Transparent Pixels
  12. boolean hasAlpha = hasAlpha(image);
  13.  
  14. // Create a buffered image with a format that's compatible with the screen
  15. BufferedImage bimage = null;
  16. GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
  17. try {
  18. // Determine the type of transparency of the new buffered image
  19. int transparency = Transparency.OPAQUE;
  20. if (hasAlpha) {
  21. transparency = Transparency.BITMASK;
  22. }
  23.  
  24. // Create the buffered image
  25. GraphicsDevice gs = ge.getDefaultScreenDevice();
  26. GraphicsConfiguration gc = gs.getDefaultConfiguration();
  27. bimage = gc.createCompatibleImage(
  28. image.getWidth(null), image.getHeight(null), transparency);
  29. } catch (HeadlessException e) {
  30. // The system does not have a screen
  31. }
  32.  
  33. if (bimage == null) {
  34. // Create a buffered image using the default color model
  35. int type = BufferedImage.TYPE_INT_RGB;
  36. if (hasAlpha) {
  37. type = BufferedImage.TYPE_INT_ARGB;
  38. }
  39. bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
  40. }
  41.  
  42. // Copy image to buffered image
  43. Graphics g = bimage.createGraphics();
  44.  
  45. // Paint the image onto the buffered image
  46. g.drawImage(image, 0, 0, image.getWidth(null), image.getHeight(null), null);
  47. g.dispose();
  48.  
  49. return bimage;
  50. }
  51. // This method returns true if the specified image has transparent pixels
  52. public static boolean hasAlpha(Image image) {
  53. // If buffered image, the color model is readily available
  54. if (image instanceof BufferedImage) {
  55. BufferedImage bimage = (BufferedImage)image;
  56. return bimage.getColorModel().hasAlpha();
  57. }
  58.  
  59. // Use a pixel grabber to retrieve the image's color model;
  60. // grabbing a single pixel is usually sufficient
  61. PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
  62. try {
  63. pg.grabPixels();
  64. } catch (InterruptedException e) {
  65. }
  66.  
  67. // Get the image's color model
  68. ColorModel cm = pg.getColorModel();
  69. return cm.hasAlpha();
  70. }
Reply With Quote  
Join Date: Jan 2008
Posts: 1,771
Reputation: VernonDozier is just really nice VernonDozier is just really nice VernonDozier is just really nice VernonDozier is just really nice VernonDozier is just really nice 
Rep Power: 8
Solved Threads: 218
VernonDozier VernonDozier is offline Offline
Posting Virtuoso

Re: How do you save a JPanel as a GIF file?

  #4  
Jul 25th, 2008
Awesome. Ezzaral, it worked on the first try! Thank you. I'm still going over the code line by line to try to get my hands around it, but like I said, it worked. Alex, thank you for the links. They look similar to what Ezzaral posted. I had heard that it can be a hassle to deal with gif files since there is some patent on the compression and that Acme released something to deal with that. Looks useful and I'll give it a try. Thanks a bunch, both of you!
Reply With Quote  
Join Date: May 2007
Location: USA
Posts: 2,843
Reputation: Ezzaral is a name known to all Ezzaral is a name known to all Ezzaral is a name known to all Ezzaral is a name known to all Ezzaral is a name known to all Ezzaral is a name known to all 
Rep Power: 12
Solved Threads: 283
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Posting Maven

Re: How do you save a JPanel as a GIF file?

  #5  
Jul 25th, 2008
Glad it worked for you.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 5:47 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC