Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (577)
games submitted by our members
Games in WIP (498)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  BufferedImage not drawing with g.drawImage()  (Read 3237 times)
0 Members and 1 Guest are viewing this topic.
Offline Jhagor

Junior Newbie





« Posted 2012-10-21 05:10:40 »

Okay, so I've been looking over this for about 2 hours and I simply cannot find out what's wrong.  Here's the code for both of my class files:

Skeleton.class
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
import javax.swing.*;

public class Skeleton {
   
   public static void main(String[] args) {
      JFrame frame = new JFrame("Map Editor");
      frame.setVisible(true);
      frame.setSize(800, 600);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setLocationRelativeTo(null);
      Draw object = new Draw();
      frame.add(object);
      object.drawing();
   }
   
}


Draw.class
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;

public class Draw extends JPanel {
   private BufferedImage walkable;
   
   public void drawing() {
      System.out.println("good");
      BufferedImage walkable = null;
      try {
         walkable = ImageIO.read(new File("Sprites/Animated.png"));
      } catch(IOException e) {
         System.out.println("failed");
      }
      System.out.println(walkable.getWidth());
      repaint();
   }
   
   public void paintComponent(Graphics g) {
      super.paintComponent(g);
      g.drawImage(walkable, 0, 0, this);
      g.setColor(Color.red);
      g.drawRect(10, 10, 10, 10);
   }
}


As you can see, I have some System.out.println()'s scattered throughout the code.  No exception is thrown when the image is loaded with ImageIO.read(), and when I query the width of the image after loading it is 160 which is correct.  The red rectangle also draws, but the image just will not.  I'm sure I'm missing something silly, but after looking over it so many times, I'm at a dead end.  Any suggestions?

Edit:  If it helps, here is a screenshot of the program running with the command prompt and debug outputs.
https://www.dropbox.com/s/s89n9ud3z0uwv7f/test.png
Offline Agro
« Reply #1 - Posted 2012-10-21 05:12:55 »

Rename your function to paint(), not paintComponent().

public void paint(Graphics g) {
      super.paint(g);
      g.drawImage(walkable, 0, 0, this);
      g.setColor(Color.red);
      g.drawRect(10, 10, 10, 10);
      g.dispose();
   }

Offline Jhagor

Junior Newbie





« Reply #2 - Posted 2012-10-21 05:17:00 »

Rename your function to paint(), not paintComponent().

public void paint(Graphics g) {
      super.paint(g);
      g.drawImage(walkable, 0, 0, this);
      g.setColor(Color.red);
      g.drawRect(10, 10, 10, 10);
      g.dispose();
   }

Gave the same output as before. Sad
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Agro
« Reply #3 - Posted 2012-10-21 05:21:00 »

Are you making sure the image is in the directory?

Offline Jhagor

Junior Newbie





« Reply #4 - Posted 2012-10-21 05:23:11 »

Are you making sure the image is in the directory?

Yes.  The image is loaded fine, the catch on ImageIO.read() gives no debug, and when I query the width of the image it returns the correct value.  Look in the code and you will see a System.out for the catch on the try event, and another for the width of the image.  In the screenshot I linked, you can see the results in the command prompt.
Offline Agro
« Reply #5 - Posted 2012-10-21 05:25:21 »

Well, this might also be a problem with the image itself, so try to test with other BMP/PNGs.

Offline Jhagor

Junior Newbie





« Reply #6 - Posted 2012-10-21 05:26:46 »

Well, this might also be a problem with the image itself, so try to test with other BMP/PNGs.

Tested with 3 different ones so far.  I will try with a plain black image from MS Paint, but I doubt I get results.

Edit:  No go.  Here's the results with the plain image.  You can see the application, CMD Promt and the image in the screenshot.
https://www.dropbox.com/s/5saok2amw15bdz1/test2.png
Offline Jhagor

Junior Newbie





« Reply #7 - Posted 2012-10-21 07:05:09 »

Okay, so the problem still persist.  I guess I just need to give it a rest for a while, and come at it fresh ... unless I get some miracle advice from a fellow user. Sad
Offline ctomni231

JGO Ninja


Medals: 71
Projects: 1
Exp: 7 years


Not a glitch. Just have a lil' pixelexia...


« Reply #8 - Posted 2012-10-21 08:17:48 »

I strictly use ImageIO only for saving images in other formats. You can use it for pictures, but you have to connect a MediaTracker to it so it can actually load the Image. So, to save you the headache I'll give you a shorter solution that will help you get the image onto the screen.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
import java.awt.*;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class Draw extends JPanel {
   private Image walkable;
   
   public void drawing() {
      System.out.println("good");
      ImageIcon icon = new ImageIcon("Sprites/Animated.png");
      walkable = icon.getImage();
      repaint();
   }
   
   public void paint(Graphics g) {
      g.drawImage(walkable, 0, 0, this);
      g.setColor(Color.red);
      g.drawRect(10, 10, 10, 10);
   }
}

Offline nsigma
« Reply #9 - Posted 2012-10-21 11:13:27 »

Try removing line 13. You're creating a new local walkable variable and setting that rather than the field you mean!

You should also be creating your UI on the Swing event thread. Might be another cause of issues. Wrap all your code in main() inside a Runnable and pass to EventQueue.invokeLater()

Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Jhagor

Junior Newbie





« Reply #10 - Posted 2012-10-21 15:36:21 »

I strictly use ImageIO only for saving images in other formats. You can use it for pictures, but you have to connect a MediaTracker to it so it can actually load the Image. So, to save you the headache I'll give you a shorter solution that will help you get the image onto the screen.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
import java.awt.*;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class Draw extends JPanel {
   private Image walkable;
   
   public void drawing() {
      System.out.println("good");
      ImageIcon icon = new ImageIcon("Sprites/Animated.png");
      walkable = icon.getImage();
      repaint();
   }
   
   public void paint(Graphics g) {
      g.drawImage(walkable, 0, 0, this);
      g.setColor(Color.red);
      g.drawRect(10, 10, 10, 10);
   }
}


This works.  Thanks a lot. Smiley  I can now proceed with my project.
Offline Jhagor

Junior Newbie





« Reply #11 - Posted 2012-10-21 15:41:17 »

Try removing line 13. You're creating a new local walkable variable and setting that rather than the field you mean!

You should also be creating your UI on the Swing event thread. Might be another cause of issues. Wrap all your code in main() inside a Runnable and pass to EventQueue.invokeLater()

This also works!  I don't know how I missed that.  Anyways, I will throw the main function in the Swing event thread as well.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 605
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #12 - Posted 2012-10-21 17:50:15 »

Rename your function to paint(), not paintComponent().
Please don't do this. It will break Swing functionality like double-buffering.


I strictly use ImageIO only for saving images in other formats. You can use it for pictures, but you have to connect a MediaTracker to it so it can actually load the Image.
ImageIO is a blocking API, meaning that after load(...) returns, the image will be fully loaded. Using a MediaTracker on it makes no sense.

Jhagor had perfectly fine code to start with (except line 13) and it's sad to see how the advice here (except from nsigma) made it worse.


Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (20 views)
2014-04-15 18:08:23

BurntPizza (16 views)
2014-04-15 03:46:01

UprightPath (29 views)
2014-04-14 17:39:50

UprightPath (14 views)
2014-04-14 17:35:47

Porlus (30 views)
2014-04-14 15:48:38

tom_mai78101 (55 views)
2014-04-10 04:04:31

BurntPizza (112 views)
2014-04-08 23:06:04

tom_mai78101 (212 views)
2014-04-05 13:34:39

trollwarrior1 (181 views)
2014-04-04 12:06:45

CJLetsGame (187 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!