Jump to content

Preloading images in Java

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
miloguy

miloguy

    Newbie

  • Members
  • Pip
  • 4 posts
I can't make anything in Java until I know how to preload images.
Just like, simple example code so I atleast know what to do.
I heard something about BufferedImages or something, but I'm all lost. Anything I make with Java will look terrible without it.

Thanks! :D

#2
Fae

Fae

    Learning Programmer

  • Members
  • PipPipPip
  • 80 posts
Taken from some code I wrote a few weeks ago...


private ImageIcon back;		//Global variables
private ImageIcon next;
private ImageIcon menu;

back = new ImageIcon(getImage(getDocumentBase(), "../img/backtext.png"));       //Inside constructior method
next = new ImageIcon(getImage(getDocumentBase(), "../img/nexttext.png"));
menu = new ImageIcon(getImage(getDocumentBase(), "../img/menutext.png"));

That loaded the images, then created buttons using the 'back', 'next' and 'menu' ImageIcons instead of button text.

I guess it all depends on what you're using images for. The above method was the first tiem I'd ever uised images in Java, and it was adequate for the purpose. Maybe if you're doing something more complex, you'd need a different type of image.

What are you aiming to do?
I'll ask a lot of questions (most of them probably stupid stuff). Bear with me, i'm still learning! ^_^ Also, I'll try to answer as many questions as I can as well, but I'm not very good yet. I'm sure I'll be of more use once I get better :)

#3
miloguy

miloguy

    Newbie

  • Members
  • Pip
  • 4 posts
Well, it's in an Applet. I usually just do something like:

public void paint(Graphics g){
g.drawImage(getImage(getCodeBase(),"example.png"),0,0,this);
}

But it shows nothing for a couple of seconds then appears (because it has to load, which doesn't look good at all).
I'll try storing the image in the first place like

Image example;
public void init(){
example=getImage(getCodeBase(),"example.png");
}
public void paint(Graphics g){
g.drawImage(example,0,0,this);
}

And I'll also try using an ImageIcon.

#4
Fae

Fae

    Learning Programmer

  • Members
  • PipPipPip
  • 80 posts
I'm not sure my advice is best on this topic... I'm honestly not too knowledgeable about image types and such in Java... My applet doesn't even use paint(), so maybe I'm barking up the wrong tree ^^

Mine only displays static text and images, so I didn't really have to do anything fancy.
I'll ask a lot of questions (most of them probably stupid stuff). Bear with me, i'm still learning! ^_^ Also, I'll try to answer as many questions as I can as well, but I'm not very good yet. I'm sure I'll be of more use once I get better :)

#5
miloguy

miloguy

    Newbie

  • Members
  • Pip
  • 4 posts
Bump?

#6
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
Could you try this?

import javax.swing.*;

import java.awt.*;


public class ImageApplet extends JApplet {

    Image img;

    MediaTracker mt;


    @Override

    public void init() {

        mt = new MediaTracker(this);


        System.out.println(getCodeBase().getPath());

        img = getImage(getCodeBase(), "Penguins.jpg");

        mt.addImage(img, 0);


        try {

            mt.waitForID(0);

        } catch (InterruptedException e) {

            return;

        }

    }



    public void paint(Graphics g) {

//uncomment for errorcheck of some kind i don't understand

//        if ((mt.statusID(0,false) & MediaTracker.ERRORED) != 0) {

//            g.setColor(Color.red);

//            g.fillRect(0, 0, size().width, size().height);

//            return;

//        }

        g.drawImage(img, 0, 0, this);

    }

}