JPanel Problem =/ Question(My Code Provided)

Ok Question, I have a GameBoard class(JPanel) which allows me to ingegrate w.e maps I want and w.e sprites to be loaded onto it, then this is put onto my JFrame class. For my GameBoard Class I painted a Custom image onto the JPanel by:

public void paint(Graphics g) {
        super.paint(g);
		  
        bg = new ImageIcon("Trees!.png").getImage();
        g.drawImage(bg, 0,0,null);   
	Graphics2D g2d = (Graphics2D)g;
        2d.drawImage(spO.getImage(), spO.getX(), spO.getY(), this);
		  

		  
		  
        Toolkit.getDefaultToolkit().sync();
        g.dispose();
  		  
}

Question is, when I try to add a JLabel or JButton onto this GameBoard It keeps getting painted behind the Image. The reason I know this is If I don’t paint an Image via commenting out g.drawImage(bg, 0,0,null); I get a regular background (blue) and my buttons and lables are there, so how do I make this Image the standard panes image?

All Help is appreciated thanks.

Change:

g.dispose();

to:

g2d.dispose();

…maybe? And do you have @Override above your paint mentod?

Tried g2d.dispose, didnt work neither did adding the @Override. Here’s the entire code for this Board Class:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
//------------
//import customframe.CustomFrame;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JComponent;
import java.util.*;
import java.io.*;


//------------
import javax.swing.JPanel;
import javax.swing.Timer;


public class Board3 extends JPanel implements ActionListener {

    private Timer timer;
    private SpriteO spO;
	private SpriteS sp;
	 
	 Random randomGenerator = new Random();
	monster_elixisDragon ED = new monster_elixisDragon();  
  
  	public Image bg;  
	 public Board3() {
			
			
        addKeyListener(new TAdapter());
        setFocusable(true);
        setBackground(Color.BLUE);
        setDoubleBuffered(true);
		   setPreferredSize(new Dimension(450, 210));
       	sp= new SpriteS();
	
	     spO = new SpriteO();

       timer = new Timer(5, this);
   	
		timer.start();
		 this.add(ED.mlab);//Label I want to display
		 
		 repaint();  
	 
	 }

	 @Override
    public void paint(Graphics g) {
        super.paint(g);
		  
		  bg = new ImageIcon("Trees!.png").getImage();
        g.drawImage(bg, 0,0,this);   
		  Graphics2D g2d = (Graphics2D)g;
        g2d.drawImage(sp.imgArray().get(sp.getFrame()), sp.getX(), sp.getY(), this);
		  g2d.drawImage(spO.getImage(), spO.getX(), spO.getY(), this);
		  Toolkit.getDefaultToolkit().sync();
        g2d.dispose();
  		  
	 }


    public void actionPerformed(ActionEvent e) {
        sp.move();
        repaint();  
  		  
		
	 }


    private class TAdapter extends KeyAdapter {

        public void keyReleased(KeyEvent e) {
            sp.keyReleased(e);
        }

        public void keyPressed(KeyEvent e) {
            sp.keyPressed(e);
        }
    }

}

You should be overriding paintComponent(Graphics g) instead. However, that will still paint over the buttons because the panel draws all children first.

I don’t understand what you are trying to achieve through this. You should be drawing on a JComponent that you don’t add any components to.

I’m trying to put JButtons and JLabels on this panel and have them being painted over the background Image bg. In this class I paint sprites and the background and this gets put onto a JFrame where other UI components are.

I want to make the image bg the absolute background image instead of the panels raw color background. Pretty much I don’t want the image bg to be painting over the other components? Is what i’m trying to achieve the wrong way of doing so?

And I thought I was overriding the paint method?

Thanks in advance =D.

Easy solution, split again the JPanel and use Layout.

@Above, can you elaborate more please XD.

I’ve tried this too. Using regular awt buttons and stuff works, but I can’t get it to work with JPanel and Jbuttons. I came across this link when searching for an answer:

@OP
http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html
basiclly, you define different JPanels and each one draws different thing. For your case, you need two JPanels. One to hold the swing components while other do Graphics draw. Then add both of them in single JPanel that using Layout (so you can adjust their position). The single JPanel that holds these two can be added to JFrame.

Interesting I’ll try this, thanks. Pretty much my game is an RPG and I want some npcs to have JLabels which pop up and i’d rather use java’s built in components to do so instead of drawing rectangles and such manually.
So what you’re saying is the one with Sprites and Images is on one, then the swing components etc get put on a transparent JPanel which then gets added on top of the Images one?

Also Is there better industry standard for making “Buttons” and components than what I’m doing to achieve such?

Oh! if you mean label or button inside game world, then I suggest you to leave swing. I thought you want to create some HUD or what. Rather use JLabel or JButton, make one by your own - a class that receive Graphics parameter to draw text and background while keep updating its position (for example, above player’s head).

Thanks yea, that is what I meant originally(thanks ill look more into that). Also, that does bring me back to a HuD, for now I have a panel that is adhesive to the main frame which has different buttons settings and has a custom painting on the panel, but to have a real HuD like what you originally thought I wanted, which I do but I coped out with a JPanel Holding buttons on the frame. TO do this like you say how do I prioritize A panel over a panel such as a HuD by which I mean fully transparent and can still allow clicks onto the main panel the game world? For example I would idealy like a HUD that hold a Portrait of the player with Health and one with action bars, and then eventually start menus and such.

Do you have any picture that describing what you need?

Here is what I have so far…

okay, leave the layout. use your own custom class, with this way you can set tranparency level and background image.

Not sure I understand you can you elaborate more about the custom class?

Let’s say a custom button. So this class acts like Entity, needs to be drawed. It can have fields like background image and position (x, y). A simple method to use this class: for example check if mouse click’s point happened on top of this class. If yes, tell game state to do what this class have to do.

In the end, it is best to completely avoid Swing while making games. You should create your own Button class.

Alright thanks for the code, i’ll read it over and try to understand it. So yea I guess that’s what I have to learn to do is avoid Swing, and for HuDs and all GUI you would create everything from scratch like that button class?

So pretty much the only Swing Components you use are just Panels and frames just do the basics and that’s it for game development for Swing*?

Yes, unless you want to use really complicated GUI components where it’s less time consuming to just wrangle with Swing rather than make the component yourself.

Avoiding Swing means that you’ll need to avoid classes beginning with J, except maybe for JFrame but you could substitute that with java.awt.Frame.

It is best to also draw on a java.awt.Canvas, using BufferStrategy. Google “site:java-gaming.org BufferStrategy” (without quotes) and look through posts explaining how to use it :slight_smile: