Magazine
 
Tips & Tricks
 

BufferedImage.
BufferedImage image = robot.createScreenCapture
(new Rectangle(Toolkit.getDefaultToolkit().
getScreenSize()));

// Write the image to file

ImageIO.write(image, “gif”, new File(fileName));

// Create the message dialogue

showing the success of the task.
JOptionPane.showMessageDialog
(null, “Screen captured successfully.”, “javajazzup”, 1);
}
}
catch(Exception e){}
}
}
public static void main(String[] args) throws
Exception {
ScreenshotExample screen = new
ScreenshotExample();
}
}

This program shows a frame, which holds a command button labeled by “Capture Screen”.

Screen Capture

When you click on the button then an input dialog box is open for inputting the file name, which is created after capturing the screen.

Screen Capture Code Java

  And then a message dialog box is opened with message “Screen captured successfully.”

Capturing Screen


3. Launching the user default browser to show a specified URI.

Using java.awt.Desktop class, a Java application can launch associated applications registered on the current platform to handle a URI or a file. It supports operations like launching the user-default browser to show a specified URI, launching the user-default mail client with an optional mailto URI and launching a registered application to open, edit or print a specified file. This sample code demonstrates how to launch the user default browser to show a specified URI.

LaunchBrowser.java

import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.URI;
import java.net.URISyntaxException;
public class LaunchBrowser {
public LaunchBrowser(){
JFrame frame = new JFrame(“Open
Browser Frame.”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton(“Open
Browser”);
button.addActionListener(new
OpenBrowserAction());
JPanel panel = new JPanel();
panel.add(button);
frame.add(panel, BorderLayout.CENTER);
frame.setSize(200, 200);
frame.setVisible(true);
}

Oct 2007 | Java Jazz Up | 72
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