Javax.Imageio Help Needed

Basically, I found a site with a Javax.Imageio applet used to take screenshots with the click of a button.

The problem is that I know nothing about compiling Java, and I would like some assistance. This applet is a necessary addition to a webpage gaming site browser (Basically it's a framed page which goes to the different game's servers to play the game) so that the users can easily take screenshots of what's happening in the game.

The example script they have is located at:

http://schmidt.devlib.org/java/save-screenshot.html

But I need it to be compiled and changed a bit... The output only allows output of one file, which isn't good... It saves as out.png, and rewrites over it if you go to take a 2nd screenshot. The problem with this is that many users will need to take multiple screenshots. I need this example applet changed to do something like "screen1.png" then "screen2.png" then "screen3.png", and so on... If possible, I'd like it to save to Desktop/Screenshots/screen(x).png

Could I get a bit of assistance?

Thanks a lot, I appreciate any help I could get VERY much.

Please post the finished applet if you're able to help as a response to here, preferably in the format of a zip or rar with the files inside.

[1285 byte] By [Supercoolyoa] at [2007-10-1 18:02:38]
# 1

This will generate the applet. that is required by you.

/*

* Created on Aug 2, 2005

*/

package com.screen;

import java.awt.Dimension;

import java.awt.Rectangle;

import java.awt.Robot;

import java.awt.Toolkit;

import java.awt.image.BufferedImage;

import java.io.File;

import javax.imageio.ImageIO;

import javax.swing.JApplet;

import javax.swing.JFileChooser;

import javax.swing.JOptionPane;

import javax.swing.WindowConstants;

/**

* @author shivak

*

* To change the template for this generated type comment go to

* Window>Preferences>Java>Code Generation>Code and Comments

*/

public class ScreenCapture extends JApplet {

private javax.swing.JPanel jContentPane = null;

private javax.swing.JLabel jLabel = null;

private javax.swing.JTextField jTextField = null;

private javax.swing.JButton jButton = null;

private javax.swing.JLabel jLabel1 = null;

private javax.swing.JTextField jTextField1 = null;

private javax.swing.JButton jButton1 = null;

private javax.swing.JLabel jLabel2 = null;

private javax.swing.JLabel jLabel3 = null;

/**

* This is the default constructor

*/

public ScreenCapture() {

super();

init();

}

/**

* This method initializes this

*

* @return void

*/

public void init() {

this.setContentPane(getJContentPane());

this.setSize(480, 394);

this.setCursor(new java.awt.Cursor(java.awt.Cursor.S_RESIZE_CURSOR));

this.setName("Screen Capture");

}

/**

* This method initializes jContentPane

*

* @return javax.swing.JPanel

*/

private javax.swing.JPanel getJContentPane() {

if (jContentPane == null) {

jContentPane = new javax.swing.JPanel();

jContentPane.setLayout(null);

jContentPane.add(getJLabel(), null);

jContentPane.add(getJTextField(), null);

jContentPane.add(getJButton(), null);

jContentPane.add(getJLabel1(), null);

jContentPane.add(getJTextField1(), null);

jContentPane.add(getJButton1(), null);

jContentPane.add(getJLabel2(), null);

jContentPane.add(getJLabel3(), null);

jContentPane.setName("panel");

jContentPane.setCursor(

new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));

}

return jContentPane;

}

/**

* This method initializes jLabel

*

* @return javax.swing.JLabel

*/

private javax.swing.JLabel getJLabel() {

if (jLabel == null) {

jLabel = new javax.swing.JLabel();

jLabel.setBounds(29, 141, 72, 22);

jLabel.setText("Output File");

jLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

jLabel

.addPropertyChangeListener(

new java

.beans

.PropertyChangeListener() {

public void propertyChange(java.beans.PropertyChangeEvent e) {

if ((e.getPropertyName().equals("text"))) {

System.out.println("propertyChange(text)");

// TODO Auto-generated property Event stub "text"

}

}

});

}

return jLabel;

}

/**

* This method initializes jTextField

*

* @return javax.swing.JTextField

*/

private javax.swing.JTextField getJTextField() {

if (jTextField == null) {

jTextField = new javax.swing.JTextField();

jTextField.setBounds(111, 140, 236, 22);

}

return jTextField;

}

/**

* This method initializes jButton

*

* @return javax.swing.JButton

*/

private javax.swing.JButton getJButton() {

if (jButton == null) {

jButton = new javax.swing.JButton();

jButton.setBounds(113, 238, 182, 37);

jButton.setText("Capture Screen");

jButton.addMouseListener(new java.awt.event.MouseAdapter() {

public void mouseClicked(java.awt.event.MouseEvent e) {

String outFileName = jTextField.getText().trim();

if ((jTextField.getText()).trim().equalsIgnoreCase("")) {

JOptionPane.showConfirmDialog(

jContentPane,

"Please Enter path and Output File name");

} else if (!outFileName.toLowerCase().endsWith(".png")) {

JOptionPane.showMessageDialog(

null,

"File Name should end with png.");

} else if (

jTextField1.getText().trim().equalsIgnoreCase("")) {

JOptionPane.showMessageDialog(

jContentPane,

"Time should be entered in seconds.");

} else {

try {

long time =

Long.parseLong(jTextField1.getText()) * 1000;

System.out.println(

"Waiting " + (time / 1000) + " second(s)...");

Thread.sleep(time);

} catch (NumberFormatException nfe) {

System.err.println(

"Time does not seem to be a "

+ "valid number of seconds.");

System.exit(1);

} catch (Exception ex) {

System.err.println(

"Time does not seem to be a "

+ "valid number of seconds.");

System.exit(1);

}

// determine current screen size

Toolkit toolkit = Toolkit.getDefaultToolkit();

Dimension screenSize = toolkit.getScreenSize();

Rectangle screenRect = new Rectangle(screenSize);

// create screen shot

try {

Robot robot = new Robot();

BufferedImage image =

robot.createScreenCapture(screenRect);

// save captured image to PNG file

ImageIO.write(image, "png", new File(outFileName));

// give feedback

System.out.println(

"Saved screen shot ("

+ image.getWidth()

+ " x "

+ image.getHeight()

+ " pixels) to file \""

+ outFileName

+ "\".");

} catch (Exception ed) {

System.out.println(ed.toString());

}

System.out.println("mouseClicked()");

}

}

});

}

return jButton;

}

/**

* This method initializes jLabel1

*

* @return javax.swing.JLabel

*/

private javax.swing.JLabel getJLabel1() {

if (jLabel1 == null) {

jLabel1 = new javax.swing.JLabel();

jLabel1.setBounds(29, 178, 36, 24);

jLabel1.setText("Time");

jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

}

return jLabel1;

}

/**

* This method initializes jTextField1

*

* @return javax.swing.JTextField

*/

private javax.swing.JTextField getJTextField1() {

if (jTextField1 == null) {

jTextField1 = new javax.swing.JTextField();

jTextField1.setBounds(113, 179, 79, 24);

}

return jTextField1;

}

/**

* This method initializes jButton1

*

* @return javax.swing.JButton

*/

private javax.swing.JButton getJButton1() {

if (jButton1 == null) {

jButton1 = new javax.swing.JButton();

jButton1.setBounds(356, 141, 89, 26);

jButton1.setText("Browse");

jButton1.addMouseListener(new java.awt.event.MouseListener() {

public void mouseClicked(java.awt.event.MouseEvent e) {

JFileChooser fc = new JFileChooser("C:\\temp\\");

fc.showOpenDialog(jContentPane);

System.out.println(fc.getSelectedFile());

jTextField.setText((fc.getSelectedFile()).toString());

System.out.println("mouseClicked()");

// TODO Auto-generated Event stub mouseClicked()

}

public void mousePressed(java.awt.event.MouseEvent e) {

}

public void mouseReleased(java.awt.event.MouseEvent e) {

}

public void mouseEntered(java.awt.event.MouseEvent e) {

}

public void mouseExited(java.awt.event.MouseEvent e) {

}

});

}

return jButton1;

}

/**

* This method initializes jLabel2

*

* @return javax.swing.JLabel

*/

private javax.swing.JLabel getJLabel2() {

if (jLabel2 == null) {

jLabel2 = new javax.swing.JLabel();

jLabel2.setBounds(73, 47, 358, 58);

jLabel2.setText("Print Screen Feature");

jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

}

return jLabel2;

}

/**

* This method initializes jLabel3

*

* @return javax.swing.JLabel

*/

private javax.swing.JLabel getJLabel3() {

if (jLabel3 == null) {

jLabel3 = new javax.swing.JLabel();

jLabel3.setBounds(64, 184, 51, 15);

jLabel3.setText("(sec's)");

jLabel3.setFont(

new java.awt.Font("DialogInput", java.awt.Font.PLAIN, 12));

}

return jLabel3;

}

}

ShivaKatulaa at 2007-7-11 12:40:29 > top of Java-index,Developer Tools,Java Compiler...