Results 1 to 6 of 6

Thread: Need Help with Importing Image

  1. #1

    Thread Starter
    Lively Member fundean's Avatar
    Join Date
    Apr 2001
    Posts
    98

    Need Help with Importing Image

    assume an image called "die_no_3.jpg", how does one display this image within an applet if a randomly generated number yields the integer 3?

    thanks

  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: Need Help with Importing Image

    Perhaps store all of the images within an Image[]. Then use the random number as the index.

  3. #3
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: Need Help with Importing Image

    The random numbers would have to be in the range of the array of course. 0-array.length -1

  4. #4
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: Need Help with Importing Image

    Each time i refresh firefox i get a new image.
    Code:
     import java.io.*; 
     import java.awt.*; 
     import java.util.*;
     import javax.swing.*; 
    
     public class AppletEx extends JApplet{
      private Image image; 
      private String[] filenames; 
      private File imgdir; 
      private int dirlength; 
      private Random r;
      private int imageindex;
      
      public void init(){
       imgdir = new File("C:\\Pics");
       filenames = imgdir.list(); 
       dirlength = filenames.length; 
       r = new Random(); 
       imageindex = r.nextInt(dirlength);
       image = Toolkit.getDefaultToolkit().getImage(imgdir.getPath() + "//" + filenames[imageindex]); 
      }
      public void paint(Graphics gfx){
       gfx.drawImage(image,50,50,this);
      }
     }

  5. #5
    Frenzied Member vbNeo's Avatar
    Join Date
    May 2002
    Location
    Jutland, Denmark
    Posts
    1,994

    Re: Need Help with Importing Image

    Quote Originally Posted by Dilenger4
    Each time i refresh firefox i get a new image.
    Code:
     import java.io.*; 
     import java.awt.*; 
     import java.util.*;
     import javax.swing.*; 
    
     public class AppletEx extends JApplet{
      private Image image; 
      private String[] filenames; 
      private File imgdir; 
      private int dirlength; 
      private Random r;
      private int imageindex;
      
      public void init(){
       imgdir = new File("C:\\Pics");
       filenames = imgdir.list(); 
       dirlength = filenames.length; 
       r = new Random(); 
       imageindex = r.nextInt(dirlength);
       image = Toolkit.getDefaultToolkit().getImage(imgdir.getPath() + "//" + filenames[imageindex]); 
      }
      public void paint(Graphics gfx){
       gfx.drawImage(image,50,50,this);
      }
     }
    Out of topic, but shouldn't it be PIV and not PIIII in your sig?
    "Lies, sanctions, and cruise missiles have never created a free and just society. Only everyday people can do that."
    - Zack de la Rocha


    Hear me roar.

  6. #6

Posting Permissions

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



Click Here to Expand Forum to Full Width