Been searching around yet to no avail on how I can essentially add Objects on top of a contentpane/background image. Essentially I have to create a Board Game and my task is to make the GUI. I need to make a Center location to hold my pieces then be able to move the pieces to the spots. As of right now I just have a background image of my board and would like to add components to make it interactive yet, I cant seem to overlay the interactive components on top of my background image.
Here's my code:
public class Lotus{
private static JPanel _panel = new JPanel();
private static JFrame _frame = new JFrame("Lotus");
private static Container _pieces = new Container();
public static void main(String[] args){
/**
JLabel A = new JLabel("");
A.setOpaque(true);
A.setBackground(Color.BLACK);
_frame.add(_pieces);
_pieces.add(A);
_frame.setAlwaysOnTop(_pieces);
*/
try{
_frame.setContentPane(new JLabel(new ImageIcon(
ImageIO.read(new File("C:\\Users\\Martin\\Pictures\\pic625580.jpg")))));
}catch(IOException e){
System.out.println("Image is not Found.");
e.printStackTrace();
}
Toolkit tk = Toolkit.getDefaultToolkit();
int xSize = ((int)tk.getScreenSize().getWidth());
int ySize = ((int)tk.getScreenSize().getHeight());
_frame.setSize(xSize,ySize);
Container contentPane = _frame.getContentPane();
contentPane.setLayout(null);
_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//_frame.pack();
_frame.setVisible(true);
/**
JLayeredPane mainLayer = new JLayeredPane();
_frame.add(mainLayer, BorderLayout.CENTER);
JLabel label = new JLabel("LABEL", JLabel.CENTER);
label.setBounds(100,100,200,100);
label.setOpaque(false);
_frame.getContentPane().setComponentZOrder(label, 0);
//label.setVisible(true);
label.setBackground(Color.CYAN);
mainLayer.add(label,1);
mainLayer.moveToFront(_frame);
*/
}
EDIT:
Ok so I tried the approach below yet, my JFrame is now not showing up, and looking the Java console for some reason it is terminating the program. Code:
public static void main(String[] args){
class Board extends JPanel{
JFrame _frame = new JFrame("Lotus");
Image image = Toolkit.getDefaultToolkit().getImage("C:\\Users\\Martin\\Pictures\\pic625580.jpg");
public void paintComponent(Graphics g) {
JLabel A = new JLabel("");
A.setOpaque(true);
A.setBackground(Color.BLACK);
Board b = new Board();
_frame.setContentPane(b);
_frame.setVisible(true);
_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
super.paintComponent(g);
g.drawImage(image,0,0,A);
g.fillOval( 50,50,5,5 );
}
public Dimension getPreferredSize() {
return new Dimension( 300,300);
}
You'll have to subclass JPanel
overriding paintComponent
to do the drawing stuff. For instance:
public class Board extends JPanel {
Image image = ... // load it here
public void paintComponent(Graphics g) {
super.paintComponent(g); // fatal
g.drawImage(image,0,0,this); // omitted label
g.fillOval( 50,50,5,5 ) // circle 5x5 at pixel 50x50
// etc.
}
}
Now you should also override getPreferredSize
, to tell the layout manager how big the board should be:
public Dimension getPreferredSize() {
return new Dimension(300,300) // your required size...
}
Then set Board
as the content pane.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.