Draw BufferedImage

3 respostas
java
N
Olá galera.

Estou aprendendo a classe drawImage e queria pintar na tela uma imagem,iria fazer com ImageIcon,

fazendo assim:

2d.drawImage(imageicon.getImage(),etc);

Mas me sugeriram fazer com BufferedImage

Nos artigos que vi isto,você precisaria de um InputStream,um File,ou uma URL e usava como um portador da imagem

File arquivo = new File(teste.png);

BufferedImage image = ImageIO.read(arquivo);

Depois disso você colocava

g.drawImage(image,x,y,null);

Mas não esta funcionando,nem compilando está para falar a verdade!

Queria a ajuda de vocês para minha compreensão.

Obrigado!

3 Respostas

darlan_machado

Para não compilar, algum erro existe.
Qual?

N
at java.desktop/javax.imageio.ImageIO.read(Unknown Source)

at Wind.paint(Wind.java:33)

at java.desktop/javax.swing.RepaintManager$4.run(Unknown Source)

at java.desktop/javax.swing.RepaintManager$4.run(Unknown Source)

at java.base/java.security.AccessController.doPrivileged(Native Method)

at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)

at java.desktop/javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)

at java.desktop/javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)

at java.desktop/javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)

at java.desktop/javax.swing.RepaintManager.access$1200(Unknown Source)

at java.desktop/javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)

at java.desktop/java.awt.event.InvocationEvent.dispatch(Unknown Source)

at java.desktop/java.awt.EventQueue.dispatchEventImpl(Unknown Source)

at java.desktop/java.awt.EventQueue.access$600(Unknown Source)

at java.desktop/java.awt.EventQueue$4.run(Unknown Source)

at java.desktop/java.awt.EventQueue$4.run(Unknown Source)

at java.base/java.security.AccessController.doPrivileged(Native Method)

at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)

at java.desktop/java.awt.EventQueue.dispatchEvent(Unknown Source)

at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)

at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)

at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)

at java.desktop/java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.desktop/java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.desktop/java.awt.EventDispatchThread.run(Unknown Source)

javax.imageio.IIOException: Cant read input file!

at java.desktop/javax.imageio.ImageIO.read(Unknown Source)

at Wind.paint(Wind.java:33)

at java.desktop/javax.swing.RepaintManager$4.run(Unknown Source)

at java.desktop/javax.swing.RepaintManager$4.run(Unknown Source)

at java.base/java.security.AccessController.doPrivileged(Native Method)

at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)

at java.desktop/javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)

at java.desktop/javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)

at java.desktop/javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)

at java.desktop/javax.swing.RepaintManager.access$1200(Unknown Source)

at java.desktop/javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)

at java.desktop/java.awt.event.InvocationEvent.dispatch(Unknown Source)

at java.desktop/java.awt.EventQueue.dispatchEventImpl(Unknown Source)

at java.desktop/java.awt.EventQueue.access$600(Unknown Source)

at java.desktop/java.awt.EventQueue$4.run(Unknown Source)

at java.desktop/java.awt.EventQueue$4.run(Unknown Source)

at java.base/java.security.AccessController.doPrivileged(Native Method)

at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)

at java.desktop/java.awt.EventQueue.dispatchEvent(Unknown Source)

at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)

at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)

at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)

at java.desktop/java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.desktop/java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.desktop/java.awt.EventDispatchThread.run(Unknown Source)

javax.imageio.IIOException: Cant read input file!

at java.desktop/javax.imageio.ImageIO.read(Unknown Source)

at Wind.paint(Wind.java:33)

at java.desktop/javax.swing.RepaintManager$4.run(Unknown Source)

at java.desktop/javax.swing.RepaintManager$4.run(Unknown Source)

at java.base/java.security.AccessController.doPrivileged(Native Method)

at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)

at java.desktop/javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)

at java.desktop/javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)

at java.desktop/javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)

at java.desktop/javax.swing.RepaintManager.access$1200(Unknown Source)

at java.desktop/javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)

at java.desktop/java.awt.event.InvocationEvent.dispatch(Unknown Source)

at java.desktop/java.awt.EventQueue.dispatchEventImpl(Unknown Source)

at java.desktop/java.awt.EventQueue.access$600(Unknown Source)

at java.desktop/java.awt.EventQueue$4.run(Unknown Source)

at java.desktop/java.awt.EventQueue$4.run(Unknown Source)

at java.base/java.security.AccessController.doPrivileged(Native Method)

at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)

at java.desktop/java.awt.EventQueue.dispatchEvent(Unknown Source)

at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)

at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)

at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)

at java.desktop/java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.desktop/java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.desktop/java.awt.EventDispatchThread.run(Unknown Source)

javax.imageio.IIOException: Cant read input file!

at java.desktop/javax.imageio.ImageIO.read(Unknown Source)

at Wind.paint(Wind.java:33)

at java.desktop/javax.swing.RepaintManager$4.run(Unknown Source)

at java.desktop/javax.swing.RepaintManager$4.run(Unknown Source)

at java.base/java.security.AccessController.doPrivileged(Native Method)

at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)

at java.desktop/javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)

at java.desktop/javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)

at java.desktop/javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)

at java.desktop/javax.swing.RepaintManager.access$1200(Unknown Source)

at java.desktop/javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)

at java.desktop/java.awt.event.InvocationEvent.dispatch(Unknown Source)

at java.desktop/java.awt.EventQueue.dispatchEventImpl(Unknown Source)

at java.desktop/java.awt.EventQueue.access$600(Unknown Source)

at java.desktop/java.awt.EventQueue$4.run(Unknown Source)

at java.desktop/java.awt.EventQueue$4.run(Unknown Source)

at java.base/java.security.AccessController.doPrivileged(Native Method)

at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)

at java.desktop/java.awt.EventQueue.dispatchEvent(Unknown Source)

at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)

at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)

at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)

at java.desktop/java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.desktop/java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.desktop/java.awt.EventDispatchThread.run(Unknown Source)
TerraSkilll

Isso é uma exceção, o que significa que seu código compila, mas dá um erro durante a execução.

Aparentemente, faltou você copiar o começo da mensagem de erro, que diz qual exceção foi disparada. Mas, pelo restante, deduzo que tenha a ver com o fato de o arquivo “teste.png” não estar sendo encontrado, o que faz com que o File arquivo fique nulo e dê erro na linha seguinte ( BufferedImage image = ImageIO.read(arquivo); ).

Certifique-se de que o arquivo teste.png está num caminho que sua classe consegue encontrar e que o objeto File não está ficando inválido.

Abraço.

Criado 5 de junho de 2018
Ultima resposta 5 de jun. de 2018
Respostas 3
Participantes 3