Skip to content Skip to sidebar Skip to footer

How to Continue Call the Paintcomponent Method in Java

repaint() is not calling paintComponent

843805

843805 Member Posts: 49,999 Green Ribbon

Hi

I writing a code where it has two classes in the same file. In the other class (which does not have the main method) i'm using the repaint() method but it does not call the paintComponent method which i've over-ridden.

I've tried revalidate but even then it doesn't call.

Does anyone know what the problem might be?

Thanks

Comments

  • camickr

    Does anyone know what the problem might be?

    A coding problem.

    If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

    And don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags so the code retains its original formatting.

  • 843805

    843805 Member Posts: 49,999 Green Ribbon

    ok...i've been trying this for a long time now.... in different ways ...

    the only time i can draw is - if i write my code inside the paint method and then using repaint i can call the method.... but paint has different affects on my software (for e.g. the whole window becomes the same colour) .

    i really need to work on the paintComponent but i just cannot call it using repaint. is there any other way i can call the paintComponent method. ...

    Thanks

  • camickr

    i really need to work on the paintComponent but i just cannot call it using repaint.

    Yes you can. Your code is wrong!!!!

  • 843805

    843805 Member Posts: 49,999 Green Ribbon

    ok...so could you give me some example of it when it doesnt work or any ideas ....

  • 843805

    843805 Member Posts: 49,999 Green Ribbon

    ok...so could you give me some example of it when it
    doesnt work or any ideas ....

    1,000,000 plus ways to do it wrong. One way you are doing it wrong. Is it better for us to post 1,000,000 plus wrong approaches for you to go through OR for you to post your single wrong approach for us to take a look at?

  • 843805

    843805 Member Posts: 49,999 Green Ribbon

    If you need further help then you need to create a
    [url
    http://homepage1.nifty.com/algafield/sscce.html]Short,
    Self Contained, Compilable and Executable, Example
    Program (SSCCE) that demonstrates the incorrect

    behaviour, because I can't guess exactly what you are
    doing based on the information provided.

    1,000,000 plus ways to do it wrong. One way you are >doing it wrong. Is it better for us to post 1,000,000 plus >wrong approaches for you to go through OR for you to >post your single wrong approach for us to take a look at?

    Okay .. i have made a simple small program from my software to show you how mine is working:

                        layeredPane.addMouseListener(new java.awt.event.MouseAdapter()         {             public void mousePressed(java.awt.event.MouseEvent evt)             {                 layeredPaneMousePressed(evt);             }         });

    The above code calls the below when the mouse is clicked

                        private void layeredPaneMousePressed(java.awt.event.MouseEvent evt)                                              {                                                      Component c = layeredPane.findComponentAt(evt.getX(),evt.getY());          	//============This is where i am trying to store the drawing in a JPanel ...          if (icoIndex==0)//When the user has selected the SELECT icon and pressed         {                                     Graphics g = layeredPane.getGraphics();             //g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);                          //test             Vector v = new Vector(), vt[] = new Vector[2];                          vt[0] = new Vector();             vt[1] = new Vector();                          v.add(3);             v.add(1);                           int x[] = {20,50,200,100,300,50,45};             int y[] = {40,100,40,300,60,80,35};                          for(int i = 0; i<2; i++)             {                 for(int j=0; j< x.length; j++)                 {                     if (i==0)                     {                         vt<em>.add(x[j]);<br /> }<br /> else<br /> {<br /> vt[i].add(y[j]);<br /> }<br /> }<br /> }<br /> <br /> DrawPanel dp = new DrawPanel(vt);<br /> <br /> vPoly.add(dp.getPolyLine());<br /> //add to the layer and locate<br /> layeredPane.add(dp, new Integer(2));//Lies on top of all other components<br /> repaint(); //Not calling the paintComponent()<br /> <br /> }<br /> <br /> } <br /> <br /> <br /> <br /> Below is the exact paintComponent method which i'm using...but the super.paintComponent is throwing an error<br />  <pre class="jive-pre"><code class="jive-code">    public void paintComponent(Graphics g)     {         //super.paintComponent(g);         //layeredPane.setOpaque(false);         g = layeredPane.getGraphics();         Graphics2D g2 = (Graphics2D) g;         g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);         g2.draw((Shape) vPoly.elementAt(0));     }

    The below error is thrown when i use the super.paintComponent(g);

    symbol  : method paintComponent(java.awt.Graphics) location: class javax.swing.JFrame         super.paintComponent(g); Note: D:\Usama\NetBeans\MTT\src\First.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. 1 error

    Message was edited by:
    getusama

    just wanted to add that:

    The repaint() method does not call the paintComponent but if I change it to paint it does (in both cases i could not use the "super.paintComponent(g);"). When I change the paintComponent method to paint the I do not get the required result.

  • 800774

    Just a guess since this is not a SSCCE, and we can't see the context of paintComponent.
    Are you trying to override paintComponent of JFrame?
    Because JFrame doesn't have a paintComponent function it might be a problem calling super.paintComponent...

  • 843805

    843805 Member Posts: 49,999 Green Ribbon

    Just a guess since this is not a SSCCE, and we can't
    see the context of paintComponent.
    Are you trying to override paintComponent of JFrame?
    Because JFrame doesn't have a paintComponent function
    it might be a problem calling super.paintComponent...

    yes you are right ... my class extends JFrame.... so paintComponent is in JComponent and paint is in Container. But I thought you could still use those since its inherited.

    Could you now please tell me how i can make use of my paintComponent() to call through repaint()?

    Thanks Alot

  • camickr

    Just a guess since this is not a SSCCE, and we can't see the context
    of paintComponent. Are you trying to override paintComponent of JFrame?

    That was my guess as well. I was just waiting for the OP to actually post a SSCCE. I was waiting to see the code so I could point out how the context of how the code used is just as important as the code itself. Just seeing paintComponent() method does not tell us what component is being overridden. The OP does still not have an idea of what a SSCCE even though he has been asked multiple times to produce one.

    Instead of taking time to create a simple SSCCE, the OP would rather multipost questions on the forum hoping someone will eventually waste time making random guesses.

    I don't take this attitude on first time offenders, only repeat offenders.

  • 843805

    843805 Member Posts: 49,999 Green Ribbon

    I don't take this attitude on first time offenders,
    only repeat offenders.

    hey, i already said that was a mistake thinking that may be if i worked on the error message it might have helped and that is why i started a new post then but i have been posting here since you mentioned ....

This discussion has been closed.

calhounprou1949.blogspot.com

Source: https://community.oracle.com/tech/developers/discussion/1374172/repaint-is-not-calling-paintcomponent

Post a Comment for "How to Continue Call the Paintcomponent Method in Java"