Código:
main es otra clase que genera lo grafico.import java.awt.AWTException; import java.awt.HeadlessException; import java.awt.Image; import java.awt.Robot; import java.awt.Rectangle; import java.awt.Toolkit; import java.awt.image.BufferedImage; import java.io.*; import javax.imageio.ImageIO; import javax.swing.ImageIcon; class CapturarPantalla { int x=1; Main m; public CapturarPantalla(Main m){ this.m=m; } public int captura() throws HeadlessException, AWTException, IOException{ BufferedImage pantalla = new Robot().createScreenCapture( new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()) ); File file = new File("src/pantalla"+x+".jpg"); ImageIO.write(pantalla, "jpg", file); ImageIcon tmpIconAux = new ImageIcon(getClass().getResource("src/pantalla"+x+".jpg")); mageIcon tmpIcon = new ImageIcon(tmpIconAux.getImage().getScaledInstance(950, -5, Image.SCALE_DEFAULT)); this.m.label.setIcon(tmpIcon); this.m.label.validate(); x++; return 1; } public static void main(String args[]){ } }
Código:
este es el errorpublic static void main(String arg[]){Main m=new Main();objeto= new CapturarPantalla(m)} @Override public void actionPerformed(ActionEvent arg0) { try { objeto.captura(); }catch (HeadlessException e) { e.printStackTrace(); }catch (AWTException e) { e.printStackTrace(); }catch (IOException e) { e.printStackTrace(); } }
Código:
que puede serException in thread "AWT-EventQueue-0" java.lang.NullPointerException at javax.swing.ImageIcon.<init>(Unknown Source) at CapturarPantalla.captura(CapturarPantalla.java:26) at Main.actionPerformed(Main.java:42) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)
saludos