Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 20 of 20

Thread: Help needed with Images.

  1. #1
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Help needed with Images.

    I'm trying to make a simple game, but I'm having trouble with my Image not appearing.

    (Im going to make a game loop, but this is just a test)

    gameMain.class
     
    public class gameMain {
    	public static void main(String[] args){
    		chatBox chatBox = new chatBox();
    		gui gui = new gui();
    		gui.updateScreen();
    	}
    }

    gui.class
    import java.awt.*;
    import javax.swing.*;
     
    public class gui extends JPanel {
    	Image img;
     
    	public gui(){
    		JFrame f = new JFrame("BrettScape");
    		f.setSize(640,480);
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		f.setVisible(true);
    		img = Toolkit.getDefaultToolkit().getImage("testtile.png"); 
    	}
     
            public void updateScreen(){
    		repaint();
            }
     
         public void drawMessages(){
               //TODO
         }
     
         public void paintComponent(Graphics g){
    		super.paintComponent(g);
    		g.drawImage(img, 0, 0, null);
       }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help needed with Images.

    Try using the ImageIO class for reading the image.

    Is the image file being found by the program? What is the value of img after the getImage call?

  3. #3
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Help needed with Images.

    How do I check the value of img, and use ImageIO?

  4. #4

  5. #5
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Help needed with Images.

    I changed my gui class to the code below, but the drawing still doesn't show up.
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
     
    import javax.imageio.ImageIO;
    import javax.swing.*;
     
    public class gui extends JPanel {
    	BufferedImage img;
    	chatBox chatBox;
    	public gui(chatBox chatBox){
    		JFrame f = new JFrame("BrettScape");
    		f.setSize(640,480);
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		f.setVisible(true);
    		try {
    			img = ImageIO.read(new File("testtile.png"));
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
     
    	}
     
    	public void updateScreen(){
    		repaint();
    	}
     
    	public void paintComponent(Graphics g){
    		System.out.println("hey");
    		g.drawImage(img, 0, 0, null);
    	}
    }

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Help needed with Images.

    Some things to check:

    1. Is there any error information displayed (like file not found exceptions, or ioexceptions)? If so, please post them.

    2. Does the image contain something (for example, is it not just a white rectangle being drawn onto a white background)?

    3. Have you considered the size of the image (too small, too big)? This method doesn't properly scale the image to fit the region. There are other paint methods you can use to draw a scaled version of the image, or you can scale the image before-hand (either in code or externally)

    edit:

    as a side note, you don't want to put any print statements into the paintComponent method. This method gets called quite frequently, and if you have print statements, it can significantly reduce the performance of your application, as well as flooding the console and making any other console output pretty much useless.
    Last edited by helloworld922; August 16th, 2010 at 12:12 AM.

  7. #7
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Help needed with Images.

    1. No exceptions.
    2. the picture is 4tiles consiting of grass dirt water stone.
    3. the image is 96x64 the JFrame is 640x480

    side note. Where do I put it then?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help needed with Images.

    as a side note, you don't want to put any print statements into the paintComponent method.
    I disagree. For debugging one time, its useful to see when/if a method is being called. I use print statements a lot to verify that. Then remove them after the they've shown what is happening.

    If fact in this case the print shows what is wrong!!!
    The hey is printed before the image is loaded. If you add a print after img is given a value you will see this:
    hey
    img=BufferedImage@6bade9: type = 0 ColorModel: #pixelBits = 24 numComponents = 3 color space = java.awt.color.ICC_ColorSpace@166afb3 transparency = 1 has alpha = false isAlphaPre = false ByteInterleavedRaster: width = 1024 height = 537 #numDataElements 3 dataOff[0] = 0
    paint is called before the image is available.

    Move the setVisible to after you have the image, the image is shown and you'll see:
    img=BufferedImage@126f75b: type = 0 ColorModel: #pixelBits = 24 numComponents = 3 color space = java.awt.color.ICC_ColorSpace@139b78e transparency = 1 has alpha = false isAlphaPre = false ByteInterleavedRaster: width = 1024 height = 537 #numDataElements 3 dataOff[0] = 0
    hey
    Also you need to add the JPanel(this) to the frame.
    Last edited by Norm; August 16th, 2010 at 08:21 AM.

  9. #9
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Help needed with Images.

    I just remembered... For some reason my paintComponent method is NEVER called. ("hey" never prints in the terminal) I even put a while loop to keep the program running.
    while(true)
    		gui.updateScreen();

    Here is my current gui.class
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
     
    import javax.imageio.ImageIO;
    import javax.swing.*;
     
    public class gui extends JPanel {
    	BufferedImage img;
    	chatBox chatBox;
    	public gui(chatBox chatBox){
    		JFrame f = new JFrame("BrettScape");
    		f.setSize(640,480);
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		try {
    			img = ImageIO.read(new File("testtile.png"));
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    		f.setVisible(true);
     
    	}
     
    	public void updateScreen(){
    		repaint();
    	}
     
    	public void paintComponent(Graphics g){
    		g.drawImage(img, 0, 0, null);
    	}
    }
    Last edited by Brt93yoda; August 16th, 2010 at 12:29 PM.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help needed with Images.

    Did you read my last post? I pointed out several problems with your code and showed consoles where "hey" is printed.
    The code works with those changes.

    BTW This would show you a problem: System.out.println("hey img=" + img); // show contents of img variable

  11. #11
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Help needed with Images.

    I did, I changed the setVisible to after the image is loaded.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help needed with Images.

    Does it work now?

  13. #13
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Help needed with Images.

    no it does'nt

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help needed with Images.

    Ok go back and read my post#8 again.
    Did you do what I said on the last line of the post: you need to add the JPanel(this) to the frame.

  15. #15
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Help needed with Images.

    I typed
    j.add(JPanel(this))
    and I got an error. I'm sorry, but I'm very bad with images. I've looked at many different tutorials, but I just can't get it to work by myself.

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help needed with Images.

    I got an error
    Please copy and post the full text of the error message here.

    What was the code you posted supposed to do?
    What is the 'j' variable?

    Your problem has nothing to do with images. It has to do with adding a component to a container.
    Work your way thru the problem:
    What variable is the frame you have created? Its f
    How do you add a component to f? use its add() method
    What do you want to add to f?
    You want to add the JPanel that has the paintComponent method in it that will draw the image.
    That is the gui class object.
    How do you get a reference to the current object? its this
    So putting it all together: f.add(this); // add the gui object to the frame

  17. The Following User Says Thank You to Norm For This Useful Post:

    Brt93yoda (August 16th, 2010)

  18. #17
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Help needed with Images.

    That worked! Thank you so much!

    What is the 'j' variable?
    I meant f not j

  19. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help needed with Images.

    Its better to copy and paste from the actual program than to try to remember what the code was.

    Now that you have the code working, you need to rework it. Putting the JFrame code in the gui constructor doesn't make sense.

    For testing your gui class, add a main() method to it and move all the JFrame code out of the gui constructor to the main method.
    Leave the image code there because that belongs to the gui class.

    After testing, you can comment out the main() method.
    Last edited by Norm; August 16th, 2010 at 04:44 PM. Reason: Move code to main

  20. #19
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Help needed with Images.

    Quote Originally Posted by Norm View Post
    I disagree. For debugging one time, its useful to see when/if a method is being called. I use print statements a lot to verify that. Then remove them after the they've shown what is happening.
    true, I guess I tend to use debug tools more (breakpoints, watches, variables, stack traces, etc.).

  21. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help needed with Images.

    They're great if you have em.

Similar Threads

  1. Loading Images and Edit it
    By shadihrr in forum Java Theory & Questions
    Replies: 1
    Last Post: March 25th, 2010, 08:39 PM
  2. Converting Images to Shapes
    By Ian in forum Java Theory & Questions
    Replies: 2
    Last Post: February 7th, 2010, 05:18 PM
  3. Loading in images
    By eXcellion in forum Java Theory & Questions
    Replies: 3
    Last Post: January 17th, 2010, 12:13 PM
  4. Images not going in email
    By anjali09s in forum Java SE APIs
    Replies: 3
    Last Post: August 2nd, 2009, 06:06 PM
  5. What are the best way of placing images in GUI?
    By Ciwan in forum AWT / Java Swing
    Replies: 5
    Last Post: February 26th, 2009, 05:19 PM