Hi all, I am experimenting with JApplet and JPanel.
My JApplet contains a JPanel called jpanel1 size 210x210.
I created a class myPanel which extends JPanel and I set its size to
210x210.
myPanel contains a Canvas called canvas1 and its size is 210x210.
But when I run the applet and draw an image to the canvas1, it is not
visible.
I call canvas1.setVisible(true), myPanel.setVisible(true) and
jpanel1.setVisible(true) and still my image is not visible.
I set canvas1.setLocation(0,0), myPanel.setLocation(0,0) and still my
image is not visible.
Is there something else that I should try to make the image visible?
Thanks,
> Hi all, I am experimenting with JApplet and JPanel.
>
[quoted text clipped - 17 lines]
>
> Thanks,
I have narrowed it down but I don't fully understand why this is
happening. If I draw an image during the init() method nothing is
displayed, even if I put the setVisibale and setLocation method calls
before the drawImage method. But after the applet is inited, then images
can be drawn. but why? I want to display an image with out user
intervention, and as far as I know, that can only be done during init().
Is there another way?
Thanks,
Joshua Cranmer - 30 Sep 2007 22:45 GMT
> I have narrowed it down but I don't fully understand why this is
> happening. If I draw an image during the init() method nothing is
[quoted text clipped - 3 lines]
> intervention, and as far as I know, that can only be done during init().
> Is there another way?
Typically, drawing is done by paint() and not init(). init() is the code
called when your applet is first invoked.
Perhaps you might want to look at
http://java.sun.com/docs/books/tutorial/uiswing/components/applet.html ?

Signature
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
James Barrett - 01 Oct 2007 04:24 GMT
> Typically, drawing is done by paint() and not init(). init() is the code
> called when your applet is first invoked.
>
> Perhaps you might want to look at
> http://java.sun.com/docs/books/tutorial/uiswing/components/applet.html ?
I overrode the paint method in my JPanel class, and that seemed to only
be called one time. It wasn't getting called if I minimized and then
maximized the applet. So, I then overrode the paintComponent method.
That seemed to be called only when I resize the applet. For example,if I
minimized then maximized the applet, the panel went blank until I
resized, the the Panel was painted.
I'll keep researching, but it seems like there should be way to force a
paint of the entire Panel. I could call paint directly in a timer but I
think that would defeat the purpose.
Thanks,
Jim
James Barrett - 01 Oct 2007 12:27 GMT
>> I have narrowed it down but I don't fully understand why this is
>> happening. If I draw an image during the init() method nothing is
[quoted text clipped - 9 lines]
> Perhaps you might want to look at
> http://java.sun.com/docs/books/tutorial/uiswing/components/applet.html ?
I think I figured it out. I was going about it wrong. Instead of
extending JPanel and adding a Canvas, I now extend canvas and override
paint(). That works. But I noticed that when the applet first loads
paint() doesn't get called or it gets called before the applet is
completely initted. So, I set a timer to call repaint() on my extended
Canvas object. This might still not be what I want but I am much farther
along than I was before.
Thanks!
Jim
>Hi all, I am experimenting with JApplet and JPanel.
Be prepared for a world of trouble, dealing with applets.
Do you really need an applet, or just something that
can be launched from a link? If the latter, look instead
to a JFrame and Java Web Start.
>My JApplet contains a JPanel called jpanel1 size 210x210.
>
>I created a class myPanel which extends JPanel and I set its size to
>210x210.
>
>myPanel contains a Canvas ...
You should not mix Swing and AWT, though I suspect
this is not the source of the propblem here.
>..called canvas1 and its size is 210x210.
Setting the preferredsize of the innermost component
should suffice in most situations, but that all becomes
redundant with applets in the sense that the entire GUI
is restrcted to the size set in the HTML.
>But when I run the applet and draw an image to the canvas1, it is not
>visible.
>
>I call canvas1.setVisible(true), myPanel.setVisible(true) and
>jpanel1.setVisible(true) and still my image is not visible.
All these calls to setVisible() are redundant.
>I set canvas1.setLocation(0,0), myPanel.setLocation(0,0) and still my
>image is not visible.
These calls are redundant so long as the applet
uses layouts, if it does not use layouts, it should.
If you simply aComponent.add(anotherComponent),
there will be a layout present.
>Is there something else that I should try to make the image visible?
MediaTracker.
There was a recent thread re. fading an image in an applet..
<http://www.javakb.com/Uwe/Forum.aspx/java-setup/10820/Fading-effect>
Look for Knute Johnson's code example, that shows
how to do it in a frame.
I turned the OP's version of Knute's code into an applet
that you can see here. The code is linked.
<http://www.physci.org/test/applet/slideshow/>
Failing that, try posting an SSCCE.
<http://www.physci.org/codes/sscce.html>

Signature
Andrew Thompson
http://www.athompson.info/andrew/