Results 1 to 3 of 3
  1. #1
    cecily is offline Member
    Join Date
    Jul 2007
    Posts
    33
    Rep Power
    0

    Default how to import a picture into an applet

    I can't really get the way to import a picture into an applet, I don't need to do anything specific with it only import it into the applet
    help please

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    If you mean image loading here are four options available in applets.
    Java Code:
    //  <applet code="AppletImageLoading" width="400" height="400"></applet>
    //  prompt>appletviewer AppletImageLoading.java
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.net.URL;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    public class AppletImageLoading extends JApplet {
        public void init() {
            Image[] images = loadImages();
            JPanel panel = new JPanel(new GridLayout(0,2));
            for(int j = 0; j < images.length; j++)
                panel.add(new JLabel(new ImageIcon(images[j])));
            getContentPane().add(new JScrollPane(panel));
        }
    
        private Image[] loadImages() {
            Image[] images = new Image[4];
            String path = "images/Bird.gif";
            // getResource looks for image on class path.
            URL url = getClass().getResource(path);
            // Use Applet method.
            images[0] = getImage(url);
            loadImage(images[0]);
            // Use Toolkit method.
            images[1] = getToolkit().createImage(url);
            loadImage(images[1]);
            // Use ImageIcon.
            ImageIcon icon = new ImageIcon(url);
            showLoadingStatus(icon.getImageLoadStatus());
            images[2] = icon.getImage();
            // Use ImageIO read method.
            try {
                images[3] = ImageIO.read(url);
            } catch(IOException e) {
                System.out.println("Read error: " + e.getMessage());
            }
            return images;
        }
    
        private void loadImage(Image image) {
            MediaTracker tracker = new MediaTracker(this);
            tracker.addImage(image, 0);
            try {
                tracker.waitForID(0);
            } catch(InterruptedException e) {
                System.out.println("Loading interrupted");
            }
            showLoadingStatus(tracker.statusAll(false));
        }
    
        private void showLoadingStatus(int status) {
            String result = "";
            if((status & MediaTracker.COMPLETE) == MediaTracker.COMPLETE)
                result += "COMPLETE";
            if((status & MediaTracker.ABORTED) == MediaTracker.ABORTED)
                result += "ABORTED ";
            if((status & MediaTracker.ERRORED) == MediaTracker.ERRORED)
                result += "ERRORED";
            System.out.println("Image loading = " + result);
        }
    }

  3. #3
    sgware is offline Member
    Join Date
    Jan 2010
    Posts
    1
    Rep Power
    0

    Default

    There is an alternative method to accomplish this that I use in my applets.

    Instead of importing the images, convert them to .java files and then simply use them in your code.

    I made a tool for doing this, which makes the process a lot easier: ImageToClass

    Hope that helps :)
    -- Stephen

Similar Threads

  1. error with import java.io.*
    By osval in forum New To Java
    Replies: 5
    Last Post: 05-13-2010, 06:03 PM
  2. import statement.
    By diRisig in forum New To Java
    Replies: 2
    Last Post: 02-08-2008, 01:34 AM
  3. Picture in a JFrame problem
    By saytri in forum New To Java
    Replies: 3
    Last Post: 01-12-2008, 10:44 AM
  4. Print a picture file
    By oli001 in forum New To Java
    Replies: 0
    Last Post: 11-26-2007, 02:40 PM
  5. how to import jface in java swt
    By rajaletchumy in forum New To Java
    Replies: 1
    Last Post: 08-08-2007, 01:31 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
  •