Results 1 to 13 of 13

Thread: painting is Swing

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2004
    Posts
    222

    painting is Swing

    How to work with Graphics in Swing?
    Specially in JFrame

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: painting is Swing

    PM a moderator to move this thread. This forum is only for working samples, not asking questions.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2004
    Posts
    222

    Re: painting is Swing

    I am really sorry to post the question here.

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: painting is Swing

    Moved from Java CodeBank.

  5. #5
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: painting is Swing

    Quote Originally Posted by purusingh1
    How to work with Graphics in Swing?
    Specially in JFrame
    override the paintComponent() method.
    Code:
    public void paintComponent(Graphics g)
    {
           g.drawLine(x1,y1,x2,y2);
    }

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Mar 2004
    Posts
    222

    Re: painting is Swing

    Thanks but why doesn't it work

    setBackground(Color.RED);

  7. #7
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: painting is Swing

    Quote Originally Posted by purusingh1
    Thanks but why doesn't it work

    setBackground(Color.RED);

    For that you don't need a paintComponent method. You can insert that statementing right in the constructor.

  8. #8
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: painting is Swing

    Quote Originally Posted by purusingh1
    Thanks but why doesn't it work

    setBackground(Color.RED);

    What are you accessing the frame itself?

    you'd have to get the contentpane for the JFrame for setBackground to work.

  9. #9
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: painting is Swing

    I don't override much the painting for components.. unless I absolutely have to.

  10. #10
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: painting is Swing

    here's a sample on making setBackground on JFrames work

    http://www.vbforums.com/showthread.php?t=350197

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Mar 2004
    Posts
    222

    Re: painting is Swing

    Thanks a lot.
    I see the contentpane should be used insted of using just JFrame.set...

  12. #12
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: painting is Swing

    gud for you!. Is this now resolved?

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Mar 2004
    Posts
    222

    Re: painting is Swing

    yes it's almost resolved but I got some other problem.

    If I use

    void paint(Graphics g)
    void paintComponent(Graphics g)

    it over writes my background color.

    If I use
    getGraphics().drawLine(5,5,100,100);
    from other finctions it doesn't show.
    Last edited by purusingh1; Jul 20th, 2005 at 03:45 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
  •  



Click Here to Expand Forum to Full Width