Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

JButton hidden behind Image

$
0
0
I am using an image as a background and then I have another image as a JButton. The Background image currently overlaps the button, so you can't see it. When I comment repaint() out, then the button is in front, however because I re-located the button, that space is faded out of the background image. So basically, what must I do with my code to put the button in front of the background image( with the background image still intact)?

public class Start extends JFrame {

	JPanel jp = new JPanel();
	JButton startButton = new JButton();
	private Image dbImage;
	private Graphics dbg;
	Image backgroundFirst;
	int backx;
	int backy;

	public Start() {
		ImageIcon i = new ImageIcon(
				"C:/Users/Mel/workspace/camptycoon/javagame/src/javagame/background1.png");
		backgroundFirst = i.getImage();

		startButton
				.setIcon(new ImageIcon(
						"C:/Users/Mel/workspace/camptycoon/javagame/src/javagame/start.png"));
		jp.add(startButton);
		startButton.setLayout(getLayout());
		add(jp);
		validate();

		// Frame Properties
		setTitle("Counselor Training");
		setVisible(true);
		setSize(755, 600);
		setDefaultCloseOperation(EXIT_ON_CLOSE);

	}

	public LayoutManager getLayout() {
		int x = 540;
		int y = 475;
		startButton.setBorder(null);
		startButton.setLocation(x, y);

		return null;

	}

	public void paint(Graphics g) {
		dbImage = createImage(getWidth(), getHeight());
		dbg = dbImage.getGraphics();
		paintComponent(dbg);
		g.drawImage(dbImage, 0, 0, this);
	}

	public void paintComponent(Graphics g) {
		backx = 10;
		backy = 30;

		g.setColor(Color.BLUE);
		g.drawImage(backgroundFirst, backx, backy, this);
		repaint();
	}

}


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>