java images overlapping JMenuBar

Started by
3 comments, last by ChickenMcOwnage 20 years, 5 months ago
I'm making a game in java, a simple one similar to scrabble. For each letter I am using an array of 26 images loaded in my game class' constructor. Then I just do g.drawImage(letterGfx[board[x][y]].getImage(), x*letterSizeX, y*letterSizeY, letterSizeX, letterSizeY, this); for every element of my board (a 2-d array of integers). My problem is, I'm also using a JMenuBar up top, and when I click on on of the menu items, it drops down underneath what I have drawn on the board, so you can't really see the menu items at all until you move your mouse over them.... I'm using super.paint(), and public void paint(Graphics g) etc. Is there a better way of drawing the images or is there something I did wrong with the menubar? (I can only use built in java methods as this is for a class) sorry if this isn't explained well. thanks in advance for any help! [edited by - ChickenMcOwnage on November 13, 2003 8:33:13 PM]
~ChickenMcOwnage~
Advertisement
Umm, there is a forum dedicated to java lamers if you scroll down a bit further....
Hey,
Where are you doing the drawing? From my experience, you want to create a new class that extends some basic (a JPanel is what I use) and then put your drawing code in the paintComponent method of the JPanel, not forgetting the super call of course. I know that works with menus, but there may just be something simple wrong with what you''re doing.

tj963
tj963
Thanks for your response! I''m doing my drawing right onto the JFrame... I tried what you suggested, making a new class GfxPanel extending JPanel, but now nothing is drawn at all, it''s just an empty frame (not even my menuBar is drawn... and I get stack overflows? hmmm..) I heard there are layers to frames? Is there a way to put my menu bar on the top layer and the graphics on the bottom? I also looked around for examples of how to do simple drawing but I really can''t find any.

@AP: yeah there''s a java forum, but I figured since I''m a beginner and not sure where to begin doing simple graphics, I assumed the "for beginners" section would be more appropriate. Is this "for beginners" section only for beginning C++? At any rate, you don''t have to be a dick about it.
~ChickenMcOwnage~
You want to add your JPanel to the content pane.

myFrame.getContentPane().add(myGfxPanel).

Your menu should be added with JFrame.setJMenuBar(myMenu). Note there's a 'J' in there, there is also setMenuBar(), but that's an old relic from AWT, don't use it. If you are adding your JMenuBar that way, it really should just take care of itself. But since JMenuBars are JComponents, you can handle them "manually", which may be what you are doing. Like I said, setJMenuBar() should handle all the details for you.

You also want to use paintComponent(Graphics g) instead of paint(Graphics g). paint() is how it was done in AWT, due to some changes here and there in swing, paintComponent() is what you want. Also use super.paintComponent(g) to clear the background. All in all you use it identically to paint().

Beyond that, it's hard to say what the problem is without your code. Swing's pretty complex, and since almost everything in Swing is a Component, it's possible to go off in the wrong direction without realizing it.

Go ahead and use the Java forum. There's very little game oriented questions there, just about Java in general, and most arae quite beginner. You'd have no problems there. An even better place are Sun's forums at forums.java.sun.com, quite a few more qualified people there than on gamedev.


and don't worry about AP, he's flaming annonymously. Wow, that takes balls.

[edited by - tortoise on November 14, 2003 5:35:48 PM]

This topic is closed to new replies.

Advertisement