FreeCaptureのダウンロード
ダウンロード開始
FreeCaptureのソースコード
●FreeCaptureクラス(FreeCapture.java)
package jp.tuyano;
import java.awt.*;
import java.awt.image.BufferedImage;
public class FreeCapture {
public static void main(String[] args){
FreeCapture fc1 = new FreeCapture();
fc1.doCapture();
}
public void doCapture(){
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
try{
Robot r2d2 = new Robot();
BufferedImage img = r2d2.createScreenCapture(new Rectangle(screenSize));
CaptureFrame jf1 = new CaptureFrame(img);
jf1.setTitle("Capture screen");
jf1.setSize(300, 300);
jf1.setLocation(50,50);
jf1.setVisible(true);
Toolkit.getDefaultToolkit().beep();
} catch(Exception e){}
}
}
●CaptureFrameクラス(CaptureFrame.java)
package jp.tuyano;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
public class CaptureFrame extends JFrame {
private static final long serialVersionUID = 1L;
private CapturePanel jp1;
private JScrollPane js1;
public CaptureFrame(BufferedImage img){
super();
jp1 = new CapturePanel(img);
this.getContentPane().setLayout(new BorderLayout());
js1 = new JScrollPane(jp1);
this.getContentPane().add(js1,BorderLayout.CENTER);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
jp1.saveJPEG();
System.exit(0);
}
});
}
}
●CapturePanelクラス(CapturePanel.java)
package jp.tuyano;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.*;
public class CapturePanel extends JPanel {
private static final long serialVersionUID = 1L;
private BufferedImage img;
private JFileChooser chooser;
private Dimension psize;
public CapturePanel(BufferedImage img){
super();
this.img = img;
psize = new Dimension(img.getWidth(),img.getHeight());
this.setPreferredSize(psize);
chooser = new JFileChooser();
}
public void paint(Graphics g) {
if (img != null) {
g.drawImage(img,0,0,this);
}
}
public void saveJPEG(){
int result = chooser.showSaveDialog(this);
File file = chooser.getSelectedFile();
if(result == JFileChooser.APPROVE_OPTION){
try{
ImageIO.write(img,"jpeg",file);
} catch(Exception e){}
}
}
}