Results 1 to 7 of 7
  1. #1
    v1nsai's Avatar
    v1nsai is offline Member
    Join Date
    Mar 2009
    Posts
    28
    Rep Power
    0

    Default a few questions about JPanel

    I'm still working on getting my head around JPanel and JFrame, so I was wondering if the paintComponent can be called on a JPanel inside of a JFrame. I was having some trouble putting a background on the window of my program, and while searching I came across an example that did this:

    Java Code:
    public class something extends JFrame
    {
       public something()
       {
           JPanel bg = new JPanel()
           {
               this.paintComponent( Graphics g )
               Image img = new ImageIcon( "image.jpg" ).getImage();
               g.drawImage( img, 0, 0, null );
            }
        }
    }
    This of course did not compile for me, though in the author's defense it may be from an old version of Java. It got me thinking however if it would be possible to do something like that, and call a JPanel inside of a JFrame and paint just the panel. I've only used JPanels and JFrames as extensions of classes, I've never used them directly inside of each other.
    C:\DOS > C:\DOS.RUN > RUN.DOS.RUN

  2. #2
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    It doesn't compile because it's horribly mangled and broken. It could never have compiled.

    I suggest creating the image in the panel constructor and making 'img' a class field. Then override the panel's paintComponent and call the g.drawImage method from there.

    Here's a quick way:
    Java Code:
    JPanel panel = new JPanel() {
        Image img = new ImageIcon("image.jpg").getImage();
    
        protected void paintComponent(Graphics g) {
            g.drawImage( img, 0, 0, null );
        }
    };
    Last edited by dlorde; 08-11-2009 at 01:21 PM.

  3. #3
    v1nsai's Avatar
    v1nsai is offline Member
    Join Date
    Mar 2009
    Posts
    28
    Rep Power
    0

    Default

    Cool, that makes perfect sense. I actually managed to find the post that I got that from, and it was from June 2009 -_-

    I'm curious though, if I had some content laid out on a JFrame and I wanted to add a JPanel to only a section of it, and paint just that section how could I call that? It's a purely hypothetical question I'm just trying to get an idea of how to manipulate this object precisely.
    C:\DOS > C:\DOS.RUN > RUN.DOS.RUN

  4. #4
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    Quote Originally Posted by v1nsai View Post
    I'm curious though, if I had some content laid out on a JFrame and I wanted to add a JPanel to only a section of it, and paint just that section how could I call that? It's a purely hypothetical question I'm just trying to get an idea of how to manipulate this object precisely.
    I'm not quite sure what you mean - if you want to custom paint a single panel on a frame with multiple panels, just override the paintComponent method of that panel... You can also override the painting of the panels associated with the frame itself (content pane, glass pane, etc). The glass pane allows you to paint on top of everything else, e.g. using transparency.

  5. #5
    v1nsai's Avatar
    v1nsai is offline Member
    Join Date
    Mar 2009
    Posts
    28
    Rep Power
    0

    Default

    I meant something like that, basically creating a small panel that covers only a section of a frame and painting just that. It's a lot simpler now then it seemed at the time I wrote that hehe.

    I have another inquiry though, I'm using JPanel to paint a background on my program and I was curious how I could stretch the image to the size of the JFrame that it sits on and have it resize with the window. I haven't found the right way to phrase that for google and haven't figured it out...
    C:\DOS > C:\DOS.RUN > RUN.DOS.RUN

  6. #6
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    If the panel sizes to the frame and you paint the image to fill the panel, I don't see where the problem is...

    But you could add a ComponentListener to the panel and repaint the image in the ComponentResized method.
    Last edited by dlorde; 08-22-2009 at 10:38 AM.

  7. #7
    v1nsai's Avatar
    v1nsai is offline Member
    Join Date
    Mar 2009
    Posts
    28
    Rep Power
    0

    Default

    By simply painting the Panel, the image stayed one size, and when I resized the window beyond the size, the panel was simply grey. I wanted the image to stay stretched across the whole panel and always be corner-to-corner with it.

    I was able to get my desired effect by using this code:
    Java Code:
    class BackgroundPanel extends JPanel
    {
      Image image;
      public BackgroundPanel()
      {
        try
        {
          image = javax.imageio.ImageIO.read(new java.net.URL(getClass().getResource("Test.gif"), "Test.gif"));
        }
        catch (Exception e) { /*handled in paintComponent()*/ }
      }
    
      @Override
      protected void paintComponent(Graphics g)
      {
        super.paintComponent(g); 
        if (image != null)
          g.drawImage(image, 0,0,this.getWidth(),this.getHeight(),this);
      }
    }
    Works like a charm
    C:\DOS > C:\DOS.RUN > RUN.DOS.RUN

Similar Threads

  1. Some Questions
    By MuslimCoder in forum New To Java
    Replies: 2
    Last Post: 02-25-2009, 04:01 PM
  2. I have Questions -_-
    By ChazZeromus in forum New To Java
    Replies: 5
    Last Post: 09-13-2008, 08:08 PM
  3. Questions About JSP?
    By mtz1406 in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 08-19-2008, 07:56 PM
  4. Just a Few Questions
    By pringle in forum New To Java
    Replies: 21
    Last Post: 01-09-2008, 06:21 PM
  5. questions
    By Gilgamesh in forum New To Java
    Replies: 3
    Last Post: 11-27-2007, 11:18 PM

Posting Permissions

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