-2

I'm working on a 2D game and am now stumped on a drawing issue. I'm repainting the JPanel with the background image and characters every frame, but the GUI is created in a setup function. I think that causes the JButton to appear behind the background image, and its worth mentioning this class extends JPanel. I'm using setBounds in order to arrange GUI elements a special way. What is a possible solution to this issue?

/** Draw game to the screen **/
private void gameDraw() {
    Graphics g2 = this.getGraphics();
    g2.drawImage(image, 0, 0 ,null);
    g2.dispose();
}

public void SetupUI() {
    uploadButton = new JButton("UPLOAD");
    uploadButton.setBounds(WIDTH / 2, HEIGHT - 40, 50, 30);
    uploadButton.setToolTipText("Press this button to upload your AI to the ship");
    uploadButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            //TO DO: Make upload button compile and run the users code
        }
    });

    this.add(uploadButton);
    this.setVisible(true);
}
Benifir
  • 24
  • 5
  • 4
    You need to take a very close look at [Performing Custom Painting](https://docs.oracle.com/javase/tutorial/uiswing/painting/), in particular [Painting in AWT and Swing](http://www.oracle.com/technetwork/java/painting-140037.html) to understand how painitng actually works in Swing. Please, don't use `getGraphics`, it's capable of returning `null` and anything you paint to will be removed during the next normal paint cycle. This likely also explains why you're having issues, as you're basically painting over the button – MadProgrammer Jan 13 '17 at 02:45
  • 4
    `setBounds` also suggest that you're using a `null` layout, this is going to cause a lot of issues which are better managed/solved by the layout management API – MadProgrammer Jan 13 '17 at 02:45
  • 1
    1) For better help sooner, post a [MCVE]. 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). 3) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). .. – Andrew Thompson Jan 13 '17 at 11:55
  • 1
    .. 4) *"..arrange GUI elements a special way.."* Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height. – Andrew Thompson Jan 13 '17 at 11:56
  • I've switched to using a layout manager rather than setting the bounds directly. It's not exact but it is similar to what I had originally planned. I'll attempt to do some more tests to see if I can solve this then report back for anyone else having similar issues. – Benifir Jan 13 '17 at 23:37
  • Tip: Add @MadProgrammer (or whoever, the `@` is important) to *notify* the person of a new comment. *"I'll attempt to do some more tests to see if I can solve this then report back for anyone else having similar issues."* That's the attitude. Note that the best approach to fixing the main problem is in that first comment, and the links to the tutorial. – Andrew Thompson Jan 14 '17 at 14:06

1 Answers1

0

I decided to redo the code for rendering the game component. The issue seems to have been the GUI button and the image sharing the same screen space, then the image redrawing over the button each frame. What I was trying to go for was the gui overlaid over the image, but I still do not know how to achieve that.

Instead I created two separate JPanels, one to hold the GUI elements in a 'ribbon' above the image, and one to hold the image where neither panels are overlapping then packed both of those into another panel which was then assigned to the JFrame (feel like this is a scene from the Emperors New Groove lol) . Each panel has a layout assigned. The image in its own panel can now be rendered as frequently as needed without interfering with the gui. Just remember to draw whatever images you want in order e.g. draw the background, then the player.

I couldn't solve the original problem, but this is a decent workaround.

Benifir
  • 24
  • 5