1

When loading an image using ImageIO, you are returned a BufferedImage. I used this BufferedImage in the function java.awt.image.BufferedImage.getSubimage(int,int,int,int). Now... I need to print some of these BufferedImage's, but the java.awt.Graphics.drawImage(Image,int,int,null) function takes in an Image, not a BufferedImage. How to convert a BufferedImage to an Image? Code:

import javax.swing.JPanel; import javax.swing.BorderFactory; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import javax.swing.SwingUtilities; import javax.swing.JFrame; import java.awt.Image; import java.io.*; import java.net.URL; import javax.imageio.*; import java.awt.image.*; import java.awt.Font; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.awt.image.BufferedImage; public class engine{ public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { GUI(); } }); } private static void GUI(){ JFrame f = new JFrame("Daealia"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setPreferredSize(new Dimension(800, 600)); f.add(new p()); f.pack(); f.setVisible(true); } } class p extends JPanel{ public p(){} public engine eng = new engine(); public BufferedImage[][] spells; public BufferedImage image; public void paintComponent(Graphics g){ g.setColor(new Color(82,41,0)); g.fillRect(0,0,74,254); g.setColor(new Color(16,8,0)); g.drawRect(0,0,74,254); try{ image = ImageIO.read(new File("spells.png"));} catch(IOException e){System.out.println("EXCEPTION THROWN!"); } for(int k = 0; k <= 6; k++){ spells[k][1] = image.getSubimage((34*2*k)-34*2,0,34,34); } for(int j = 0; j <= 6; j++){ spells[j][2] = image.getSubimage((34*2*j)-34,0,34,34); } for(int a = 0; a <= 6; a++){ for(int b = 0; b <= 1; b++){ g.drawImage(spells[a][b],1+((b-1)*34),1+((a-1)*34),null); } } } } 

Edit: The error I get is:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at p.paintComponent(engine.java:51) at javax.swing.JComponent.paint(JComponent.java:1045) at javax.swing.JComponent.paintChildren(JComponent.java:878) at javax.swing.JComponent.paint(JComponent.java:1054) at javax.swing.JComponent.paintChildren(JComponent.java:878) at javax.swing.JComponent.paint(JComponent.java:1054) at javax.swing.JLayeredPane.paint(JLayeredPane.java:585) at javax.swing.JComponent.paintChildren(JComponent.java:878) at javax.swing.JComponent.paintToOffscreen(JComponent.java:5219) at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:295) at javax.swing.RepaintManager.paint(RepaintManager.java:1236) at javax.swing.JComponent.paint(JComponent.java:1031) at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:39) at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:78) at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:115) at java.awt.Container.paint(Container.java:1967) at java.awt.Window.paint(Window.java:3877) at javax.swing.RepaintManager$3.run(RepaintManager.java:807) at javax.swing.RepaintManager$3.run(RepaintManager.java:784) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:784) at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:757) at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:706) at javax.swing.RepaintManager.access$1000(RepaintManager.java:62) at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1647) at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733) at java.awt.EventQueue.access$200(EventQueue.java:103) at java.awt.EventQueue$3.run(EventQueue.java:694) at java.awt.EventQueue$3.run(EventQueue.java:692) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:703) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138) at java.awt.EventDispatchThread.run(EventDispatchThread.java:91) Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at p.paintComponent(engine.java:51) at javax.swing.JComponent.paint(JComponent.java:1045) at javax.swing.JComponent.paintChildren(JComponent.java:878) at javax.swing.JComponent.paint(JComponent.java:1054) at javax.swing.JComponent.paintChildren(JComponent.java:878) at javax.swing.JComponent.paint(JComponent.java:1054) at javax.swing.JLayeredPane.paint(JLayeredPane.java:585) at javax.swing.JComponent.paintChildren(JComponent.java:878) at javax.swing.JComponent.paintToOffscreen(JComponent.java:5219) at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:295) at javax.swing.RepaintManager.paint(RepaintManager.java:1236) at javax.swing.JComponent.paint(JComponent.java:1031) at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:39) at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:78) at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:115) at java.awt.Container.paint(Container.java:1967) at java.awt.Window.paint(Window.java:3877) at javax.swing.RepaintManager$3.run(RepaintManager.java:807) at javax.swing.RepaintManager$3.run(RepaintManager.java:784) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:784) at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:757) at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:706) at javax.swing.RepaintManager.access$1000(RepaintManager.java:62) at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1647) at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733) at java.awt.EventQueue.access$200(EventQueue.java:103) at java.awt.EventQueue$3.run(EventQueue.java:694) at java.awt.EventQueue$3.run(EventQueue.java:692) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:703) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138) at java.awt.EventDispatchThread.run(EventDispatchThread.java:91) 
2
  • You still haven't told us what line throws the exception. What line is engine.java:51, line 51 of your class? Commented Jun 2, 2014 at 0:01
  • line 51 is spells[k][1] = image.getSubimage((34*2*k)-34*2,0,34,34); Commented Jun 2, 2014 at 0:21

1 Answer 1

7

A BufferedImage is an Image, in other words it extends from the Image abstract class. You don't need to convert anything.

Please check out the BufferedImage API for the details.


Edit
You state in comment:

When I run this code, I get spammed with errors in my console. Why would it do this? When the java file is converted into a class it has no errors found.

Then you'll want to post any error/exception messages that you receive, and also indicate for us which line(s) throw them.

Also as an aside, you will never want to read in an image from within a paintComponent method. This method should be lean, mean, and fast as hell, and it should concern itself with painting and painting only. Why read in the image multiple times when you only need to read it into a variable once say in the constructor?


Edit 2
Your error is a NullPointerException that occurs on engine.java:51, on line 51 of your engine.java class. Note that this has nothing to do with use of BufferedImage vs. Image, and everything to do with trying to use a null reference variable. As I suspected, the key to the problem is in the exception message. In your future questions, you will want to post any and all exception messages and indicate which line throws them.


Edit 3
NEVER do this:

try{ image = ImageIO.read(new File("spells.png")); } catch(IOException e) { } 

never leave your catch block blank. At least print the stack trace. Otherwise you will have no clue if something wrong occurs.


Edit 4
You state in comment:

Note on your edit: when declarations of the spritesheet and the sprites are put outside the paintComponent method. 20 parts of the code have unnecessary errors at variables and sometimes curly brackets saying "engine.java:<line>: class, interface, or enum expected"

From your error, I'm guessing that you've got code that calls methods, sitting out naked in the class outside of any constructor or method, where it doesn't belong. I suggest that you declare your Image variable in the class, and initialize it in your constructor (which right now is empty).

And again, you need to indicate for us which line is throwing the exception.


Edit 5
You state in comment:

line 51 is spells[k][1] = image.getSubimage((34*2*k)-34*2,0,34,34);

And this is as I guessed in my last comment. This means that image is null, that your attempt to read it in is wrong. Often this is due to looking for the file in the wrong place, or if in a jar file, trying to use a file in the first place. You will want to consider getting the Image as a resource, not a file.

To debug this, try to simplify your program -- just create a very small program that reads in an Image, creates an ImageIcon from it, and displays the ImageIcon in a JOptionPane.showMessageDialog(null, yourIconHere).


Edit 6
For example, try testing your code with something like this:

import java.awt.image.BufferedImage; import java.io.*; import javax.imageio.ImageIO; import javax.swing.*; public class SimpleTestImage { public static void main(String[] args) { String path = "spells.png"; InputStream inputStream = SimpleTestImage.class.getResourceAsStream(path); try { BufferedImage img = ImageIO.read(inputStream); ImageIcon icon = new ImageIcon(img); JOptionPane.showMessageDialog(null, icon); } catch (IOException e) { e.printStackTrace(); } } } 

In this example, I don't use a File to get the Image but rather get an InputStream as a class resource. When doing this, Java will start looking for the image in the same location that you have your class files.

And again, read in the Image in your class's constructor.

Sign up to request clarification or add additional context in comments.

19 Comments

When I run this code, I get spammed with errors in my console. Why would it do this? When the java file is converted into a class it has no errors found.
@user3502615: good, and also indicate which lines throw them. Please see edit to answer.
Note on your edit: when declarations of the spritesheet and the sprites are put outside the paintComponent method. 20 parts of the code have unnecessary errors at variables and sometimes curly brackets saying "engine.java:<line>: class, interface, or enum expected".
@user3502615: then you're doing it wrong. Show us how you try to do this, but post any new code to the bottom of your original question by editing your question. Also, please see Edit 3 to my answer.
@user3502615: Please see Edit 4 to answer, and again, you need to indicate for us which line is throwing the exception.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.