Original Post
🔒 Locked
[java] Images as a background in an application
Advertisement
Punx I want to have a picture as the background of an application I''m making but I''m confused on exactly how to do it. Would I use an Image or an ImageIcon and how would I initialize/draw it
I would recommend you to draw it in a panel... I usually use Swing components because I get rid easily of the double buffer issue with them, though the applications you make with them are a bit slow. Therefore, I´m going to use Swing for these examples...
To draw it in your window, you have to make a class that extends the JPanel class, and then you override the public void paintComponent(Graphics g) method this way:
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, posX, posY, this);
}
You should paint your background first, as the next things you''ll draw will overlap the first one.
By the way, you should use Image for your background, ImageIcon is used for the components like JButtons. To get an Image, in some part where you want to load your image you must add:
import java.awt.Toolkit;
import java.awt.Image;
Toolkit tk = Toolkit.getDefaultToolkit();
Image image = tk.getImage("nameImage");
getImage() supports quite a few known formats: JPG, GIF, PNG...
As a last thing, but not a least one, I recommend you to read the API about the things I mentioned you above...
Happy coding! :-)
I would recommend you to draw it in a panel... I usually use Swing components because I get rid easily of the double buffer issue with them, though the applications you make with them are a bit slow. Therefore, I´m going to use Swing for these examples...
To draw it in your window, you have to make a class that extends the JPanel class, and then you override the public void paintComponent(Graphics g) method this way:
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, posX, posY, this);
}
You should paint your background first, as the next things you''ll draw will overlap the first one.
By the way, you should use Image for your background, ImageIcon is used for the components like JButtons. To get an Image, in some part where you want to load your image you must add:
import java.awt.Toolkit;
import java.awt.Image;
Toolkit tk = Toolkit.getDefaultToolkit();
Image image = tk.getImage("nameImage");
getImage() supports quite a few known formats: JPG, GIF, PNG...
As a last thing, but not a least one, I recommend you to read the API about the things I mentioned you above...
Happy coding! :-)
> So would I draw forground objects onto the JPanel as well?
Yes, of course... after drawing the background...
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(background, x, y, this);
g.drawImage(fg1, x1, y1, this);
g.drawImage(fg2, x2, y2, this);
//etc. etc. etc.
}
Yes, of course... after drawing the background...
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(background, x, y, this);
g.drawImage(fg1, x1, y1, this);
g.drawImage(fg2, x2, y2, this);
//etc. etc. etc.
}
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement