1 Attachment(s)
Adding components to my GUI over a background image
So here is my frame with the background image i want and now i am just having a bit of trouble adding labels on top of it. Do i need to use LayeredPane to add the labels(the part about this tool will..., your atk/matk, your element, etc are all labels) on top of the background?
also is there a layout manager i could use to place the components like this or would i have to use absolute positioning?
any help and suggestions are very much appreciated :D thanks very much
Attachment 695
Re: Adding components to my GUI over a background image
How are you doing the background image? And what layouts have you tried? Check out your options here: A Visual Guide to Layout Managers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container) and keep in mind that you can nest layouts.
Re: Adding components to my GUI over a background image
i just have a basic Jframe right now and i used this to add the image to it
Code :
public void paint(Graphics g) {
Image bg = Toolkit.getDefaultToolkit().getImage("bg.png");
g.drawImage(bg,0,0,getSize().width,getSize().height,this);
super.paint(g);
}
and when i add a button to the frame and use null layout and i use absolute positioning the button is good. when i try to do the same adding a label the label doesnt appear
Code :
import javax.swing.*;
import java.awt.*;
public class fdmgcalc extends JPanel {
private static final long serialVersionUID = 0L;
public fdmgcalc() {
setOpaque(false);
setLayout(null);
}
public static void main(String[] args) {
JFrame mFrame = new JFrame("Smack The Pet 1.0");
fdmgcalc c = new fdmgcalc();
mFrame.add(c);
mFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mFrame.setSize(400, 500);
mFrame.setVisible(true);
}
public void paint(Graphics g) {
Image bg = Toolkit.getDefaultToolkit().getImage("bg.png");
g.drawImage(bg,0,0,getSize().width,getSize().height,this);
super.paint(g);
}
}
this is the whole code so far..just messing around with making the frame with bg image and then adding stuff on top of it
the program i made it has everything but the bg image so before i try to put it in there i figured i should mess with it some instead of screwing all up haha :D ill read that layout manager guide. any other stuff i should look at that might help too?
Re: Adding components to my GUI over a background image
That's because you're adding a JPanel, not a JLabel. Unless you mean something else?
And you probably don't want to load the image each time your JFrame is repainted. Load it once and store it in a variable, then just use the already loaded image.
...and why is your super.paint() happening after you paint the image? Doesn't that just clear the image?
Re: Adding components to my GUI over a background image
ah i took out the label stuff cause it kept removing the bg when it did go on the frame
when would it get repainted? i thought once its up there thats it
and i thought the super goes after lol guess i need to reread how to paint lol this is becoming harder than i originally thought :confused:
Edit: ah what does the super part do? cause i removed it and it still puts the image on the bg...i also stored the image in a variable like you said :D
edit again: :D:D:D i tried something different now for the label i used setBounds to move it around and put it before i made the window opaque and now it sits on top of the bg :D:D:D
Re: Adding components to my GUI over a background image
it does disappear :(
when i run it in eclipse everything looks perfect but when i make it into a jar the bg disappears but everything else is good why does the bg disappear :(
Re: Adding components to my GUI over a background image
Well, you've got a few problems here. I recommend you read through this: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)
Also, does your Jar have access to that file? What is being returned by your getImage() method?
Read that tutorial, and figure out the file access issue, and post an updated SSCCE with your most recent code (after you read that tutorial and fix anything that you see in it), and we'll go from there.
Re: Adding components to my GUI over a background image
thanks very much :D ill read through that right now and get to fixing
and you know everything lol every time i post a question you are always the one answering first i very much appreciate it :D
Re: Adding components to my GUI over a background image
finally got the image working :D:D:D i did like this
Code :
public void paintComponent(Graphics mFrame) {
super.paintComponent(mFrame);
URL bg = fdmgcalc.class.getResource("bg.png");
Image bgpic = Toolkit.getDefaultToolkit().getImage(bg);
mFrame.drawImage(bgpic,0,0,getSize().width,getSize().height,this);
}
Re: Adding components to my GUI over a background image
You should move this code outside of the paint method so you only have to load the image one time.
Code :
URL bg = fdmgcalc.class.getResource("bg.png");
Image bgpic = Toolkit.getDefaultToolkit().getImage(bg);
Re: Adding components to my GUI over a background image
thanks norm :D i did that now and i read through that paint guide and figured i need a panel so i added that and works good
i also took everything out of the main() and put it its own methods
now i just have 1 more question...how come you need a panel instead of painting on the jframe?
Code :
package smackthepet;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class MyPanel extends JPanel{
private static final long serialVersionUID = 1L;
public Image bgpic;
public MyPanel() {
setBorder(BorderFactory.createLineBorder(Color.black));
URL bg = Msmack.class.getResource("bg.png");
bgpic = Toolkit.getDefaultToolkit().getImage(bg);
JLabel heading = new JLabel("Smack The Pet 1.0");
heading.setFont(new Font("Serif", Font.PLAIN, 32));
Dimension headingSize = heading.getPreferredSize();
heading.setBounds(79,22,headingSize.width,headingSize.height);
add(heading);
}
public Dimension getPreferredSize() {
return new Dimension(410, 510);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(bgpic,0,0,getSize().width,getSize().height,this);
}
}
Code :
package smackthepet;
import java.awt.*;
import javax.swing.*;
public class Msmack {
public Msmack() {
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
private static void createAndShowGUI() {
JFrame mFrame = new JFrame("Smack The Pet 1.0");
mFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mFrame.add(new MyPanel());
mFrame.pack();
mFrame.setResizable(false);
mFrame.setVisible(true);
}
}
so this is how i have it now :D anything i should do differently? and thanks for all the help everyone :D
Re: Adding components to my GUI over a background image
Quote:
a panel instead of painting on the jframe?
I think it is because JPanel extends JComponent which has useful Swing methods and features.
JFrame extends Frame which doesn't.
Re: Adding components to my GUI over a background image
sounds pretty good to me :D thanks :D