0

I am trying to draw shapes over a background image in a JFrame. I can get the image I want as the background, but I can't get the shapes to be drawn on top of that image. What I've tried so far is just rearranging the order that the components are called, but that doesn't seem to have had an effect. This is where I did it:

JFrame frame = new JFrame ();
frame.setTitle("Happy Holidays!");
frame.setSize(813,645);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setContentPane(new JLabel(new ImageIcon("C:\\Users\\Brian\\Desktop\\Eclipse Projects\\BrianBolnickFinal\\bin\\christmas.jpeg")));
frame.add(holiday);

Where the content pane contains the image and holiday is where my shapes are being drawn. Is it something with not being able to draw on a content pane? Or is there something else going wrong in some other place? my holiday class works fine without the image, so I know that is at least working. Any suggestions?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Luminusss
  • 571
  • 1
  • 6
  • 27

1 Answers1

3

First of all, holiday should be transparent if you hope to be able to draw over other components

holiday.setOpaque(false);

Next, you have two options.

First, you could add holiday to the label...

JLabel label = new JLabel(new ImageIcon("C:\\Users\\Brian\\Desktop\\Eclipse Projects\\BrianBolnickFinal\\bin\\christmas.jpeg")));
label.setLayout(new BorderLayout());
label.add(holiday);

Or you could use OverlayLayout layout manager

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366