Sign in to follow this  

[java] Painting a component - Why my buttons vanish?

This topic is 3363 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello I'm sure what I'm doing is no doube a big No-No, basically I have a JFrame with a JPanel as its child. This Jpanel has a white background - what I wanted to do was to use the method paint(Graphics g) to draw the logo image on, and then add the various buttons to the Panel. At the moment I've tested this by simply calling the drawString method from the Graphics object, but when I load everything it loads it up with no white background nor any buttons (unless I roll my mouse cursor over where they are located). Is this basically a bad thing to do? I can just use a JLabel if it is, just thought I'd try this approach. Code
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.BorderLayout;

public class LoginPanel extends JPanel{
	
	private JButton btnLogin, btnQuit;
	private JLabel imgLogo;
	public LoginPanel(){
		this.setLayout(new BorderLayout());
		this.setBackground(Color.WHITE);
		
		btnLogin = new JButton("Login");
		btnQuit = new JButton("Quit");

		this.add(btnLogin, BorderLayout.PAGE_START);
		this.add(btnQuit, BorderLayout.PAGE_END);
		
		
	}
	
	public void paint(Graphics g){
		g.drawString("Test", 50, 50);
	}
	
}

Share this post


Link to post
Share on other sites
The problem here is, that you do not paint the panel in your paint() method. That is, you should call super.paint(g) before you actually paint anything onto the panel, like this:


public void paint(Graphics g){
super.paint(g);
g.drawString("Test", 50, 50);
}

Share this post


Link to post
Share on other sites
That's a nice article however all I really wanted was this portion of it.

http://java.sun.com/products/jfc/tsc/articles/painting/#callbacks

Another useful part of the article is

http://java.sun.com/products/jfc/tsc/articles/painting/#swing_summary


I see the bottom line being, if you want child components painted for you as well as any borders painted for you then don't override paint().

If you want to override any border or children from being painted and you know what your doing then you can override paint(). It also suggests not to transform the Graphics context passed in so no translating, rotating, or scaling of the context passed in which makes sense.

Share this post


Link to post
Share on other sites
Quote:
Original post by 5MinuteGaming
It also suggests not to transform the Graphics context passed in so no translating, rotating, or scaling of the context passed in which makes sense.


You can use Graphics.create() for this, I believe. It will "clone" the graphics context and give you a separate area for component-specific transformations. Then you can dispose of it without affecting the "parent" context.

Share this post


Link to post
Share on other sites
Quote:
Original post by RobAU78
Quote:
Original post by 5MinuteGaming
It also suggests not to transform the Graphics context passed in so no translating, rotating, or scaling of the context passed in which makes sense.


You can use Graphics.create() for this, I believe. It will "clone" the graphics context and give you a separate area for component-specific transformations. Then you can dispose of it without affecting the "parent" context.

Yeah, I have used Graphics.create calls to do just that in many many situations. For instance in my engine each entity has it's own Graphics context that is created. But you can override that functionality if you want to paint outside of the entities drawing rectangle. It's really nice to use when creating your own gui system.

Share this post


Link to post
Share on other sites

This topic is 3363 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this