Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Swing-AWT How to save JPanel as gif or jpeg file
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


nagarjuna
How to save JPanel as gif or jpeg file
2 Answer(s)      3 years and 11 months ago
Posted in : Swing AWT


i draw polygons , rectangles, lines on jpanel using draw line method drawpolygon methods etc, now i want save the data of jpanel which is drawn by me(lines,polygons) as a gif or jpeg file , how can i?


Advertisement
View Answers

October 19, 2010 at 5:09 PM


Hi Friend,

Try the following code:

import java.io.*;
import java.awt.*;
import javax.swing.*;
import javax.imageio.*;
import java.awt.event.*;
import java.awt.image.*;

public class SavePanel extends JPanel{
  private static final Dimension MAIN_SIZE = new Dimension(400, 200);
    public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    Dimension dimension = getSize();
    g2d.drawLine(10,50,80,80);
    g2d.drawRect(130,50,80,80);
    Polygon polygon= new Polygon();
     for (int i = 0; i < 6; i++){
      polygon.addPoint((int) (300 + 50 * Math.cos(i * 2 * Math.PI / 6)),
          (int) (100 + 50 * Math.sin(i * 2 * Math.PI / 6)));
   }
   g2d.drawPolygon(polygon);
  }
  public SavePanel(){
     final JPanel p=this;
    JButton button = new JButton("Make Image");
    add(button);
    setPreferredSize(MAIN_SIZE);
    button.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent ae){
      JFrame win = (JFrame)SwingUtilities.getWindowAncestor(p);
      Dimension size = win.getSize();
      BufferedImage image = (BufferedImage)win.createImage(size.width, size.height);
      Graphics g = image.getGraphics();
      win.paint(g);
      g.dispose();
      try      {
        ImageIO.write(image, "jpg", new File("image.jpg"));
      }
      catch (IOException e){
        e.printStackTrace();
      }
    }
  });
    }
  public static void main(String[] args)  {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(new SavePanel());
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}

Thanks



May 3, 2012 at 12:37 AM


Hi,

I am new to this. I am looking for the same type of solution and seen your code. But, what I am looking is without using the button "Make Image", I want to save it as an image. But, in this code, If I press "Make Image" button, then only i am able to save it as an image. Is it possible? Can you please help me in this? Can you please send me the code with out using "Make Image" button?

Thanks in advance.

Regards, Chandra.



Related Tutorials/Questions & Answers:
How to save JPanel as gif or jpeg file
How to save JPanel as gif or jpeg file  i draw polygons , rectangles, lines on jpanel using draw line method drawpolygon methods etc, now i want save the data of jpanel which is drawn by me(lines,polygons) as a gif or jpeg file
How to save JPanel as gif or jpeg file
How to save JPanel as gif or jpeg file  i draw polygons , rectangles, lines on jpanel using draw line method drawpolygon methods etc, now i want save the data of jpanel which is drawn by me(lines,polygons) as a gif or jpeg file
Advertisements
convert jpeg to gif - Swing AWT
convert jpeg to gif  i want to convert jpeg file to gif file... convert jpeg to gif. help me plz....  Hi friend, import java.io....(); File output = new File(imageName1); ImageIO.write(image, "gif", output
how to re size image proportionately using java for different images like jpeg, png, gif.
how to re size image proportionately using java for different images like jpeg, png, gif.  hi, i need sample code for re size image proportionately using java for different images like jpeg, png, gif, this code for my
Export chart to JPEG file
Export chart to JPEG file  Hi. I have one problem. I have generated piecharts and barcharts using chartlib.js and jquery in jsp. Now i want to export this charts to jpeg file . how can i do this? Thank q in advance
How to add JTable in JPanel
How to add JTable in JPanel  How to add JTable in JPanel
How to read content of JPEG file in Java?
in the Java Program. In this article we will see how to read the JPEG image file. We will use the javax.imageio.ImageIO class for reading the image file data...Example of reading the content of JPEG file in Java Java JAI API helps
How to upload and save a file in a folder - Java Beginners
How to upload and save a file in a folder  Hi How to upload and save a file in a folder? for example Agreement.jsp File: So when user browses the file and enters the submit button the respective file should
How to copy text from a gif image
How to copy text from a gif image  I have some gif images that containing some important text. i want to have that text in my notepad file. i have some hundred's of gif images like that. so that i can't type the text in notepad
Read content for JPEG image File in Java - Java Beginners
Read content for JPEG image File in Java  How to Read content for JPEG image File in Java? or Extract text from JPEG image File in Java
How to download and save a file from Internet using Java?
How to download and save a file from Internet using Java?  How to download and save a file from Internet using Java
How to create file and save it into user defined path using jsp/servlet?
How to create file and save it into user defined path using jsp/servlet?  Hi.. Onclick event I have created one file.When file will create it should asked where to save file(like browse option
How i upload file and save that record in database using JSP?
How i upload file and save that record in database using JSP?  Hi All, I m the beginner in JSP and I want to upload the file and store that file... this file). Now refer the Employee Profile but it not work in localhost. If anybody
How to save form data to a csv file using jquery or ajax
How to save form data to a csv file using jquery or ajax  Please let me know more about how to do this or please suggest me any sites to learn... this. i am able to read the csv file using this code if (window.XMLHttpRequest
How to draw in jPanel? swing/graphics Java
How to draw in jPanel? swing/graphics Java  How to draw in jPanel? (swing/graphics Java
how to save html form data into .csv file using only jsp.
how to save html form data into .csv file using only jsp.  Dear all, I am developing a website only using jsp and html. i need to save the form data into a .csv file using jsp. can anyone give me any sample solution or tutorial
upload and save the file
upload and save the file  upload and save a file from a computer to specific location of remote computer using jsp
How to save run time created text-file on a disk using jsp/servlet?
How to save run time created text-file on a disk using jsp/servlet?  I have a JSP page with save button.In that I created the file(e.g a.txt) at run-time,when I click on the save button,it will ask where to save the file
How to save image on the server by applet
How to save image on the server by applet  HI, in my project I am... If user want to export this chart as GIF format then this chart should be save as a gif image on the server directory. Can any Body help me how can I do it Thanks
How to save image on the server by applet
How to save image on the server by applet  HI, in my project I am... If user want to export this chart as GIF format then this chart should be save as a gif image on the server directory. Can any Body help me how can I do it Thanks
Text field save as word file
Text field save as word file  Dear experts At run how to save set of text field contains text into single Word file. To save in our desktop computer. Solve my problem
how to save an input to jsp page in a text file?ave an input given b
how to save an input to jsp page in a text file?ave an input given b  how to save the input given by the user in jsp page to a text file
How to Add JComboBox on JPanel - Java Beginners
How to Add JComboBox on JPanel  How to Add JComboBox on JPanel D:\java>javac ViewElements.java ViewElements.java:181: expected jc.addItem("France"); ^ ViewElements.java:182: expected
save excel file with save and open dilog box
save excel file with save and open dilog box  following is my jsp code it is working correct.. i want to save excel file throgh jsp bt not by hard...");) user should have freedom to save the file in any location so plz give me
How to download web page table data, export the table records in an excel file and save
How to download web page table data, export the table records in an excel file... be displayed and ask me to save the file. on clicking the save button the data should be exported in to the excel file. any views??? Thanks in advance
save text file - Java Beginners
save text file  hi i have just start programming in java.please guide me if i want to read a text file in java.then the text file is save in which directory
How to save value in JSP
How to save value in JSP  Employee Name Time-IN Time-OUT... 324 2012-12-12 save i want to save dis value jsp to action ...how can i get all value ..and store..how can its values goes
How to read data from txt file and save to database MySql - Java Beginners
How to read data from txt file and save to database MySql  Please help me again ,my problem right know is how to read data from txt file then data save to database from JButton.Please give me a tutorial and source code
How to save data - Swing AWT
How to save data  Hi, I have a problem about how to save data ,but first before data save to dababase, data must view in jLisit or Jtable... save data from jList and Jtable(in jList or jTable data will be many).Thank's
how to save uploaded image in database using javascript
how to save uploaded image in database using javascript  Please can you tell me how to store uploaded image in database using java I'll use the upload code as extArray = new Array(".gif", ".jpg", ".png"); function LimitAttach
How to Set Transperent Background Image to Jpanel - Java Beginners
How to Set Transperent Background Image to Jpanel  Sir ,How to set Background Image to Jpanel.  Hi Friend, Try the following code: import java.awt.*; import java.awt.event.*; import javax.swing.*; public
 

 

 

DMCA.com