Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By doWhile

Thread: paintComponent function problem

  1. #1
    max1 is offline Member
    Join Date
    Feb 2012
    Posts
    4
    Rep Power
    0

    Default paintComponent function problem

    Hi guys I'm learing about Graphic in Java and I have a problem , because my paintComponent
    doesn't work I mean that complier doesn't see ma function and program is performed without paint function



    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Graphics;
    import javax.swing.JFrame;
    import javax.swing.JPanel;

    public class Figure extends JPanel{

    private JPanel panel;
    Figure()
    {
    setLayout( new BorderLayout() );
    panel = new JPanel();
    panel.setBackground(Color.YELLOW);
    add(panel, BorderLayout.CENTER);
    }

    public void paintComponent (Graphics g )
    {
    super.paintComponent(g);
    g.setColor(Color.RED);
    g.drawRect(45, 45, 45, 45);
    }

    public static void main( String [] args )
    {
    JFrame f = new JFrame("Name");

    Figure k = new Figure();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(300, 200);
    f.setVisible(true);
    f.add(k);
    }
    }

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

    Default Re: paintComponent function problem

    Hi guys I'm learing about Graphic in Java and I have a problem , because my paintComponent
    doesn't work I mean that complier doesn't see ma function and program is performed without paint function
    How do you know it "doesn't work"? Add some println's in the paintComponent method and see if the what you add is printed to the console.

  3. #3
    max1 is offline Member
    Join Date
    Feb 2012
    Posts
    4
    Rep Power
    0

    Default Re: paintComponent function problem

    How do you know it "doesn't work"? Add some println's in the paintComponent method and see if the what you add is printed to the console.
    I tried but sill is yellow panel without any drawing text or shapes

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

    Default Re: paintComponent function problem

    Quote Originally Posted by max1 View Post
    I tried but sill is yellow panel without any drawing text or shapes
    Adding a System.out.println statement won't fix it...it lets you know whether paintComponent is being called or not and is an invaluable tool in stepping through and debugging code...in your case you think paintComponent isn't being called - this will tell you whether it is or not. Post back the result. Whether it does or not will indicate what you should do to fix the problem.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: paintComponent function problem

    Add components to a GUI hierarchy before, rather than after, making it visible.

    Java Code:
    // f.setVisible(true);
     f.add(k);
    f.setVisible(true);
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    max1 is offline Member
    Join Date
    Feb 2012
    Posts
    4
    Rep Power
    0

    Default Re: paintComponent function problem

    "Adding a System.out.println statement won't fix it..."
    "Add components to a GUI hierarchy before, rather than after, making it visible.

    Java Code:

    // f.setVisible(true);
    f.add(k);
    f.setVisible(true);"
    I tried both this methods but it wasn't change anything , but when I changed paintComponent() to paint(), it works correctly drawing this things what I want,
    but all this stuff are drawing at default JFrame background rather then yellow JPanel

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

    Default Re: paintComponent function problem

    Quote Originally Posted by max1 View Post
    I tried both this methods but it wasn't change anything , but when I changed paintComponent() to paint(), it works correctly drawing this things what I want,
    but all this stuff are drawing at default JFrame background rather then yellow JPanel
    Did you read my post? Adding println' won't fix it, but give you information to help you 'fix' it. And yes, changing paintComponent to paint will 'fix' the problem given the inherent way that paint works (calling super.paint at the start of the paint method will paint the children before you paint over the children - one of the reasons its advised not to override the paint method). I am not sure why you are adding a JPanel to make the background Yellow instead of just painting yellow.

  8. #8
    max1 is offline Member
    Join Date
    Feb 2012
    Posts
    4
    Rep Power
    0

    Default Re: paintComponent function problem

    I am not sure why you are adding a JPanel to make the background Yellow instead of just painting yellow.
    I want to do a simple game which will consist of to parts first - label (JLabel) which will be show a score and second part will be (JPanel) area where when a player click on viewing element they change result on a label score. But now I am not sure if I must put this two part together to JFrame or it is anther way to do this.

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

    Default Re: paintComponent function problem

    Quote Originally Posted by max1 View Post
    I want to do a simple game which will consist of to parts first - label (JLabel) which will be show a score and second part will be (JPanel) area where when a player click on viewing element they change result on a label score. But now I am not sure if I must put this two part together to JFrame or it is anther way to do this.
    If I understand correctly, then combine the components using an appropriate LayoutManager. It should not be necessary to rely on Components painting over one another as the example above demonstrates, rather create each one and rely on a LayoutManager to place them accordingly.
    Fubarable likes this.

Similar Threads

  1. paintComponent problem
    By luke in forum New To Java
    Replies: 5
    Last Post: 04-02-2011, 03:07 PM
  2. Problem regarding function
    By sudarson in forum New To Java
    Replies: 8
    Last Post: 02-26-2011, 11:28 PM
  3. Problem with split function
    By a.tajj in forum New To Java
    Replies: 4
    Last Post: 04-14-2009, 04:30 AM
  4. Problem going outside paintComponent
    By Thez in forum Java 2D
    Replies: 9
    Last Post: 12-08-2007, 05:59 PM
  5. Function declaration problem.
    By snooze-g in forum Advanced Java
    Replies: 3
    Last Post: 07-18-2007, 10:15 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
  •