Results 1 to 6 of 6
- 01-02-2011, 05:31 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 13
- Rep Power
- 0
Question about repainting a JPanel
Hi,
My question is about (re)painting a JPanel. The problem is, right now the panel only paints sometimes (if I run the program 10 times it might only paint correctly 7 times) and when I change it slightly and try to repaint it only paints a portion of it. This is my first time using the JPanel class, so be kind, but I would appreciate any help that you could give. The relevant code is pasted below...The JFrame that it is going on is called myScreen and in it I have the code:
myLogoPanel = new LogoPanel(frameBuffers[0], frameBuffers[3], this);
this.getContentPane().add(myLogoPanel);
repaint();
public class LogoPanel extends JPanel{
private static final long serialVersionUID = 5035046207862352383L;
// Other classes
Screen myScreen;
Image myLogo;
public double myHeight;
public double myWidth;
public LogoPanel(double height, double width, Screen s){
myScreen = s;
myHeight = height;
myWidth = width;
setBounds(0,0,(int) width, (int) height);
myLogo = Toolkit.getDefaultToolkit().getImage(System.getPro perty("user.dir") + "//Images//Logo.png"); //Get the image
setVisible(true);
}
public void paintComponent(Graphics simplePen) {
super.paintComponent(simplePen);
Graphics2D pen = (Graphics2D) simplePen;
pen.drawImage(myLogo,0, 0,(int) myWidth,(int) myHeight,0, 0, myLogo.getWidth(null), myLogo.getHeight(null),null);
}
}
Thanks :)
- 01-02-2011, 07:25 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,606
- Rep Power
- 5
You must be adding the JPanel to a container (JFrame, applet, etc...), what layout are you using for that container?
- 01-02-2011, 07:34 PM #3
Member
- Join Date
- Jan 2011
- Posts
- 13
- Rep Power
- 0
I'm using a null layout because I need to be able to place my items (JPanels, images...etc) very precisely...Also, if it matters I'm also adding a variety of different JPanels to this same JFrame...Do I need a validate() method somewhere?
Last edited by Nosrettap; 01-02-2011 at 07:44 PM.
- 01-02-2011, 07:48 PM #4
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,606
- Rep Power
- 5
That is what I thought...null layouts result in many issues such as this. I'd recommend using an appropriate layout manager...see A Visual Guide to Layout Manager, setting the preferred size of each component and arranging the positions in the layout as appropriate. Null layouts may seem better in cases where precise positions is required, but I recommend avoiding using null layout as you can get similar layouts using managers and (amongst other advantages) avoid these issues entirely. My .02
Last edited by doWhile; 01-02-2011 at 07:51 PM.
- 01-02-2011, 08:10 PM #5
Member
- Join Date
- Jan 2011
- Posts
- 13
- Rep Power
- 0
Thanks doWhile!!! Just wondering, what layout manager would you suggest to give me the most flexibility in positioning? Also, if I change the layout manager will I have to alter the code in the paintComponent method of this class and other similar classes?
Last edited by Nosrettap; 01-02-2011 at 08:22 PM.
- 01-02-2011, 08:29 PM #6
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,606
- Rep Power
- 5
Your welcome.
Depends upon how you wish to lay things out. You can also have several JPanels with different layouts combined to get the best of many layouts. Look specifically at the Box, Border, and the Grid's
No.Also, if I change the layout manager will I have to alter the code in the paintComponent method of this class and other similar classes?
Similar Threads
-
JPanel and JLabels Not Repainting
By phosphide in forum AWT / SwingReplies: 6Last Post: 11-13-2010, 03:17 AM -
repainting more efficiently
By imorio in forum AWT / SwingReplies: 2Last Post: 08-24-2010, 04:24 AM -
Need help with array list jpanel question!!! Plz help!!!
By helpmeplease in forum New To JavaReplies: 17Last Post: 12-11-2009, 12:37 PM -
Repainting From Another Class
By habester in forum New To JavaReplies: 1Last Post: 11-13-2009, 02:29 AM -
repainting lines separately
By rstepler in forum Java 2DReplies: 8Last Post: 07-07-2008, 02:46 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks