Page 1 of 2 12 LastLast
Results 1 to 20 of 25

Thread: Image troubles

  1. #1
    Theodoreb is offline Member
    Join Date
    Jul 2009
    Posts
    15
    Rep Power
    0

    Question Image troubles

    Hello. I am relatively new to Java, but I have learned quickly and now I have decent experience. However (this is a little embarrassing) I can't seem to display a gif or jpg image in an applet I am designing. In fact I cant display any type of image at all. I am attempting to rotate an image at the users will and a click will send a projectile in the mouses direction, but no matter what code I use, no image appears in the applet. Can anyone help me?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    First off, if you are using AWT (Applet, Panel, Canvas, etc...), I recommend that you switch to Swing (JApplet, JPanel, JComponent, etc...).

    Next off, how are you currently trying to display a picture? Have you tried trouble shooting your problems? Is it a problem with reading the image file (a common problem with applets since you don't use files but instead resources when uploading data), or is the image file uploading fine and your problem is with displaying it? You may wish to post some code here, but if you do, please use code tags so that the code displays properly.

    Oh, and by the way, good luck and welcome to the forum!

  3. #3
    Theodoreb is offline Member
    Join Date
    Jul 2009
    Posts
    15
    Rep Power
    0

    Default

    Thanks :)
    Here is one of the many codes I tried:
    Java Code:
    import java.awt.*; 
    import java.applet.*; 
    import java.net.*; 
    
    public class ImageExample extends Applet 
    { 
         Image my_gif; 
     
         URL base; 
    
         MediaTracker mt; 
    
         public void init()  
         { 
    
              mt = new MediaTracker(this);  
    
             try {  
                   base = getDocumentBase(); 
             } 
             catch (Exception e) {} 
     
              my_gif = getImage(base,"gumby.gif"); 
    
              mt.addImage(my_gif,1); 
    
             try { 
                   mt.waitForAll(); 
              } 
              catch (InterruptedException  e) {} 
    
    
         } 
    
         public void paint(Graphics g)  
         {  
    
              g.drawImage(my_gif,20,20,this); 
    
    
              g.drawImage(my_gif,20,140,30,40,this); 
      
    
         } 
    
    }
    And it wouldn't show an image; the applet would show up blank.
    Am I missing something really obvious?
    Last edited by Theodoreb; 07-11-2009 at 11:53 PM. Reason: Code tags placed

  4. #4
    Theodoreb is offline Member
    Join Date
    Jul 2009
    Posts
    15
    Rep Power
    0

    Default

    Oh yes and I am trying to make this appear in an applet, so when I finish it, it can be viewed in a web browser.

  5. #5
    Theodoreb is offline Member
    Join Date
    Jul 2009
    Posts
    15
    Rep Power
    0

    Default

    And finally I'm sorry I didn't use code tags. I'm new to the forum-thing. (How do you write code in code tags anyway?)
    Sorry :(

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Theodoreb View Post
    And finally I'm sorry I didn't use code tags. I'm new to the forum-thing. (How do you write code in code tags anyway?)
    Sorry :(
    You place [code] above your code block and [/code] below it. I have placed this in your post, and if you edit your post, you'll see exactly where and how they were placed.

  7. #7
    Theodoreb is offline Member
    Join Date
    Jul 2009
    Posts
    15
    Rep Power
    0

    Default

    Thanks again :)
    Was there something wrong with my code? There were no errors in my IDE, but when I ran the code the applet window appeared entirely blank. At the bottom left corner of the applet, it said "applet not initialized." Does that have any significance?

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I'd try again to use a Swing JApplet and would read in the image using ImageIO.read(...). If I want the image to be a background image in the JApplet, I'd paint it in the paintComponent method of a JPanel and then place that JPanel into the JApplet's contentPane:

    Java Code:
    import java.awt.*;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    public class ImageExample2 extends JApplet {
    
      private static final String IMAGE_PATH = "gumby.gif";
    
      @Override
      public void init() {
        Image image;
        try {
          image = ImageIO.read(getClass().getResourceAsStream(IMAGE_PATH));
          DrawPanel drawPanel = new DrawPanel(image);
          getContentPane().add(drawPanel);
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
    
    class DrawPanel extends JPanel {
      private Image image;
      public DrawPanel(Image image) {
        this.image = image;
      }
      
      @Override
      protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (image != null) {
          g.drawImage(image, 0, 0, this);
        }
      }
    }

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Theodoreb View Post
    Thanks again :)
    Was there something wrong with my code? There were no errors in my IDE, but when I ran the code the applet window appeared entirely blank. At the bottom left corner of the applet, it said "applet not initialized." Does that have any significance?
    Yes, you're applet is likely not finding the image, but you'll never know as you're throwing out your exceptions:
    Java Code:
    catch (Exception e) {}
    Yikes. That is something you should almost never do

  10. #10
    Theodoreb is offline Member
    Join Date
    Jul 2009
    Posts
    15
    Rep Power
    0

    Default

    Using your code still brings a blank applet up... I don't think theres anything wrong with the code. It must be something else. I heard something about having the image in a specific directory...

  11. #11
    Theodoreb is offline Member
    Join Date
    Jul 2009
    Posts
    15
    Rep Power
    0

    Default

    Is it something to do with the fact that I'm making an applet and not an application?

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Yes, where exactly is your image in relationship to your class files? Are your class files and image in a JAR?

  13. #13
    Theodoreb is offline Member
    Join Date
    Jul 2009
    Posts
    15
    Rep Power
    0

    Default

    No... ;) The picture file is in the My Pictures folder.

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Theodoreb View Post
    No... ;) The picture file is in the My Pictures folder.
    That doesn't tell us much. Please answer these questions:
    Where is your pictures folder in relation to your class files, are they related at all?
    Can you show the path of your class files?
    Can you show the path of your pictures?
    Have you JAR'd the applet?
    Do you plan to?

    my suggestion: create an images directory off of the directory that holds your program's class files. Then using system's resources, you can refer to your image via an image path string like so (see my sample program and change):
    Java Code:
    private static final String IMAGE_PATH = "images/gumby.gif";

    Then if you JAR this program, make sure to include your image as part of the resources.
    Last edited by Fubarable; 07-12-2009 at 11:01 PM. Reason: forum software is sticking in Strings where they don't belong

  15. #15
    Theodoreb is offline Member
    Join Date
    Jul 2009
    Posts
    15
    Rep Power
    0

    Default

    I haven't JAR'd the applet. How do you do that?
    Also, would creating a folder called "Images" in the src folder of the applet before JARing work? (And than you'd put the needed image in the Images folder)

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    One thing at a time. Please answer the questions if you can as they can help us figure this whole thing out.
    Last edited by Fubarable; 07-12-2009 at 11:22 PM.

  17. #17
    Theodoreb is offline Member
    Join Date
    Jul 2009
    Posts
    15
    Rep Power
    0

    Default

    The image and the coding are not related in any way, aside from the fact that the code has the name of the image in it.
    The image path is: C:\Documents and Settings\Theodore\My Documents\My Pictures
    I have no idea how to JAR my applet, so no I have not JAR'd it.

  18. #18
    Theodoreb is offline Member
    Join Date
    Jul 2009
    Posts
    15
    Rep Power
    0

    Default

    Oh sorry I forgot the class file path. Here it is:
    C:\Documents and Settings\Theodore\My Documents\JCreator Pro\MyProjects\ShootTheTarget

  19. #19
    Theodoreb is offline Member
    Join Date
    Jul 2009
    Posts
    15
    Rep Power
    0

    Default

    Should I include the image path in my code?
    If so, why? No other code I've seen that had images had any specific paths.
    (And how?)

  20. #20
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I stand by my recommendations in post #14. Please give it a try.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 3
    Last Post: 01-29-2010, 07:53 AM
  2. Gif decoding/LZW troubles
    By hellochar in forum Advanced Java
    Replies: 2
    Last Post: 07-15-2009, 12:26 AM
  3. subclass troubles
    By xf021209 in forum New To Java
    Replies: 12
    Last Post: 04-21-2009, 12:46 AM
  4. [SOLVED] Array troubles, yes I searched...
    By Reiyn in forum New To Java
    Replies: 11
    Last Post: 04-17-2009, 12:28 AM
  5. StreamCorruptedException and Casting troubles
    By Wassa in forum Networking
    Replies: 2
    Last Post: 02-18-2009, 04:07 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •