Hintergundbild im JFrame

bjmicha

Mitglied
Hallo,

ich versuche in einem JFrame Fentser ein Hintergrundbild einzubauen.....
Ich habe es mit der Methode paint() versucht. Das klappt zwar aber ich kann in dem Fenster keine anderen Elemente anzeigen, weil das Hintergundbild immer im Vordergrund ist.

Was kann ich tun

Gruß

Michael
 
Schau dir mal den Aufbau des JFrame an (RootPane, LayeredPane, ContentPane, GlassPane).
Könntest das Bild auf ein Pane zeichnen und die Steuerelemente eine Ebene höher anordnen.

Gehört eher ins Swing/AWT/SWT-Forum.
 
Holla!

Versuchs mal damit:

(Eigenes JPanel abgeleitet und paintComponent überschrieben)

Code:
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

/*
 * Created on 23.02.2004
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */

/**
 * @author Darimont
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public class BImgTest extends JFrame {

	public BImgTest() {
		super("Hintergrund Test");
		setSize(320, 240);
		BGImgJPanel panel = new BGImgJPanel("c:/energy.jpg");
		panel.setSize(320, 240);

		panel.setLayout(new FlowLayout());

		for (int i = 0; i < 10; i++) {
			panel.add(new JButton("Test " + i));
		}

		getContentPane().add(panel);

		setVisible(true);

		// TODO Auto-generated constructor stub
	}

	public static void main(String[] args) {
		new BImgTest();
	}

	class BGImgJPanel extends JPanel {

		private Image bgImage;
		private Image org;
		private ImageIcon icon;

		BGImgJPanel() {
			this(null);
		}

		BGImgJPanel(String fileName) {
			super();
			if (fileName == null)
				return;

			setOpaque(false);
			icon = loadImage(fileName);

			if (bgImage == null)
				throw new RuntimeException();
			org = bgImage;

			return;
		}

		public void paintComponent(Graphics g) {

			if (bgImage != null)
				g.drawImage(
					bgImage,
					0,
					0,
					(int) icon.getIconWidth(),
					(int) icon.getIconHeight(),
					this);
			super.paintComponent(g);
		}

		private ImageIcon loadImage(String imgLoc) {
			// TODO Auto-generated method stub
			MediaTracker mt = new MediaTracker(this);
			try {
				bgImage = ImageIO.read(new File(imgLoc));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			mt.addImage(bgImage, 0);

			try {
				mt.waitForAll();
			} catch (InterruptedException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

			return new ImageIcon(bgImage);

		}

		/* (non-Javadoc)
		 * @see java.awt.Component#setBounds(int, int, int, int)
		 */
		public void setBounds(int x, int y, int width, int height) {
			// TODO Auto-generated method stub
			bgImage = org.getScaledInstance(width, height, Image.SCALE_SMOOTH);
			icon = new ImageIcon(bgImage);
			super.setBounds(x, y, width, height);
		}

	}
}

Gruß Tom
 
Zurück