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
Preloading images in Java
Started by miloguy, Sep 22 2010 11:00 PM
5 replies to this topic
#1
Posted 22 September 2010 - 11:00 PM
|
|
|
#2
Posted 23 September 2010 - 01:08 AM
Taken from some code I wrote a few weeks ago...
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?
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
Posted 23 September 2010 - 08:33 AM
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.
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
Posted 24 September 2010 - 12:12 AM
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.
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
Posted 26 October 2010 - 09:48 PM
Bump?
#6
Posted 27 October 2010 - 06:36 AM
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);
}
}


Sign In
Create Account

Back to top









