Results 1 to 6 of 6
Thread: Hidden Button after paint()
- 09-14-2009, 12:41 AM #1
Senior Member
- Join Date
- Jan 2009
- Posts
- 119
- Rep Power
- 0
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.
-
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)?
- 09-14-2009, 06:18 AM #3
Senior Member
- Join Date
- Jan 2009
- Posts
- 119
- Rep Power
- 0
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(); } }
-
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.
- 09-14-2009, 05:24 PM #5
Senior Member
- Join Date
- Jan 2009
- Posts
- 119
- Rep Power
- 0
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.
-
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
-
need to adjust a code I have to work with hidden fields
By carag in forum New To JavaReplies: 2Last Post: 07-29-2009, 12:23 PM -
Some confusions with paint()
By kendaop in forum Java AppletsReplies: 1Last Post: 01-24-2009, 12:23 AM -
finished paint!
By diggitydoggz in forum New To JavaReplies: 3Last Post: 01-04-2009, 10:33 AM -
other than paint repaint
By amith in forum Java 2DReplies: 1Last Post: 07-01-2008, 11:39 PM -
paint() and paintComponent()
By goldhouse in forum Java 2DReplies: 1Last Post: 07-17-2007, 03:43 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks