469,271 Members | 1,484 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

JPanel not visible in JApplet

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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,


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFHAAW8QuDJiZ/QrH0RAnEhAJ41cR35MvFbu43/MkyJr5Wj+kbptACgzc0x
AK0VStBx5AtaRnJzLQ0LPW4=
=dx6/
-----END PGP SIGNATURE-----
Sep 30 '07 #1
5 9777
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

James Barrett wrote:
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,
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,

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFHABd8QuDJiZ/QrH0RAlvRAKCBqzN2qYUzR7YtgQQvSETBxWZEigCdEozA
7iNuJd1CkoRg2qBCnNNYOLY=
=mCN0
-----END PGP SIGNATURE-----
Sep 30 '07 #2
James Barrett wrote:
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?
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/tutor...ts/applet.html ?

--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
Sep 30 '07 #3
James Barrett wrote:
>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>

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

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.asp...neral/200710/1

Oct 1 '07 #4
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Joshua Cranmer wrote:
>
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/tutor...ts/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

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFHAGiDQuDJiZ/QrH0RAshHAKCfM/ZdtfVJZokrukJkbs4ePj0CrQCgradA
S6f/mMqbJEpzWNSi7ZFfvCo=
=Mf1R
-----END PGP SIGNATURE-----
Oct 1 '07 #5
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Joshua Cranmer wrote:
James Barrett wrote:
>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?

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/tutor...ts/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

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFHANmyQuDJiZ/QrH0RAhCYAKDCStEHPf79o37jGLTrmXow4GdV3wCdFJia
Dr4Io3QavZ5LBsG2QZuPwxQ=
=/EaQ
-----END PGP SIGNATURE-----
Oct 1 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by David Eynon | last post: by
6 posts views Thread by Hal Vaughan | last post: by
reply views Thread by Kookymon1 | last post: by
1 post views Thread by dishal | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.