   [java] Painting a component - Why my buttons vanish?
  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. Codeimport 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);}}
  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);};  Also you should override paintComponent not paint.   ;Quote:Original post by CaptainJesterAlso you should override paintComponent not paint.What is the benefit in this situation of overriding paintComponent versus just paint?;  http://java.sun.com/products/jfc/tsc/articles/painting/   ;  That's a nice article however all I really wanted was this portion of it.http://java.sun.com/products/jfc/tsc/articles/painting/#callbacksAnother useful part of the article ishttp://java.sun.com/products/jfc/tsc/articles/painting/#swing_summaryI 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.   ;Quote:Original post by 5MinuteGamingIt 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.   ;Quote:Original post by RobAU78Quote:Original post by 5MinuteGamingIt 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.   