Results 1 to 6 of 6
  1. #1
    MuslimCoder is offline Senior Member
    Join Date
    Jan 2009
    Posts
    119
    Rep Power
    0

    Question Hidden Button after paint()

    I am working on a program that needs to paint a background at the back, and some buttons in the middle. (JButtons that is). However, when the program runs the background is printed by the JFrame, however it ends up on top of the buttons. Additionally, something odd is when I move the cursor to the position of the button, it beomces visible.
    I am thinking it is a paint err or bug, but dont know the solution.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Yep, you are correct in that you've got a bug in your code, but without seeing code, it's tough to tell exactly where or what it is or how to offer you a solution.

    Your background painting is done in the paintComponent method of a JPanel, correct? If so, is the first method called here to super.paintComponent(g)?

  3. #3
    MuslimCoder is offline Senior Member
    Join Date
    Jan 2009
    Posts
    119
    Rep Power
    0

    Default

    No, I actually used a paint() methods, to paint a JPEG image using the paintIcon() method.

    Here it the code though:
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class SampleMenu extends JFrame
    {
    	JButton[] cmd = new JButton[5];
    	ImageIcon bg;
    	JPanel cont = new JPanel();
    	
    	GridBagLayout gb = new GridBagLayout();
    	GridBagConstraints gc = new GridBagConstraints();
    	
    	public void paint(Graphics g)
    	{
    		super.paint(g);
    		
    		bg = new ImageIcon("bg2.jpg"); //image
    		
    		bg.paintIcon(this,g,0,0);
    	
    		
    		
    	}
        public SampleMenu()
        {
        	
          
        	
        	super("Tic Tac Toe Evolved");
        //	setLayout(new GridBagLayout());
        //	getContentPane().setLayout(gb);
        	cont.setLayout(gb);
        	gc.gridx =1;
        	gc.gridy = 1;
        	gc.weightx = 0;
        	gc.weighty = 0;
        
        //	gc.setConstraints(gc);
      	cont.add(new JButton("SinglePlayer"),gc);
      	gc.gridx =1;
        	gc.gridy = 2;
        	gc.weightx = 0.4;
        	gc.weighty = 0.4;
      	cont.add(new JButton("Multiplayer"),gc);
      	gc.gridx =1;
        	gc.gridy = 3;
        	gc.weightx = 0.4;
        	gc.weighty = 0.4;
      	cont.add(new JButton("Multiplayer"),gc);
      	gc.gridx =1;
        	gc.gridy = 4;
        	gc.weightx =0.4;
        	gc.weighty = 0.4;
      	cont.add(new JButton("Multiplayer"),gc);
      	repaint();
        	
        	add(cont);
        	setSize(960,720);
        	setVisible(true);
        	//setResize(false);
        	
        }
        public static void main(String args[])
        {
        	SampleMenu app = new SampleMenu();
        	app.repaint();
        	
        }
        
        
    }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You've got your painting all wrong, and this is what is messing you up.
    1) Do not paint in JFrame unless you have a very good reason to do so and know what you're doing.
    2) Instead paint in a JPanel, in this case you can paint in a JPanel and use it as the JFrame's contentPane.
    3) Don't paint in the JPanel's paint method but rather in its paintComponent method. The first method call of this method should be super.paintComponent(g).
    4) Do not read in image files (or any files) from within a paint or paintComponent method. Doing this will slow down your app's painting immensely. Here you could read in the image in the class's constructor instead.
    5) Read the Sun Swing tutorials on painting in Swing.

    Much luck.

  5. #5
    MuslimCoder is offline Senior Member
    Join Date
    Jan 2009
    Posts
    119
    Rep Power
    0

    Default Thanks

    Thanks , the heads up was really helpful.<smile>

    Though I still don't know the reason why the JFrame paint() method should be avoided.but I would check it up. if you spare any explanations, it would be great.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    A JFrame is responsible for many things including providing a framework by which native (operating system generated) windows can display Java-generated GUI widgets. It is composed of a root pane, layered pane, content pane, glass pane, and menubar. If you draw on it, you have to take these components into account which your code doesn't do. Also if you draw in the JFrame, you're essentially drawing in a heavy weight AWT widget, not a light weight Swing widget, since JFrames are based on Frame, and you lose many of the advantages that are available when drawing in Swing (such as automatic double buffereing).

    Have a look here for more on JFrames: http://java.sun.com/docs/books/tutor.../toplevel.html

Similar Threads

  1. Replies: 2
    Last Post: 07-29-2009, 01:23 PM
  2. Some confusions with paint()
    By kendaop in forum Java Applets
    Replies: 1
    Last Post: 01-24-2009, 01:23 AM
  3. finished paint!
    By diggitydoggz in forum New To Java
    Replies: 3
    Last Post: 01-04-2009, 11:33 AM
  4. other than paint repaint
    By amith in forum Java 2D
    Replies: 1
    Last Post: 07-02-2008, 12:39 AM
  5. paint() and paintComponent()
    By goldhouse in forum Java 2D
    Replies: 1
    Last Post: 07-17-2007, 04:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •