Link to home
Start Free TrialLog in
Avatar of Kfkm09
Kfkm09Flag for United States of America

asked on

How do I load images in Java?

I'm having trouble loading images in this application version of an applet. The application is barely started, but please see the code I've provided below. Thanks!
/**
 * @author Me
 *
 * @version 1.2b
 */
//menu bar edit
package bin;//this is edit
 
import java.awt.*;
import java.awt.event.*;
 
public class FUI extends Frame {
    MediaTracker mTrack;
    Image icon1, img1, img2;
    int topWindowspace = 30;
    Rect oldColorRect = new Rect(40, 30 + topWindowspace, 90, 50);
    Rect newColorRect = new Rect(140, 30 + topWindowspace, 90, 50);
 
    /* default constructor */
    public FUI(String[] input) {
        super("Color Selector");
        setVisible(true);
        setSize(400, 250);
        mTrack = new MediaTracker(this);
        
        giComponentsInit();
        uIComponents();
        imageLoad();
        setIconImage(icon1);
 
        this.addWindowListener(new java.awt.event.WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent we){
                System.exit(0);
            }
        });
        System.out.println("Main Arguments:  " + input + "\n\n" + this);
    }
 
    public void giComponentsInit() {
        oldColorRect.setColor(0, 0, 0);
        newColorRect.setColor(200, 200, 200);
    }
 
    protected void uIComponents() {
        setLayout(null);
        /************* Initialization ***********/
        rPlus = new Button();
        rMinus = new Button();
        red = new TextField();
        gPlus = new Button();
        gMinus = new Button();
        green = new TextField();
        bPlus = new Button();
        bMinus = new Button();
        blue = new TextField();
        memButton = new Button();
        revertButton = new Button();
 
        /************* Object Messages ***********/
        rPlus.setLabel("+10");
        rPlus.addActionListener(new java.awt.event.ActionListener() {
            @Override public void actionPerformed(java.awt.event.ActionEvent evt) {
                rPlusActionPerformed(evt);
            }
        });
        add(rPlus);
        rPlus.setBounds(40, 100 + topWindowspace, 35, 20);
 
        rMinus.setLabel("-10");
        rMinus.addActionListener(new java.awt.event.ActionListener() {
            @Override public void actionPerformed(java.awt.event.ActionEvent evt) {
                rMinusActionPerformed(evt);
            }
        });
        add(rMinus);
        rMinus.setBounds(90, 100 + topWindowspace, 35, 20);
 
        red.setText("" + newColorRect.red);
        red.addActionListener(new java.awt.event.ActionListener() {
            @Override public void actionPerformed(java.awt.event.ActionEvent evt) {
                redActionPerformed(evt);
            }
        });
        add(red);
        red.setBounds(40, 140 + topWindowspace, 85, 25);
 
        gPlus.setLabel("+10");
        gPlus.addActionListener(new java.awt.event.ActionListener() {
            @Override public void actionPerformed(java.awt.event.ActionEvent evt) {
                gPlusActionPerformed(evt);
            }
        });
        add(gPlus);
        gPlus.setBounds(140, 100 + topWindowspace, 35, 20);
 
        gMinus.setLabel("-10");
        gMinus.addActionListener(new java.awt.event.ActionListener() {
            @Override public void actionPerformed(java.awt.event.ActionEvent evt) {
                gMinusActionPerformed(evt);
            }
        });
        add(gMinus);
        gMinus.setBounds(190, 100 + topWindowspace, 35, 20);
 
        green.setText("" + newColorRect.blue);
        green.addActionListener(new java.awt.event.ActionListener() {
            @Override public void actionPerformed(java.awt.event.ActionEvent evt) {
                greenActionPerformed(evt);
            }
        });
        add(green);
        green.setBounds(140, 140 + topWindowspace, 85, 25);
 
        bPlus.setLabel("+10");
        bPlus.addActionListener(new java.awt.event.ActionListener() {
            @Override public void actionPerformed(java.awt.event.ActionEvent evt) {
                bPlusActionPerformed(evt);
            }
        });
        add(bPlus);
        bPlus.setBounds(240, 100 + topWindowspace, 35, 20);
 
        bMinus.setLabel("-10");
        bMinus.addActionListener(new java.awt.event.ActionListener() {
            @Override public void actionPerformed(java.awt.event.ActionEvent evt) {
                bMinusActionPerformed(evt);
            }
        });
        add(bMinus);
        bMinus.setBounds(290, 100 + topWindowspace, 35, 20);
 
        blue.setText("" + newColorRect.blue);
        blue.addActionListener(new java.awt.event.ActionListener() {
            @Override public void actionPerformed(java.awt.event.ActionEvent evt) {
                blueActionPerformed(evt);
            }
        });
        add(blue);
        blue.setBounds(240, 140 + topWindowspace, 85, 25);
 
        memButton.setLabel("Save Color");
        memButton.addActionListener(new java.awt.event.ActionListener() {
            @Override public void actionPerformed(java.awt.event.ActionEvent evt) {
                memButtonActionPerformed(evt);
            }
        });
        add(memButton);
        memButton.setBounds(245, 25 + topWindowspace, 85, 28);
 
        revertButton.setLabel("Revert");
        revertButton.addActionListener(new java.awt.event.ActionListener() {
            @Override public void actionPerformed(java.awt.event.ActionEvent evt) {
                revertButtonActionPerformed(evt);
            }
        });
        add(revertButton);
        revertButton.setBounds(245, 55 + topWindowspace, 85, 28);
    }
 
    private void rPlusActionPerformed(java.awt.event.ActionEvent evt) {
        int varR = 0;
        int dispR;
        try{
            varR = Integer.parseInt(red.getText());
        }
        catch(Exception e) {
            varR = oldColorRect.red;
        }
        try {
            dispR = varR + 10;
            newColorRect.setR(varR + 10);
        } catch(Exception e) {
            dispR = varR;
            newColorRect.setR(varR);
        }
        red.setText("" + dispR);
        repaint();
    }
    private void rMinusActionPerformed(java.awt.event.ActionEvent evt) {
        int varR = 0;
        int dispR;
        try{
            varR = Integer.parseInt(red.getText());
        }
        catch(Exception e) {
            varR = newColorRect.red;
        }
        try{
            dispR = varR - 10;
            newColorRect.setR(varR - 10);
        } catch(Exception e) {
            dispR = varR;
            newColorRect.setR(varR);
        }
 
        red.setText("" + dispR);
        repaint();
    }
    private void redActionPerformed(java.awt.event.ActionEvent evt) {
        int varR = 0;
        try{
            varR = Integer.parseInt(red.getText());
            if(varR < 0) {
                varR = 0;
                red.setText("" + varR);
            }
            else if(varR > 255) {
                varR = 255;
                red.setText("" + varR);
            }
            else{}
        }
        catch(Exception e) {
        }
        newColorRect.setR(varR);
        repaint();
    }
    private void gPlusActionPerformed(java.awt.event.ActionEvent evt) {
        int varG = 0;
        int dispG;
        try{
            varG = Integer.parseInt(green.getText());
        }
        catch(Exception e) {
            varG = oldColorRect.green;
        }
        try {
            dispG = varG + 10;
            newColorRect.setG(varG + 10);
        } catch(Exception e) {
            dispG = varG;
            newColorRect.setG(varG);
        }
        green.setText("" + dispG);
        repaint();
    }
    private void gMinusActionPerformed(java.awt.event.ActionEvent evt) {
        int varG = 0;
        int dispG;
        try{
            varG = Integer.parseInt(green.getText());
        }
        catch(Exception e) {
            varG = oldColorRect.green;
        }
        try {
            dispG = varG - 10;
            newColorRect.setG(varG - 10);
        } catch(Exception e) {
            dispG = varG;
            newColorRect.setG(varG);
        }
        green.setText("" + dispG);
        repaint();
    }
    private void greenActionPerformed(java.awt.event.ActionEvent evt) {
        int varG = 0;
        try{
            varG = Integer.parseInt(green.getText());
            if(varG < 0) {
                varG = 0;
                green.setText("" + varG);
            }
            else if(varG > 255) {
                varG = 255;
                green.setText("" + varG);
            }
            else{}
        }
        catch(Exception e) {
        }
        newColorRect.setG(varG);
        repaint();
    }
    private void bPlusActionPerformed(java.awt.event.ActionEvent evt) {
        int varB = 0;
        int dispB;
        try{
            varB = Integer.parseInt(blue.getText());
        }
        catch(Exception e) {
            varB = oldColorRect.blue;
        }
        try {
            dispB = varB + 10;
            newColorRect.setB(varB + 10);
        } catch(Exception e) {
            dispB = varB;
            newColorRect.setB(varB);
        }
        blue.setText("" + dispB);
        repaint();
    }
    private void bMinusActionPerformed(java.awt.event.ActionEvent evt) {
        int varB = 0;
        int dispB;
        try{
            varB = Integer.parseInt(blue.getText());
        }
        catch(Exception e) {
            varB = oldColorRect.blue;
        }
        try {
            dispB = varB - 10;
            newColorRect.setB(varB - 10);
        } catch(Exception e) {
            dispB = varB;
            newColorRect.setB(varB);
        }
        blue.setText("" + dispB);
        repaint();
    }
    private void blueActionPerformed(java.awt.event.ActionEvent evt) {
        int varB = 0;
        try{
            varB = Integer.parseInt(blue.getText());
            if(varB < 0) {
                varB = 0;
                blue.setText("" + varB);
            }
            else if(varB > 255) {
                varB = 255;
                blue.setText("" + varB);
            }
            else{}
        }
        catch(Exception e) {
        }
        newColorRect.setB(varB);
        repaint();
    }
    private void memButtonActionPerformed(java.awt.event.ActionEvent evt) {
        oldColorRect.setR(newColorRect.red);
        oldColorRect.setG(newColorRect.green);
        oldColorRect.setB(newColorRect.blue);
        repaint();
    }
    private void revertButtonActionPerformed(java.awt.event.ActionEvent evt) {
        newColorRect.setR(oldColorRect.red);
        newColorRect.setG(oldColorRect.green);
        newColorRect.setB(oldColorRect.blue);
        red.setText("" + newColorRect.red);
        green.setText("" + newColorRect.green);
        blue.setText("" + newColorRect.blue);
        repaint();
    }
 
    /* **************** global varibles ************** */
 
    protected Button rPlus;
    protected Button rMinus;
    protected TextField red;
    protected Button gPlus;
    protected Button gMinus;
    protected TextField green;
    protected Button bPlus;
    protected Button bMinus;
    protected TextField blue;
    protected Button memButton;
    protected Button revertButton;
 
    public void imageLoad() {
        icon1 = Toolkit.getDefaultToolkit().getImage("resources/splash.gif");
        mTrack.addImage(icon1, 0);
        img1 = Toolkit.getDefaultToolkit().getImage("resources/icn.gif");
        mTrack.addImage(img1, 0);
        img2 = Toolkit.getDefaultToolkit().getImage("resources/splash.gif");
        mTrack.addImage(img2, 0);
    }
 
    @Override
    public void paint(Graphics g) {
        oldColorRect.paint(g);
        newColorRect.paint(g);
    }
 
    public void pause(int mili) {
        try{
            Thread.sleep(mili);
        }catch(Exception e) {
 
        }
    }
 
    @Override
    public String toString() {
        String toRtrn = "";
        toRtrn += "\n\t\t\tTOSTRING:";
        toRtrn += "\nVariables:";
        toRtrn += "\n\tImages:";
        toRtrn += "\n\ticon1 :   " + icon1;
        toRtrn += "\n\timg1 :   " + img1;
        toRtrn += "\n\timg2 :   " + img2;
        return toRtrn;
    }
 
 
 
    public static void main(String[] args) {
        FUI exec = new FUI(args);
    }
}

Open in new window

Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Make sure the images are in a resources directory under the directory from which you run the app
Probably need a few more details..see below...

For basic image operations you deal with images as instances of the class


  java.awt.Image


As of Java 1.4 you can load and draw image files encoded as JPEG, GIF and PNG. With Java 5.0 you can also load bitmap formats BMP and WBMP.


To load an image in an applet, you can use one of the overloaded getImage() methods for loading from a URL:

  Image img = getImage("http://www.myschool.edu/anImage.gif");

or :

  Image img = getImage(getCodeBase(),"anImage.gif");

where the Applet class method getCodeBase() provides the URL for the location of the applet's class file. The file name in the second argument is appended to this URL. (See Chapter 13 for information about the java.net.URL class.)

For an application, use

  Image img = Toolkit.getDefaultToolkit().getImage(URL or file path);

The Toolkit is a class in the java.awt package that provides various resources and tools for the display system. One of the Toolkit methods is getImage() that functions much like the getImage() method in the Applet class. It is overloaded to take either a String filename parameter specifying the location of the image file or a URL parameter identifying the image file.

Before calling getImage(), one must have a reference to the Toolkit instance in use. The static method Toolkit.getDefaultToolkit() returns a reference to that Toolkit.

You can obtain an image from a JAR file by using the getResource() static method from the Class class:

  URL url = this.getClass().getResource("anImage.gif");
  Image img = Toolkit.getDefaultToolkit().getImage(url);

or

  URL url = YourClass.class.getResource("anImage.gif");
  Image img = Toolkit.getDefaultToolkit().getImage(url);

Note: Image is an abstract class so you are actually loading an instance of a concrete subclass of Image with the getImage() methods above. However, you always treat the object as an instance of Image, i.e. you only have access to its methods.

For BufferedImage class, see:

http://www.particle.kth.se/~lindsey/JavaCourse/Book/Part1/Supplements/Chapter11/bufferedImage.html
Better off using getResource() as GlobalLevel explains above
You can then load the image url using ImageIO

http://helpdesk.objects.com.au/java/how-do-i-read-an-image-from-a-url

Toolkit.getDefaultToolkit().getImage("resources/splash.gif"); does not give you the right path to the file.
If I take that the image is within the jar and the path you are writing is from the root of that jar, it would be something like Toolkit.getDefaultToolkit().getImage(getClass().getResource("/resources/splash.gif"));
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
try this

    public void imageLoad() {
        icon1 = ImageIO.read(getClass().getResource("/resources/splash.gif"));
        img1 = ImageIO.read(getClass().getResource("/resources/icn.gif"));
        img2 = ImageIO.read(getClass().getResource("/resources/splash.gif"));
    }

you can get rid of MediaTracker, its no longer needed

you will need to put your gif's in a resources directory that is available to the classpath, eg. in your jar

:-)
The accepted comment is just a copy of what GlobaLevel and __geof__ had already suggested, why did you ignore there comments?