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



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

Have Programming Question? Ask it here!
 
 
 


nagarjuna
How to save JPanel as gif or jpeg file
2 Answer(s)      3 years and 6 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 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
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 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
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 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 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 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
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 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
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
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 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
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
Which extension i use for save hibernate file.
Which extension i use for save hibernate file.   Please give me answer that which extension i use for save the hibernate file and how i run that file as well as complie that file.   hi friend, Naming convention
Save file on Client Machine - Development process
from his machine try to save the file it gets saved in the serve machine i want to save the file created by my application on the client machine how can i do...Save file on Client Machine  Hi my application is running on main
why java file save as its class name
why java file save as its class name  hi,this is subbareddy vajrala.my doubt is why java files save as its class names
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
How to copy an jpeg image from one folder to other folder using jsp
How to copy an jpeg image from one folder to other folder using jsp  How to copy an image from one folder to another folder using jsp
How to copy an jpeg image from one folder to other folder using jsp
How to copy an jpeg image from one folder to other folder using jsp  hello, i am creating photo gallery where from admin side i allow admin to browse... be in my image folder...so,How to copy an jpeg image from one folder to other
Documentation
workbench. Print screenshots or save them to GIF, BMP, PNG, JPeG, or TIFF... a formatted version of a (almost) plain text file
code save word file in 10g database - SQL
code save word file in 10g database  I am not having any idea to save...(false); File file = new File(fileName); fis = new FileInputStream(file); pstmt = conn.prepareStatement("insert into file( file, file_data
PHP GD gif
: image/gif'); $img = LoadGif('Roseindia'); imagegif($img); imagedestroy($img
How to save database data into excelsheet using hibernate
How to save database data into excelsheet using hibernate  How to save database data into excelsheet using hibernate
how to save images in oracle using JSP
how to save images in oracle using JSP  how to insert images in oracle using jsp
how to save images in oracle using JSP
how to save images in oracle using JSP  how to insert images in oracle using jsp
how to save a gui form in core java
how to save a gui form in core java  please help me i am java beginner how to save a jframe containing jtable and panels in java thank you
URL file Download and Save in the Local Directory
URL file Download and Save in the Local Directory       This Program file download  from  URL and save this Url File in the specified directory. This program specifies
How to save and get value from JSP
How to save and get value from JSP  Employee Name Time... 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
scrollbar in JPanel
f = new Frame ("Demo");JPanel p = new JPanel();JButton b1 = new...) { Frame f = new Frame ("Demo"); JPanel p = new JPanel(); JButton b1 = new JButton
Scroll in JPanel
static void main(String[] args) { Frame f = new Frame ("Demo"); JPanel p = new JPanel(); JButton b1 = new JButton("Button 1"); JButton b2 = new JButton("Button 2
How can we save a data list in jsf?
How can we save a data list in jsf?  I have a code in jsf that fires a query in Data Base each time it is called, i have added a validation check... it is called again. I tried using save State but it did not help as other functions got
How to save user interface in flex for design tool
How to save user interface in flex for design tool  Hi I want to create a flex application "sticker design tool" in which i want to save all the caption/logos used with its properties like size,position,textcolor etc.,so when
Java save file to database
Java save file to database In this section, you will learn how to insert file... between MySql database and java file with the help of various types of APIs, interfaces and methods. Then we have created an instance of File class and specify
JPanel - Container
Java: JPanel - Container   Learn how to use the JPanel Container in a Swing application.   Swing comes with many components. The JPanel is one... application.   In this example code you will learn many methods of JPanel class
 

 

 

DMCA.com