• Post Reply Bookmark Topic Watch Topic
  • New Topic

putting images in java applet using swing, which is coded in netbeans IDE.  RSS feed

 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am a hobby programmer, however I am stuck in this java application of mine which has to put image from a directory. I have made a new package called finalconnect5.resources and also saved the image which I wanted to put in the GUI. I have tried to use the getSource() code but does not work.
I dont want to use the GUI builder, this has to be done via code,. How can I do it?

Here is what I tried to do private static final ImageIcon computerIcon = new ImageIcon(GomokuButton.class.getResource("finalconnect5.resources/Computer.png"));

but then got errors and following note.

Exception in thread "main" java.lang.ExceptionInInitializerError
at finalconnect5.Gomoku.createBoard(Gomoku.java:207)
at finalconnect5.Gomoku.<init>(Gomoku.java:51)
at finalconnect5.Gomoku.main(Gomoku.java:222)
Caused by: java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:138)
at finalconnect5.GomokuButton.<clinit>(GomokuButton.java:13)
... 3 more
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)


But for now putting some file from other file (which is physically located in my hard disk), all I am trying to get that image icon in my applet (using swing and the file formed is a ".jar" file)

Please help me. What should I do?
 
Sheriff
Posts: 22949
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since according to the API documentation for the getResource() method, the URL parameter is relative to the class which calls the method (in this case your GomokuButton class), my first advice would be to make sure you use an absolute URL, like this:



And of course make sure the image is actually in your jar file at that location.
 
Kabir Shah
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No still the problem is there.




Now the code is running but my system.is hanging.
I am trying to put this as my Image

The errors generating are:


Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space at java.awt.image.DataBufferInt.<init>(DataBufferInt.java:41) at java.awt.image.Raster.createPackedRaster(Raster.java:458) at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:1015) at sun.awt.image.SunVolatileImage.getBackupImage(SunVolatileImage.java:214) at sun.awt.image.VolatileSurfaceManager.getBackupSurface(VolatileSurfaceManager.java:247) at sun.awt.image.VolatileSurfaceManager.initialize(VolatileSurfaceManager.java:103) at sun.awt.image.SunVolatileImage.<init>(SunVolatileImage.java:66) at sun.awt.image.SunVolatileImage.<init>(SunVolatileImage.java:76) at sun.awt.image.SunVolatileImage.<init>(SunVolatileImage.java:87) at sun.awt.image.SunVolatileImage.<init>(SunVolatileImage.java:81) at sun.awt.windows.WComponentPeer.createVolatileImage(WComponentPeer.java:569) at java.awt.Component.createVolatileImage(Component.java:3209) at java.awt.Component$BltBufferStrategy.createBackBuffers(Component.java:3864) at java.awt.Component$BltBufferStrategy.<init>(Component.java:3809) at java.awt.Component$BltSubRegionBufferStrategy.<init>(Component.java:4065) at java.awt.Component.createBufferStrategy(Component.java:3455) at java.awt.Window.createBufferStrategy(Window.java:2968) at javax.swing.BufferStrategyPaintManager$BufferInfo.createBufferStrategy(BufferStrategyPaintManager.java:858) at javax.swing.BufferStrategyPaintManager$BufferInfo.createBufferStrategy(BufferStrategyPaintManager.java:815) at javax.swing.BufferStrategyPaintManager$BufferInfo.getBufferStrategy(BufferStrategyPaintManager.java:738) at javax.swing.BufferStrategyPaintManager.prepare(BufferStrategyPaintManager.java:513) at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:271) at javax.swing.RepaintManager.paint(RepaintManager.java:1128) at javax.swing.JComponent.paint(JComponent.java:1013) at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:21) at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:60) at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:97) at java.awt.Container.paint(Container.java:1797) at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:734) at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:679) at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:659) at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:128) Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space at java.awt.image.DataBufferInt.<init>(DataBufferInt.java:41) at java.awt.image.Raster.createPackedRaster(Raster.java:458) at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:1015) at sun.awt.image.SunVolatileImage.getBackupImage(SunVolatileImage.java:214) at sun.awt.image.VolatileSurfaceManager.getBackupSurface(VolatileSurfaceManager.java:247) at sun.awt.image.VolatileSurfaceManager.initialize(VolatileSurfaceManager.java:103) at sun.awt.image.SunVolatileImage.<init>(SunVolatileImage.java:66) at sun.awt.image.SunVolatileImage.<init>(SunVolatileImage.java:76) at sun.awt.image.SunVolatileImage.<init>(SunVolatileImage.java:87) at sun.awt.image.SunVolatileImage.<init>(SunVolatileImage.java:81) at sun.awt.windows.WComponentPeer.createVolatileImage(WComponentPeer.java:569) at java.awt.Component.createVolatileImage(Component.java:3209) at java.awt.Component$BltBufferStrategy.createBackBuffers(Component.java:3864) at java.awt.Component$BltBufferStrategy.<init>(Component.java:3809) at java.awt.Component$BltSubRegionBufferStrategy.<init>(Component.java:4065) at java.awt.Component.createBufferStrategy(Component.java:3455) at java.awt.Window.createBufferStrategy(Window.java:2968) at javax.swing.BufferStrategyPaintManager$BufferInfo.createBufferStrategy(BufferStrategyPaintManager.java:858) at javax.swing.BufferStrategyPaintManager$BufferInfo.createBufferStrategy(BufferStrategyPaintManager.java:815) at javax.swing.BufferStrategyPaintManager$BufferInfo.getBufferStrategy(BufferStrategyPaintManager.java:738) at javax.swing.BufferStrategyPaintManager.prepare(BufferStrategyPaintManager.java:513) at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:271) at javax.swing.RepaintManager.paint(RepaintManager.java:1128) at javax.swing.JComponent._paintImmediately(JComponent.java:5070) at javax.swing.JComponent.paintImmediately(JComponent.java:4880) at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:723) at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:679) at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:659) at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:128) at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209) at java.awt.EventQueue.dispatchEvent(EventQueue.java:597) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273) Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space at java.awt.image.DataBufferInt.<init>(DataBufferInt.java:41) at java.awt.image.Raster.createPackedRaster(Raster.java:458) at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:1015) at sun.awt.image.SunVolatileImage.getBackupImage(SunVolatileImage.java:214) at sun.awt.image.VolatileSurfaceManager.getBackupSurface(VolatileSurfaceManager.java:247) at sun.awt.image.VolatileSurfaceManager.initialize(VolatileSurfaceManager.java:103) at sun.awt.image.SunVolatileImage.<init>(SunVolatileImage.java:66) at sun.awt.image.SunVolatileImage.<init>(SunVolatileImage.java:76) at sun.awt.image.SunVolatileImage.<init>(SunVolatileImage.java:87) at sun.awt.image.SunVolatileImage.<init>(SunVolatileImage.java:81) at sun.awt.windows.WComponentPeer.createVolatileImage(WComponentPeer.java:569) at java.awt.Component.createVolatileImage(Component.java:3209) at java.awt.Component$BltBufferStrategy.createBackBuffers(Component.java:3864) at java.awt.Component$BltBufferStrategy.<init>(Component.java:3809) at java.awt.Component$BltSubRegionBufferStrategy.<init>(Component.java:4065) at java.awt.Component.createBufferStrategy(Component.java:3455) at java.awt.Window.createBufferStrategy(Window.java:2968) at javax.swing.BufferStrategyPaintManager$BufferInfo.createBufferStrategy(BufferStrategyPaintManager.java:858) at javax.swing.BufferStrategyPaintManager$BufferInfo.createBufferStrategy(BufferStrategyPaintManager.java:815) at javax.swing.BufferStrategyPaintManager$BufferInfo.getBufferStrategy(BufferStrategyPaintManager.java:738) at javax.swing.BufferStrategyPaintManager.prepare(BufferStrategyPaintManager.java:513) at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:271) at javax.swing.RepaintManager.paint(RepaintManager.java:1128) at javax.swing.JComponent._paintImmediately(JComponent.java:5070) at javax.swing.JComponent.paintImmediately(JComponent.java:4880) at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:723) at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:679) at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:659) at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:128) at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209) at java.awt.EventQueue.dispatchEvent(EventQueue.java:597) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273) Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space at java.awt.image.DataBufferInt.<init>(DataBufferInt.java:41) at java.awt.image.Raster.createPackedRaster(Raster.java:458) at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:1015) at sun.awt.image.SunVolatileImage.getBackupImage(SunVolatileImage.java:214) at sun.awt.image.VolatileSurfaceManager.getBackupSurface(VolatileSurfaceManager.java:247) at sun.awt.image.VolatileSurfaceManager.initialize(VolatileSurfaceManager.java:103) at sun.awt.image.SunVolatileImage.<init>(SunVolatileImage.java:66) at sun.awt.image.SunVolatileImage.<init>(SunVolatileImage.java:76) at sun.awt.image.SunVolatileImage.<init>(SunVolatileImage.java:87) at sun.awt.image.SunVolatileImage.<init>(SunVolatileImage.java:81) at sun.awt.windows.WComponentPeer.createVolatileImage(WComponentPeer.java:569) at java.awt.Component.createVolatileImage(Component.java:3209) at java.awt.Component$BltBufferStrategy.createBackBuffers(Component.java:3864) at java.awt.Component$BltBufferStrategy.<init>(Component.java:3809) at java.awt.Component$BltSubRegionBufferStrategy.<init>(Component.java:4065) at java.awt.Component.createBufferStrategy(Component.java:3455) at java.awt.Window.createBufferStrategy(Window.java:2968) at javax.swing.BufferStrategyPaintManager$BufferInfo.createBufferStrategy(BufferStrategyPaintManager.java:858) at javax.swing.BufferStrategyPaintManager$BufferInfo.createBufferStrategy(BufferStrategyPaintManager.java:815) at javax.swing.BufferStrategyPaintManager$BufferInfo.getBufferStrategy(BufferStrategyPaintManager.java:738) at javax.swing.BufferStrategyPaintManager.prepare(BufferStrategyPaintManager.java:513) at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:271) at javax.swing.RepaintManager.paint(RepaintManager.java:1128) at javax.swing.JComponent._paintImmediately(JComponent.java:5070) at javax.swing.JComponent.paintImmediately(JComponent.java:4880) at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:723) at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:679) at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:659) at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:128) at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209) at java.awt.EventQueue.dispatchEvent(EventQueue.java:597) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273) Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space BUILD SUCCESSFUL (total time: 19 seconds)


Please help what should I do.
 
Sheriff
Posts: 21182
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your resource cannot be found, so getResource returns null. Can you show us the folder structure up to the resource?
 
Rob Spoor
Sheriff
Posts: 21182
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The resource should be "/finalconnect5/resources/Computer.png" - all path parts must use / as a separator. That doesn't explain the OutOfMemoryError though. How big is that image?
 
Kabir Shah
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The size of the image is 7.93 KB (8,125 bytes)
and the dimensions are 327x327
 
Kabir Shah
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for replying but I have not fixed the problem.
Its about the dimensions of the picture.
I reduced it, and now got working.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!