Criar botão que printa a tela

Galera tenho a seguinte classe que printa a tela do meu pc quando executo shift + f6 porém gostaria de ter um botão onde clico e ele printa a tela na qual ele está alguém pode me ajudar? a seguir está o código da classe que faz o print…

package visão;

import java.awt.AWTException;
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 java.io.IOException;
import javax.imageio.ImageIO;

/**
 *
 * @author Mattheus
 */
public class Capture {
	
	private static Rectangle Rectangle;
	
	public static void main(String []args ) throws AWTException, IOException {
		/* Set the Nimbus look and feel */
		//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
		
		/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
		
		Robot robot = new Robot();
		Dimension a = Toolkit.getDefaultToolkit().getScreenSize();
		Rectangle = new Rectangle(a);
		
		BufferedImage img  = robot.createScreenCapture(Rectangle);
		
		ImageIO.write(img,"jpg",new File("c:/z/1.jpg"));
		ImageIO.write(img,"bmp",new File("c:/z/1.bmp"));
		ImageIO.write(img,"png",new File("c:/z/1.png"));
	}
}
1 curtida

Você pode encontrar a resposta detalhada em Botões


      public class eventos extends Applet
      {
      @Override
      public void init(){
      add(new Button("Inserir"));
     }

@111aaa Não é recomendado mais o uso de Applet em programação java.

1 curtida

O que eu posso fazer? Existe algum motivo em especial para não usar?

Os navegadores já não dão suporte para Applet já tem um bom tempo. Acho que o firefox ainda permite usar, mas tem que habilitar.

1 curtida

Valeu pela resposta ^^

try {// evento que printa a tela
        Rectangle screenrecRectangle = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
        BufferedImage capture =new Robot().createScreenCapture(screenrecRectangle);
        ImageIcon icon = new ImageIcon(capture);
        
        // ImageIO.write(capture,"jpg",new File("c:/z/1.jpg"));
          //  ImageIO.write(capture,"bmp",new File("c:/z/1.bmp"));
      ImageIO.write(capture,"png",new File("c:/z/1.png"));

        
    }catch (Exception ex ){
        JOptionPane.showMessageDialog(null,"");
    }
1 curtida

basta criar mouse event e colocar esse código com as importações necessárias que funciona legal, obrigado a todos amigos que ajudaram neste post

1 curtida