I need some help to make an image a background for a JFrame.
I tried researching and i found some ways like using the graphics g but i am not sure what class i place it in.
thanks in advance!
here is the first class of my code.
------------------------------------------
package classes; import java.awt.Color; import java.awt.Component; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.BoxLayout; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; public class rohwcg extends JFrame { // adds the buttons private JButton minerbutton; private JButton farmerbutton; private JButton lumberjackbutton; private JButton blacksmithbutton; public rohwcg() { super ("Realms of Havenwood Class Guide"); this.getContentPane().setBackground(Color.black); this.setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS)); Icon mbutton = new ImageIcon (getClass() .getResource("miner.png")); minerbutton = new JButton(mbutton); add(minerbutton); //farmer button Icon fbutton = new ImageIcon (getClass() .getResource("farmer.png")); farmerbutton = new JButton(fbutton); add(farmerbutton); //lumberJack button Icon lbutton = new ImageIcon (getClass() .getResource("lumberjack.png")); lumberjackbutton = new JButton(lbutton); add(lumberjackbutton); //blacksmith button Icon bbutton = new ImageIcon (getClass() .getResource("blacksmith.png")); blacksmithbutton = new JButton(bbutton); add(blacksmithbutton);
I didn't paste all of it because it wasn't needed.
--------------------------------------------------
2nd class
--------------------------------------------------
package classes; import java.awt.Dimension; import java.awt.Toolkit; import javax.swing.JFrame; public class thehandler { public static void main(String args []) { rohwcg classes1 = new rohwcg(); classes1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); classes1.setSize(700,300); classes1.setVisible(true); Toolkit tk = Toolkit.getDefaultToolkit(); Dimension d = tk.getScreenSize(); int x = d.width / 2; int y = (d.height / 2 ) - classes1.getHeight(); classes1.setLocation(x,y); } }
i imported Jframe its just hidden.