Results 1 to 10 of 10
Like Tree3Likes
  • 2 Post By DarrylBurke
  • 1 Post By quad64bit

Thread: Painting an JFrameBackground

  1. #1
    Join Date
    Oct 2012
    Location
    San Saba Tx.
    Posts
    8
    Rep Power
    0

    Default Painting an JFrameBackground

    I need to paint an image to a J Frame background but having trouble doing so i actually pretty clueless i need and example of how to do it please if possible id like to put it into a method where all i have to do is put the object to paint the background to and what i want to paint to it. If that is possible please tell me Thank in advance!

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Painting an JFrameBackground

    quad64bit and ChaoticXknight like this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default Re: Painting an JFrameBackground

    I agree with Darryl, personally I extend JPanel for all painting because of the flexibility, simplicity, and modularity of it. You can then have many individual JPanels in your gui, or one simple one, but it lends itself well to code reuse and extendability.

  4. #4
    Join Date
    Oct 2012
    Location
    San Saba Tx.
    Posts
    8
    Rep Power
    0

    Default Re: Painting an JFrameBackground

    How exactly do i extend JPanel for painting? I'm really new to java so i don't quit know how to do all of this yet.

  5. #5
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default Re: Painting an JFrameBackground

    You just need to make a new class which extends JPanel, then override the paintComponent() method. A simple example:
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    
    public class DrawDemo{
        public static void main(String[] args){
            new DrawDemo();
        }
        
        public DrawDemo(){
            JFrame window = new JFrame();
            MyCanvas canvas = new MyCanvas();
            canvas.setPreferredSize(new Dimension(640, 480));
            window.add(canvas);
            window.pack();
            window.setVisible(true);
        }
        
        class MyCanvas extends JPanel{
            @Override
            public void paintComponent(Graphics g){
                super.paintComponent(g);
                g.setColor(Color.BLUE);
                g.fillRect(100, 100, 50, 50);
                g.dispose();
            }
        }
    }
    Last edited by quad64bit; 10-30-2012 at 07:20 PM. Reason: typo
    ChaoticXknight likes this.

  6. #6
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default Re: Painting an JFrameBackground

    I should have mentioned in my post above - this is a nice technique because should you need to repaint the screen (if a ball is bouncing around or something), then calling window.repaint() will repaint all nested components for you! No need to repaint the JPanel itself, that happens as part of the window repaint.

  7. #7
    Join Date
    Oct 2012
    Location
    San Saba Tx.
    Posts
    8
    Rep Power
    0

    Default Re: Painting an JFrameBackground

    can Graphics g be an image i have i believe it would need to be a png.

    and if i create a new swing application using eclipse where would i put this code, i dont know if im doing it wrong or putting it in the wrong place because it changes nothing on the screen when i run the program?
    Last edited by ChaoticXknight; 11-02-2012 at 12:18 AM.

  8. #8
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default Re: Painting an JFrameBackground

    Quote Originally Posted by ChaoticXknight View Post
    can Graphics g be an image i have i believe it would need to be a png.

    and if i create a new swing application using eclipse where would i put this code, i dont know if im doing it wrong or putting it in the wrong place because it changes nothing on the screen when i run the program?
    No, but you can draw a png with a graphics object. Look at the API for Graphics.

  9. #9
    Join Date
    Oct 2012
    Location
    San Saba Tx.
    Posts
    8
    Rep Power
    0

    Default Re: Painting an JFrameBackground

    Thanks i finally got it to work i appreciate the quick responses!!!
    and can i do this with other components then JPanel.
    Last edited by ChaoticXknight; 11-02-2012 at 06:09 PM.

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Painting an JFrameBackground

    It rather looks like you didn't go through the painting tutorial I linked in the first response to your question.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. painting whole label
    By simprepol in forum AWT / Swing
    Replies: 9
    Last Post: 01-27-2011, 08:10 AM
  2. Painting Problem!
    By Jcbconway in forum Advanced Java
    Replies: 3
    Last Post: 11-17-2010, 04:14 AM
  3. Painting in SWT
    By jionnet in forum SWT / JFace
    Replies: 9
    Last Post: 09-24-2010, 06:52 AM
  4. Painting
    By xael in forum New To Java
    Replies: 6
    Last Post: 09-06-2010, 05:10 AM
  5. Need help with synchronous painting
    By JavaRulez in forum Advanced Java
    Replies: 7
    Last Post: 05-09-2010, 08:34 AM

Tags for this Thread

Posting Permissions

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