Why doesn't my JApplet repaint itself automatically?

Discussion in 'Java' started by Z, Feb 16, 2007.

  1. Z

    Z Guest

    The following simple JApplet doesn't repaint itself when the window is
    occluded and then exposed or when it's minimized and then maximized.

    I get the same results whether I use appletviewer or my browser.

    What am I doing wrong?


    JAppletTest.java :

    public class JAppletTest extends javax.swing.JApplet
    {
    public void paint(java.awt.Graphics g)
    {
    super.paint(g);
    g.drawString("Hello world!", 50, 50);
    }
    }


    JAppletTest.html :

    <html>
    <applet code="JAppletTest" width="200" height="100">
    </applet>
    </html>
     
    Z, Feb 16, 2007
    #1
    1. Advertising

  2. Z wrote:
    > The following simple JApplet doesn't repaint itself when the window is
    > occluded and then exposed or when it's minimized and then maximized.
    >
    > I get the same results whether I use appletviewer or my browser.
    >
    > What am I doing wrong?
    >
    >
    > JAppletTest.java :
    >
    > public class JAppletTest extends javax.swing.JApplet
    > {
    > public void paint(java.awt.Graphics g)
    > {
    > super.paint(g);
    > g.drawString("Hello world!", 50, 50);
    > }
    > }
    >
    >
    > JAppletTest.html :
    >
    > <html>
    > <applet code="JAppletTest" width="200" height="100">
    > </applet>
    > </html>


    I'm not sure why but take out the super.paint() and it will work fine.

    --

    Knute Johnson
    email s/nospam/knute/
     
    Knute Johnson, Feb 16, 2007
    #2
    1. Advertising

  3. On Feb 16, 5:14 pm, Z <> wrote:
    > The following simple JApplet doesn't repaint itself

    ....
    >
    > public class JAppletTest extends javax.swing.JApplet
    > {
    > public void paint(java.awt.Graphics g)


    Swing components should overidr painComponent()


    Andrew T.
     
    Andrew Thompson, Feb 16, 2007
    #3
  4. Andrew Thompson wrote:
    > On Feb 16, 5:14 pm, Z <> wrote:
    >> The following simple JApplet doesn't repaint itself

    > ...
    >> public class JAppletTest extends javax.swing.JApplet
    >> {
    >> public void paint(java.awt.Graphics g)

    >
    > Swing components should overidr painComponent()
    >
    >
    > Andrew T.
    >


    There is no paintComponent() for JApplet.

    --

    Knute Johnson
    email s/nospam/knute/
     
    Knute Johnson, Feb 16, 2007
    #4
  5. Z

    Chris Uppal Guest

    Andrew Thompson wrote:

    > Swing components should overidr painComponent()


    That is almost as revealing as the recent "Google is my fiend", or "Java is not
    a god choice" ;-)

    -- chris
     
    Chris Uppal, Feb 16, 2007
    #5
  6. On Feb 17, 4:50 am, Knute Johnson <>
    wrote:
    > Andrew Thompson wrote:
    > > On Feb 16, 5:14 pm, Z <> wrote:
    > >> The following simple JApplet doesn't repaint itself

    > > ...
    > >> public class JAppletTest extends javax.swing.JApplet
    > >> {
    > >> public void paint(java.awt.Graphics g)

    >
    > > Swing components should overidr painComponent()

    ...
    > There is no paintComponent() for JApplet.


    Huh! Good point.

    I am so used to putting everything
    in a JPanel (even animated rendering)
    before it goes into a ..
    JApplet/Frame/Dialog/OptionPane/Internal..
    ...you get the picture, that I never
    noticed that!

    ( I should have checked the JDocs,
    before I opened my big mouth ;)

    Andrew T.
     
    Andrew Thompson, Feb 16, 2007
    #6
  7. Z

    Z. Guest

    Knute Johnson wrote:
    > I'm not sure why but take out the super.paint() and it will work fine.


    Hmm. Very odd.

    My text says :

    "This statement [super.paint(g);] should be the first statement in every
    applet's paint method. Omitting it can cause subtle drawing errors in
    applets that combine drawing and GUI components."

    I must be making some fundamental mistake here. The JApplet and Applet
    docs at sun.com have offered no help or hints.

    Does anyone have any other ideas or suggestions?

    --
    Real Estate in '07: "It's dead, Jim."
     
    Z., Feb 18, 2007
    #7
  8. Z. wrote:
    > Knute Johnson wrote:
    >> I'm not sure why but take out the super.paint() and it will work fine.

    >
    > Hmm. Very odd.
    >
    > My text says :
    >
    > "This statement [super.paint(g);] should be the first statement in every
    > applet's paint method. Omitting it can cause subtle drawing errors in
    > applets that combine drawing and GUI components."
    >
    > I must be making some fundamental mistake here. The JApplet and Applet
    > docs at sun.com have offered no help or hints.
    >
    > Does anyone have any other ideas or suggestions?
    >


    The problem is that paint() only gets called once on a JApplet. I don't
    think it was ever intended that you draw on the JApplet and have
    components on it at the same time. If you want to do both, use a plain
    Applet. It will work just fine.

    --

    Knute Johnson
    email s/nospam/knute/
     
    Knute Johnson, Feb 18, 2007
    #8
  9. Z

    Z. Guest

    Knute Johnson wrote:
    > The problem is that paint() only gets called once on a JApplet. I don't
    > think it was ever intended that you draw on the JApplet and have
    > components on it at the same time. If you want to do both, use a plain
    > Applet. It will work just fine.


    Neing new to Java, I want to make sure I understand ... on repaint, the
    superclass (applet's) paint() gets called, not the JApplet's paint().

    Is that right?

    That would explain my problem.

    Do you have a URL or some other reference that explains what method gets
    called to repaint a JApplet?

    --
    Real Estate in '07: "It's dead, Jim."
     
    Z., Feb 18, 2007
    #9
  10. On Feb 17, 5:22 am, "Chris Uppal" <-
    THIS.org> wrote:
    > Andrew Thompson wrote:
    > > Swing components should overidr painComponent()

    >
    > That is almost as revealing as the recent "Google is my fiend", or "Java is not
    > a god choice" ;-)


    LOL! I'll upgrade my earlier
    'should have checked the JDocs' to
    'should have *copy/pasted* from the
    JDocs'!

    Andrew T.
     
    Andrew Thompson, Feb 18, 2007
    #10
  11. Z. wrote:
    > Knute Johnson wrote:
    >> The problem is that paint() only gets called once on a JApplet. I
    >> don't think it was ever intended that you draw on the JApplet and have
    >> components on it at the same time. If you want to do both, use a
    >> plain Applet. It will work just fine.

    >
    > Neing new to Java, I want to make sure I understand ... on repaint, the
    > superclass (applet's) paint() gets called, not the JApplet's paint().


    I don't know if that is true. I don't think it is.

    > Is that right?
    >
    > That would explain my problem.
    >
    > Do you have a URL or some other reference that explains what method gets
    > called to repaint a JApplet?


    No I don't. I can tell you though that mixing painting and components
    on a JApplet will be problematic. If you want to draw on something just
    put a JPanel in to draw on. I believe that the JApplet is just there to
    have a lightweight container so that you can use swing components in an
    applet.

    --

    Knute Johnson
    email s/nospam/knute/
     
    Knute Johnson, Feb 18, 2007
    #11
  12. Z

    Z Guest

    Z. wrote:
    >> The problem is that paint() only gets called once on a JApplet. I
    >> don't think it was ever intended that you draw on the JApplet and have
    >> components on it at the same time. If you want to do both, use a
    >> plain Applet. It will work just fine.


    > Being new to Java, I want to make sure I understand ... on repaint, the
    > superclass (applet's) paint() gets called, not the JApplet's paint().
    > Is that right?
    > That would explain my problem.
    > Do you have a URL or some other reference that explains what method gets
    > called to repaint a JApplet?


    http://java.sun.com/docs/books/tutorial/uiswing/painting/problems.html

    Ok, I think I've got it now... the "Painting Problems" page reads:


    Problem: The background of my applet shows up, but the foreground stuff
    doesn't show up.

    Did you make the mistake of performing painting directly in a JApplet
    subclass? If so, then your contents will be covered by the content pane
    that is automatically created for every JApplet instance. Instead,
    create another class that performs the painting and then add that class
    to the JApplet's content pane.

    So ...

    1. I removed the paint() method from my JApplet class

    2. I created a new class (Repainter), a subclass of JComponent, with a
    paintComponent() method and put my g.drawString() calls in that method

    3. in my JApplet's init(), I instantiated a Repainter and used a call to
    this.getContentPane().add() to add the Repainter to my JApplet's Content
    Pane


    It seems to be repainting correctly now, under all circumstances.

    Did I do this correctly?
     
    Z, Feb 18, 2007
    #12
  13. Z wrote:
    > Z. wrote:
    >>> The problem is that paint() only gets called once on a JApplet. I
    >>> don't think it was ever intended that you draw on the JApplet and
    >>> have components on it at the same time. If you want to do both, use
    >>> a plain Applet. It will work just fine.

    >
    >> Being new to Java, I want to make sure I understand ... on repaint, the
    >> superclass (applet's) paint() gets called, not the JApplet's paint().
    >> Is that right?
    >> That would explain my problem.
    >> Do you have a URL or some other reference that explains what method gets
    >> called to repaint a JApplet?

    >
    > http://java.sun.com/docs/books/tutorial/uiswing/painting/problems.html
    >
    > Ok, I think I've got it now... the "Painting Problems" page reads:
    >
    >
    > Problem: The background of my applet shows up, but the foreground stuff
    > doesn't show up.
    >
    > Did you make the mistake of performing painting directly in a JApplet
    > subclass? If so, then your contents will be covered by the content pane
    > that is automatically created for every JApplet instance. Instead,
    > create another class that performs the painting and then add that class
    > to the JApplet's content pane.
    >
    > So ...
    >
    > 1. I removed the paint() method from my JApplet class
    >
    > 2. I created a new class (Repainter), a subclass of JComponent, with a
    > paintComponent() method and put my g.drawString() calls in that method
    >
    > 3. in my JApplet's init(), I instantiated a Repainter and used a call to
    > this.getContentPane().add() to add the Repainter to my JApplet's Content
    > Pane
    >
    >
    > It seems to be repainting correctly now, under all circumstances.
    >
    > Did I do this correctly?
    >
    >


    Sounds good to me. I normally just use a JPanel because the constructor
    has parameters for LayoutManager and double buffering but to each his own.

    --

    Knute Johnson
    email s/nospam/knute/
     
    Knute Johnson, Feb 19, 2007
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. stan k.
    Replies:
    1
    Views:
    9,760
    Babu Kalakrishnan
    Nov 7, 2003
  2. Yoshi
    Replies:
    5
    Views:
    888
    Yoshi
    Mar 4, 2004
  3. Peace
    Replies:
    2
    Views:
    687
    Andrew Thompson
    Sep 29, 2005
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,033
    Smokey Grindel
    Dec 2, 2006
  5. zerg
    Replies:
    125
    Views:
    2,679
Loading...

Share This Page