0

I am making a starting screen, and it's working out pretty fine, got a background image when it starts, now I am trying to draw a JButton on the startmenu, which is a JFrame. But when I run my program, the button appears behind the background picture. If I hover above the area where the button is placed, it's flickering, and when I click it that happens too. Is there any way to draw the Button INFRONT of the background? I made the button as last in the code. My code to draw the background and button:

    public void drawStartScreen(){
    startScreenOn = true;
    Graphics2D b = buffer.createGraphics();
    b.setColor( Color.WHITE );
    b.fillRect(0, 0, 800, 600);
    b.drawImage(start,0,0,null);

    setLayout( null );
    button = new JButton("Start Game");
    button.setBounds(10,10,100,100);
    button.setVisible( true );
    add(button);
}

It draws the image first, and then the Button, but the button still draws behind the image.

jzd
  • 23,473
  • 9
  • 54
  • 76
Stan
  • 3,659
  • 14
  • 35
  • 42
  • did you implement `drawStartScreen()` in a `JFrame`? – Asaf Apr 12 '11 at 13:50
  • 2
    And what do you want the Stackoverflowers to do with that ? Imagine which image would better suit your application ? Before to go any further, please go read http://msmvps.com/blogs/jon_skeet/archive/2010/08/29/writing-the-perfect-question.aspx – Riduidel Apr 12 '11 at 13:51
  • Well, I think I explained it pretty well that the JButton is drawing behind my Image, and I am asking how to draw it infront of the button. @Asaf yes, the image appears and such, it's just that the button is drawing behind the image. – Stan Apr 12 '11 at 13:54
  • 1
    @Stan "..I think I explained it pretty well .." At least 2 people seem to think differently, but since it is your question, do you not think it is in **your** best interests to explain it as well as possible to **any** person that might show an interest? For that matter, I recommend to post an [SSCCE](http://pscode.org/sscce.html). Feel free to hot-link to some of the [images on my media page](http://pscode.org/media/#image) for the image. – Andrew Thompson Apr 12 '11 at 13:59
  • @Stan "just drawing behind" implies you'd benefit from reading about Swing's Component hierarchy (specifically, how to `add()` components to containers). you can see that the answers below talk about how you mix two levels of Swing's API. – Asaf Apr 12 '11 at 14:01
  • `setLayout( null );` If not fundamental to the problem at hand, that will cause problems later. Learn [how to use layouts](http://download.oracle.com/javase/tutorial/uiswing/layout/index.html). – Andrew Thompson Apr 12 '11 at 14:03
  • @Stan `setBounds()`/`setVisible()` should also be be called by the Layout in this basic example, not by you code. – Asaf Apr 12 '11 at 14:07

3 Answers3

2

You are mixing painting and adding of components and you definitely shouldn't be doing this. Instead add components when you create the screen or when you first need them but make sure you are only doing this once. Then separate modify the components that need painting changes inside the paintComponent() method.

jzd
  • 23,473
  • 9
  • 54
  • 76
  • 1
    Be sure to `dispose()` the graphics context when you are done painting the background, too. – Chris Apr 12 '11 at 14:03
  • Well, I tried putting it in a if-statement, which runs when "startScreenOn" is true. But it still draws behind the image. – Stan Apr 12 '11 at 14:06
  • @Stan, you are making people guess about what you are doing. Please create an SSCCE demonstrating your problem. It is very unclear what you are actually doing. – jzd Apr 12 '11 at 14:09
2

I recommend you'd use a JLayeredPane (I go for custom painting only as a last resort).

If you're still interested in mixing the 'low-level' painting with 'higher-level' JComponent hierarchy, look at a question about a JFrame that has multiple layers.

Community
  • 1
  • 1
Asaf
  • 2,480
  • 4
  • 25
  • 33
0

Override the paint method on the JFrame:

@Override
public void paint(Graphics g) {
    super.paint(g);
    Graphics2D b = (Graphics2D)g;
    b.setColor( Color.WHITE );
    b.fillRect(0, 0, 800, 600);
    b.drawImage(start.getImage(),0,0,null);
    b.dispose();        
}

Notice that this calls paint() on the parent and dispose() on the graphics context when done. I just tried this code and it worked for me.

Chris
  • 694
  • 3
  • 18
  • For the sample, __start__ is declared as an ImageIcon which is why `getImage()` is called. Sorry about that. – Chris Apr 12 '11 at 14:34
  • Just so you know, i'm using BufferedImages, so getImage() won't work. I tried putting in the example you gave me, the button still pop-ups behind the image. – Stan Apr 13 '11 at 17:22
  • Did you remove your original block similar to that which I put in `paint()`? Yesterday, I just created a new Java project, added a JFrame, added the method above, used your button code, and it worked. If I get a chance, I can try to regenerate it later. – Chris Apr 13 '11 at 20:18
  • I already fixed this, it was something with the BufferedImage, which caused it. – Stan Apr 13 '11 at 20:19