Java Screen Saver Button Code
This java code is designed to create a control capable of allowing you to click the button displayed in the window and automatically save the screen as an image file, totally designed in java, it is being made available in just one fileNote: It is necessary to execute the program as administrator to save the images, which can be done using c ++,C # ,.Bat and only click on the right button and click on run as administrator(on Windows)This program is totally free and its license is totally open to any use
package autoshotscreen;
import java.awt.AWTException;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JLabel;
/**
*
* @author Leonardo
*/
public class Autoshotscreen {
public static void main(String[] args) throws IOException {
//---------------DATE----------------------//
SimpleDateFormat dataFormat = new SimpleDateFormat("dd MM yyyy hh mm ss"); //Date formate
GregorianCalendar hoje=new GregorianCalendar(); //Obeject to get date
String dat= (dataFormat.format(hoje.getTimeInMillis())); //Var with value of date
//---------------Frame/config----------------------//
JFileChooser direc = new JFileChooser(); //main to select folder
File arquivo = new File("c:\\autoshotscreen\\config.txt"); //file of config object
JFrame janela = new JFrame("Autoshotscreen"); //Object Frame
janela.setVisible(true); //Frame visible
janela.setSize(250, 200); //Size of Frame
janela.setDefaultCloseOperation(EXIT_ON_CLOSE); //finalize code on close
//janela.getContentPane().setBackground(Color.black); //background color
janela.setLayout(null); //Layout Frame(null for edit)
janela.setResizable(false); //Frame fixed size
JButton shotb = new JButton("Shot"); //Button shot object(button with function print)
shotb.setBounds(20,10 , 200,70 ); // Size and position of Button
janela.add(shotb); //put button in Frame
System.out.println(dat); //Show the String dat in console
JButton chanb = new JButton("Changer Folder"); //Button changer folder object
chanb.setBounds(10,100 , 120,20 ); // Size and position of Button
janela.add(chanb);//put button in Frame
//----------autor-----------------//
JLabel a1 = new JLabel("By: SeeBlueOrg Oppen Software");// Autor object
a1.setBackground(Color.red);//color of Label
a1.setBounds(40, 120, 500, 20);//Size of label
janela.add(a1);//add Label on frame
//-----------folder--------------changer//
String line = null;//String line void creation
JLabel a2 = new JLabel("Save folder:" + line);//Label with caminho track od save folder
a2.setBackground(Color.red);//set color of Label
a2.setBounds(40, 140, 500, 20);//Set size and position of Label
janela.add(a2);//add Label in frame
FileReader arfq = new FileReader("c:\\autoshotscreen\\config.txt");//file config object
BufferedReader lerArq = new BufferedReader(arfq);//read file config
line = lerArq.readLine(); //String line equals first line of config
String fort;
//try code continuation
a2.setText("Save folder:" + line); //Write String with the readed line
String fileName2 = line +"/"+ dat + ".jpg"; //String file name equals string dat with
//---------------Core----------------------//
String fileName = line + "/" + dat + ".jpg"; //String with file name (date + .jpg)
System.out.println(line);//Show line on console
shotb.addActionListener(new ActionListener() {//Button Shot action event detect
@Override //override to not creat new
public void actionPerformed(ActionEvent evt) { //Action of button Shot
janela.setVisible(false); //hide frame to not show in Print
try {
Thread.sleep(1000);
Robot robot = new Robot();// Creation of objetct robot to print
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); //get the screen size
Rectangle captureRect = new Rectangle(0, 0, screenSize.width, screenSize.height); //set screen size in retagle
BufferedImage screenFullImage = robot.createScreenCapture(captureRect); //Capture print
ImageIO.write(screenFullImage, "jpg", new File(fileName2));//save print whas .pjg image
} catch (AWTException | IOException | InterruptedException ex) {
}
janela.setVisible(true);//show the hided frame
}
});
//------------changer-----------------//
chanb.addActionListener(new ActionListener() { //Button changer folder action event detect
@Override //override to not creat new
public void actionPerformed(ActionEvent evt) { //Action of Changer Folder Button
direc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); //opeen main to changer folder
int res = direc.showOpenDialog(null); //format the main of object
File track = null; //String track creation
if(res == JFileChooser.APPROVE_OPTION){ //If folder was selected
track = direc.getSelectedFile();//get the track
try( FileWriter fw = new FileWriter( arquivo ) ){ //Object writer
BufferedWriter bw = new BufferedWriter(fw); //changer object to buffer(memory)
System.out.println(track);
String teste = track.toString();
bw.write(teste); //write the file with tack
bw.flush(); //save file
janela.repaint();//reload frame
Runtime.getRuntime().exec("explorer c:\\autoshotscreen\\autoshotscreenmain.exe");
System.exit(0);
}catch(IOException ex){ //try continuation
}
}
}
}); String folderss = "explorer " + line;//string with folder track
//////////////open--------------
JButton open = new JButton("Saves");//create button Saves
open.setBounds(145,100 , 75,20 );//position and size of button
janela.add(open);//add button to frame
open.addActionListener(new ActionListener() { //add action to Button
@Override
public void actionPerformed(ActionEvent evt)//detect action of Button
{try {
System.out.println(folderss);
Runtime.getRuntime().exec(folderss);//oppen Saves folder
} catch (IOException ex) {
}
}
});
}
}
compiled file for download(.JAR)
Download Here
Comentários
Postar um comentário