Magazine
 
Tips & Tricks
 

After opening a notepad it starts writing “javajazzup” in it like below:



Now, it opens “Save As” dialog box and writes “robo.txt” in File name and hit enter key.



The file has been saved as “robo.txt” and it can be seen in the figure below.



2. Capturing the screen in java:
Capturing the screen is just one line logic in Java, using the java.awt.Robot class. Its createScreenCapture(Rectangle) method takes the location and size of the area to capture and then this screen is stored as a java.awt.image.BufferedImage, which can

 

easily be written to file using the classes from the javax.imageio package. This sample code demonstrates you how to capture the screen in gif format.

ScreenshotExample.java

import javax.swing.*;
import javax.imageio.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
public class ScreenshotExample {
public ScreenshotExample(){
JFrame frame = new JFrame(“Screenshot Frame.”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton(“Capture Screen”);
button.addActionListener(new CaptureAction());
JPanel panel = new JPanel();
panel.add(button);
frame.add(panel, BorderLayout.CENTER);
frame.setSize(200, 200);
frame.setVisible(true);
}
public class CaptureAction implements
ActionListener{
public void actionPerformed(ActionEvent ae){
try{

//Create Input dialog to take the file name from the user to save the captured screen shot.

String fileName = JOptionPane.
showInputDialog(null, “Enter file name : “, “javajazzup”, 1);
if (!fileName.toLowerCase().endsWith(“.gif”)){
JOptionPane.showMessageDialog
(null, “Error: file name must end with \”.gif\”.”, “javajazzup”, 1);
}
else{

// Create instance of Robot class.

Robot robot = new Robot();

// Capture full screen and store it as

Oct 2007 | Java Jazz Up | 71
previous
index
next
 
View All Topics
All Pages of this Issue
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,

30
, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53 , 54, 55, 56, 57,

58
, 59, 60, 61, 62, 63 , 64, 65 , 66 , 67 , 68 , 69 , 70 , 71 , 72 , 73 , 74 , 75 , 76 , 77 , 78 , 79 , 80 , 81 , 82 ,

Download PDF