Results 1 to 5 of 5
  1. #1
    JDCAce's Avatar
    JDCAce is offline Member
    Join Date
    Oct 2008
    Location
    Columbus, GA, US
    Posts
    29
    Rep Power
    0

    Exclamation Error With Simply Displaying Picture

    I have no clue what's wrong here. I'm starting to learn about games in Java, and I'm reading a free online tutorial. It starts with applets. On the first page, the writer tells us how to display pictures, but, for some reason, my picture isn't displaying. I've attached my code, along with a picture of the working applet. (In case it's pertinent, I'm using NetBeans.)

    Java Code:
    package game;
    
    import java.applet.*;
    import java.awt.*;
    
    public class HelloWorld extends Applet
    {
      // I recieve a "warning" unless I have the following line.
      private final static long serialVersionUID = 42L;
      private Font comicSansMS;
      private Image katsucon;
      
      public void init()
      {
        katsucon = getImage(getDocumentBase(), "katsucon.jpg");
        setFont(new Font("Comic Sans MS", Font.BOLD, 30));
      }
      
      public void paint(Graphics g)
      {
        g.setColor(Color.GREEN);
        g.drawString("Hello, World!", 50, 50);
        
        g.drawImage(katsucon, 0, 0, this);
      }
    }
    Dang. I can't post links or images yet. Well, I'll describe the output. The program runs fine. "Hello, World!" in green, 30-point Comic Sans MS even appears, but it appears on a white background. My picture, katsucon, is nowhere to be seen. (Yes, katsucon.jpg is in my source folder.)

    What's wrong and how do I fix it?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    NetBeans, you say? Clean and Build the project and try again.

    Let us know if that doesn't do the trick.

    db

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default

    Is there an error message in the console?
    How are you executing the applet? In a browser or via the appletviewer.

  4. #4
    JDCAce's Avatar
    JDCAce is offline Member
    Join Date
    Oct 2008
    Location
    Columbus, GA, US
    Posts
    29
    Rep Power
    0

    Default

    Darryl: I pressed "Clean and Build Main Project" (Shift+F11) under the Build menu, but that didn't fix anything. (I assume that option is what you meant. I don't know what "cleaning" the project means.)

    Norm: There is no error message. I am running it via the AppletViewer, I believe. (I'm right clicking the file icon and selecting "Run File" [Shift+F6].) I'm not sure how to run it via a browser. I tried once, but I wasn't sure where to put the .html file; should it be in the same folder as the .class or the .java (NetBeans puts them in different folders)?

    EDIT: I just found an HTML file for the HelloWorld applet. Apparently NetBeans automatically creates one. I opened it, and I received the same display as the AppletViewer: Green "Hello, World!" with white background (no picture).
    Last edited by JDCAce; 10-07-2008 at 07:47 PM. Reason: Addition
    "Things are made of littler things that jiggle."

  5. #5
    JDCAce's Avatar
    JDCAce is offline Member
    Join Date
    Oct 2008
    Location
    Columbus, GA, US
    Posts
    29
    Rep Power
    0

    Default

    Woo! I did it! It turns out, first I needed to change getDocumentBase() to getCodeBase(), then I needed to move the JPEG file from NetBeansProjects\Game\src to NetBeansProjects\Game\build\classes. Apparently I wasn't pointing to the picture correctly at first.
    Last edited by JDCAce; 10-07-2008 at 08:09 PM. Reason: Easier to Read
    "Things are made of littler things that jiggle."

Similar Threads

  1. Setting a picture on a button
    By tomhcole in forum New To Java
    Replies: 2
    Last Post: 09-26-2008, 03:30 AM
  2. Picture help
    By deathnote11 in forum AWT / Swing
    Replies: 13
    Last Post: 06-03-2008, 05:46 PM
  3. displaying error messages differently
    By Jothi in forum JavaServer Faces (JSF)
    Replies: 0
    Last Post: 05-20-2008, 01:22 PM
  4. Print a picture file
    By oli001 in forum New To Java
    Replies: 0
    Last Post: 11-26-2007, 01:40 PM
  5. Simply output the result to a text file.
    By silvia in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:48 AM

Posting Permissions

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