JPanel Problem =/ Question(My Code Provided)

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:

Alright thanks, question about the Button class.

I know this sounds stupid but I don’t quite understand the mouse methods ><, where are we getting the input from? And what is the int button for?

public void paint(Graphics g) {
super.paint(g);

    bg = new ImageIcon("Trees!.png").getImage();
    g.drawImage(bg, 0,0,null);   

Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(spO.getImage(), spO.getX(), spO.getY(), this);

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

}

You were missing a ‘g’ on the object name g2d, as marked. This might be your issue.

@JayTech
Sorry for not seeing your post earlier, the input methods should be called from you mouse listeners. The into button is the button number that causes the event, MouseEvent.BUTTON1-3.

Was on vacation(Actually didn’t bring my computer) just got back onto the forums, thanks for all of the responses . ra4king ah, I gotcha makes sense now thanks =).

Edit: the 2d some how cut off the g when I pasted the code onto here.