Results 1 to 6 of 6
  1. #1
    Nosrettap is offline Member
    Join Date
    Jan 2011
    Posts
    13
    Rep Power
    0

    Talking 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 :)

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    You must be adding the JPanel to a container (JFrame, applet, etc...), what layout are you using for that container?

  3. #3
    Nosrettap is offline Member
    Join Date
    Jan 2011
    Posts
    13
    Rep Power
    0

    Default

    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.

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    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.

  5. #5
    Nosrettap is offline Member
    Join Date
    Jan 2011
    Posts
    13
    Rep Power
    0

    Default

    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.

  6. #6
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    Your welcome.

    Quote Originally Posted by Nosrettap View Post
    Just wondering, what layout manager would you suggest to give me the most flexibility in positioning?
    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

    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?
    No.

Similar Threads

  1. JPanel and JLabels Not Repainting
    By phosphide in forum AWT / Swing
    Replies: 6
    Last Post: 11-13-2010, 03:17 AM
  2. repainting more efficiently
    By imorio in forum AWT / Swing
    Replies: 2
    Last Post: 08-24-2010, 04:24 AM
  3. Need help with array list jpanel question!!! Plz help!!!
    By helpmeplease in forum New To Java
    Replies: 17
    Last Post: 12-11-2009, 12:37 PM
  4. Repainting From Another Class
    By habester in forum New To Java
    Replies: 1
    Last Post: 11-13-2009, 02:29 AM
  5. repainting lines separately
    By rstepler in forum Java 2D
    Replies: 8
    Last Post: 07-07-2008, 02:46 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
  •