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 8 of 8

Thread: Cant figure out why images in applet wont how

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Cant figure out why images in applet wont how

    So I am trying to make this java applet that will display a random image of three that is a sub folder of my src folder within my project.

    When you run the project currentlt all you get is a white applet. Here is the code:

    import java.applet.Applet;
    import java. awt.*;
     
    public class myPictures extends Applet {
    	Image action;
     
    	public void paint(Graphics g){
     
    int rint = (int)(Math.random() * 3);
    if(rint == 0){
    	action = getImage(getDocumentBase(), "../../images/bee3.jpg");
     
    } else if(rint == 1){
    	action = getImage(getDocumentBase(), "../../images/bee1.jpg");
    } else {
        action = getImage(getDocumentBase(), "../../images/bee2.jpg");
    }
    }
     
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,803
    My Mood
    Mellow
    Thanks
    236
    Thanked 717 Times in 703 Posts

    Default Re: Cant figure out why images in applet wont how

    Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.

    Are you using an IDE? Typically, these problems plague new users (and old) until they figure out how to reliably load the files and images they know are there. Then, it'll work fine in the IDE but won't work from the command line or as a .jar file. Start with a simple program that loads one image and displays it on an Applet or some other graphics object that you know you've gotten right. When you've gotten that figured out, you can apply that to other similar programs.

    I also don't see where you're adding the image to the graphics object; for example, a statement using the Graphics.showImage() method.

    Update: Oh, and there's no reason to reload the images every time through the paint() method. Load them once and display the desired one according to logic in or outside (preferred) the paint() method.

  3. #3
    Junior Member
    Join Date
    Aug 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Cant figure out why images in applet wont how

    Can you show me an example of that (loading the images once)? On another note, i left out a part of the code the first time so here it is in its entirety. Also I am using an IDE (eclipse). Thanks for your help in advance.

    Also the println debugging doesnt show ny numbers of rint.

    import java.applet.Applet;
    import java. awt.*;
     
    public class myPictures extends Applet {
    Image action;
     
    public void start(){
    int rint = (int)(Math.random() * 2);
    System.out.println("var=" + rint);
    if(rint == 0){
    	System.out.println("var=" + rint);
        action = getImage(getDocumentBase(), "images/bee1.jpg");
     
    } else if(rint == 1){
    	System.out.println("var=" + rint);
        action = getImage(getDocumentBase(), "images/bee2.jpg");
    } else {
    	System.out.println("var=" + rint);
        action = getImage(getDocumentBase(), "images/bee3.jpg");
    }
    }
        public void paint(Graphics graph) {
        	if(action != null){
         graph.drawImage(action, 10, 50, this);
         graph.drawString("The Forest of Bent Tress", 475, 30);
         resize(1200,900);
        }
        }
    }

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,803
    My Mood
    Mellow
    Thanks
    236
    Thanked 717 Times in 703 Posts

    Default Re: Cant figure out why images in applet wont how

    I also use Eclipase and create directories in my projects to hold images and data files. When I'm simply coding something in a hurry, working out the first steps of locating the files needed for my project, I'll initially code something like this:
    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
     
    import javax.imageio.ImageIO;
    import javax.swing.JApplet;
     
    public class TestClass extends JApplet
    {
        BufferedImage action;
     
        // initialize the applet
        public void init()
        {
            try
            {
                // load the image file to the buffered image from the
                // files directory off the project folder
                action = ImageIO.read( new File( "../files/ArrowLeft.png" ) );
            }
            catch ( IOException e )
            {
                // or print an error
                e.printStackTrace();
            }
     
        } // end method init()
     
        // method paint() draws the applet's graphics area 
        @Override
        public void paint(Graphics g)
        {
            g.drawImage(action, 10, 50, this);
            g.drawString("The Forest of Bent Tress", 475, 30);
     
        } // end method paint()
     
    } // end class TestClass
    The above runs correctly with the source code in the /src directory of the default package of some project with the image file in another directly called /files which hangs off the project directory at the same level as the /src folder. Play around with this simplified code, substituting your own path and image file name for what I've done until you get it right.

  5. #5
    Junior Member
    Join Date
    Aug 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Cant figure out why images in applet wont how

    Thanks

    --- Update ---

    If I highlight the src folder in my project then use the drop down arrow from the "New" toolbar item to add a folder to the src folder, I don't get an icon that looks like a folder, i get an image that is a square with a cross in it. Plus I cant get your code to work.

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,803
    My Mood
    Mellow
    Thanks
    236
    Thanked 717 Times in 703 Posts

    Default Re: Cant figure out why images in applet wont how

    The folder is not created as a child of /src but a child or subfolder of the project to which /src belongs. Right click on the project name in the Project Explorer, select New, Folder, then a dialog appears that lets you select the parent (the project you right clicked on should be highlighted, but you could change it), give the folder a name (like files), select finish. Put your image files in that folder.
    Plus I cant get your code to work.
    Don't know what that means.

  7. #7
    Junior Member
    Join Date
    Aug 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Cant figure out why images in applet wont how

    If I highlight the src folder in my project then use the drop down arrow from the "New" toolbar item to add a folder to the src folder, I don't get an icon that looks like a folder, i get an image that is a square with a cross in it. Or am I adding the wrong type of folder? Plus I cant get your code to work.

    --- Update ---

    Finally got it. Thanks

  8. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,803
    My Mood
    Mellow
    Thanks
    236
    Thanked 717 Times in 703 Posts

    Default Re: Cant figure out why images in applet wont how

    You're welcome. This will continue to frustrate you as you explore the other cases I mentioned. Don't give up, you'll figure those out too.

Similar Threads

  1. [SOLVED] Putting images in GUI. Images in source file dont seem to be recognised.
    By nikolaiL in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 8th, 2014, 02:44 PM
  2. Replies: 29
    Last Post: May 18th, 2012, 02:16 PM
  3. Trying to make 3 images become random in an Applet. Need help
    By slahsdash in forum What's Wrong With My Code?
    Replies: 20
    Last Post: March 9th, 2012, 08:39 PM
  4. Replies: 2
    Last Post: February 24th, 2012, 06:56 PM
  5. I need help with my ohm's law applet code. can't figure it out!
    By dracula2001 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 19th, 2010, 05:16 PM