I would like the label "Test!" to sit behind the dark box, and to be able to see the background image behind. How can I achieve this?
This is a minimal example of my real problem, where I am trying to layer 2 frame-sized JPanels in order to create a navigation overlay over my application.
private fun createAndShowGUI() {
defaultCloseOperation = EXIT_ON_CLOSE
contentPane = JLabel(ImageIcon("C:\\...\\bee.jpg"))
layout = FlowLayout()
//
val translucentPanel: JPanel = object: JPanel() {
override fun paintComponent(g: Graphics?) {
super.paintComponent(g)
val graphics = g!!.create() as Graphics2D
graphics.composite = AlphaComposite.SrcOver.derive(0.5f)
graphics.color = background
graphics.fillRect(0, 0, width, height)
graphics.dispose()
}
}
translucentPanel.background = Color(0, 0, 0, 125)
translucentPanel.preferredSize = Dimension(250, 150)
translucentPanel.isOpaque = false
//
val backingPanel = JPanel()
backingPanel.isOpaque = false
backingPanel.layout = OverlayLayout(backingPanel)
backingPanel.add(translucentPanel)
backingPanel.add(Label("Test!"))
//
add(backingPanel)
setSize(600, 400)
isVisible = true
}