Tuesday 5 June 2018

Capture Component and save it in png image from java language

Capture Component and save it in png image from java language

when you create an java application with good GUI now need it print for hard information.it is to difficult how to print same GUI because all data present in component here we declared a good method how to capture same GUI to use of hard information in this post we tell you how to capture component and save it as image in png form that save in same directory where stolen your runnable programs.it to beneficial for printing information because it is a similar to your GUI simply you follow some steps and learn how to capture and save GUI  under java application.

step1

open netBeans ide.

step2

click at file menu then click at create new project then java application then click at the
next button then type project Name like Capture Component. then click at finish button.

step3

add jframe in your project with name Main.java .

step4

add some component at jframe and set its properties.

step5.

access your Main.java file in Capture Component.java from this code
 Main mm=new Main();
mm.setVisible(true);

step6.

run program and check it.

step7.

create a jbutton at jfram with title capture and save component

step8.

open source code of main.java file and add some required java class.
import java.awt.Component;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;

import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;

import java.awt.*;

import java.io.IOException;

step 8.

 create a public function to Capture Component for capture image. look like this
//this is capture component method now you use it
     public void captureComponent(Component component) {
Rectangle rect = component.getBounds();
JFrame editorFrame = new JFrame("captureComponent and save image");
              
try {
                      BufferedImage image = null;
                      
String format = "png";
String fileName = component.getName() + "." + format;
BufferedImage captureImage = 
new BufferedImage(rect.width, rect.height, 
BufferedImage.TYPE_INT_ARGB);
component.paint(captureImage.getGraphics());

ImageIO.write(captureImage, format, new File(fileName));
//save picture method
System.out.printf("The screenshot of %s was saved!", component.getName());
                        
                         image = ImageIO.read(new File(fileName));
                                        
                      JButton b1 = new JButton();
                     b1.setIcon(new javax.swing.ImageIcon(image));
        editorFrame.getContentPane().add(b1, BorderLayout.CENTER);
               editorFrame.pack();
   
        editorFrame.setLocationRelativeTo(null);
        editorFrame.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
this.setVisible(true);



this.validate();
                }
                catch(IOException ex) { }
   
    }

Step 9

 open Main.java file in design form and dbl click and do code this
captureComponent(this);

0 comments:

Post a Comment