Java Grafikbutton auf Hintergrundbild

RolandAMS

Cadet 4th Year
Registriert
März 2007
Beiträge
81
Moin.

Ich versuche gerade wie im Titel beschrieben eine Hintergrundgrafik zu laden und darauf in der Mitte einen JButton oder zumindest ein JLabel zu platzieren. Ich krieg zwar schon die Grafik und den Button hin, aber ich habe keinen Dunst wie ich den Button ins Bild "reinkriege". Ich hoffe jemand kann mir da helfen.

Datei: BackgroundImageExample.java
Code:
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;

import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
  
  public class BackgroundImageExample extends JFrame {
  
  	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public BackgroundImageExample() {
  		super("Hot Labyrinth");
  		setDefaultCloseOperation(EXIT_ON_CLOSE);
   		Box box;
  		box = Box.createVerticalBox();
  		
  		box.add(new BackgroundPanel(new ImageIcon("C:/Users/ro/Desktop/bg.jpg")));
  		
  		
  		JButton Bild = new JButton();
  		ImageIcon iI=new ImageIcon("C:/Users/ro/Desktop/startbutton.jpg");
  		Bild.setBounds(new Rectangle(0, 0, iI.getIconWidth(), iI.getIconHeight()));
  		Bild.setIcon(iI);
  		
  		box.add(Bild);
  		
  		
  		add(box);
  		
  		pack();
  		setVisible(true);
  	}
  
  	/**
  	 * @param args
  	 */
  	public static void main(String[] args) {
  		new BackgroundImageExample();
  	}
  
  	class BackgroundPanel extends JPanel {
  		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		ImageIcon icon;
  
  		public BackgroundPanel(ImageIcon icon) {
  			this.icon = icon;
  		}
  
  		protected void paintComponent(Graphics g) {
  			super.paintComponent(g);
  			if (this.icon != null) {
 				g.drawImage(icon.getImage(), 0, 0, this);
  			}
  		}
  
  		public Dimension getPreferredSize() {
  			if (icon != null) {
 			    return new Dimension(icon.getIconWidth(), icon.getIconHeight());
  			} else {
 				return super.getPreferredSize();
  			}
  		}
  	}
  }

Bitte um Hilfe.
Ergänzung ()

ok habs geschafft. Das Zauberwort heißt JLayeredPane();
 
Zurück
Oben