Sagewire Logo

JPanel not visible in JApplet

6 Message(s) by 3 Author(s) originally posted in java programming


From: James Barrett Date:   Sunday, September 30, 2007
-----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 extend s 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 isn't
visible.

I call canvas1.setVisible(true), myPanel.setVisible(true) and
jpanel1.setVisible(true) and still my image isn't visible.

I set canvas1.setLocation(0,0), myPanel.setLocation(0,0) and still my
image isn't 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-----


From: James Barrett Date:   Sunday, September 30, 2007
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

wrote in message:
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 isn't
visible.
I call canvas1.setVisible(true), myPanel.setVisible(true) and
jpanel1.setVisible(true) and still my image isn't visible.
I set canvas1.setLocation(0,0), myPanel.setLocation(0,0) and still my
image isn't visible.
Is there something else that I should try to make the image visible?
Thanks,



I have narrowed it down but I do not fully understand why this is
happening. If I draw an image during the init() method nothing is
display ed, 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-----


From: Joshua Cranmer Date:   Sunday, September 30, 2007
wrote in message:
I have narrowed it down but I do not 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/tutorial/uiswing/component s/applet.html ?

--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth


From: Andrew Thompson Date:   Sunday, September 30, 2007
wrote in message:
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 shouldn't mix Swing and AWT, though I suspect
this isn't 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 isn't
visible.
I call canvas1.setVisible(true), myPanel.setVisible(true) and
jpanel1.setVisible(true) and still my image isn't visible.



All these calls to setVisible() are redundant.

I set canvas1.setLocation(0,0), myPanel.setLocation(0,0) and still my
image isn't visible.



These calls are redundant so long as the applet
uses layouts, if it doesn't 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.aspx/JAVA-general/200710/1


From: James Barrett Date:   Sunday, September 30, 2007
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

wrote in message:
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 was not 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 will 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'd defeat the purpose.

Thanks,

Jim

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

iD8DBQFHAGiDQuDJiZ/QrH0RAshHAKCfM/ZdtfVJZokrukJkbs4ePj0CrQCgradA
S6f/mMqbJEpzWNSi7ZFfvCo=
=Mf1R
-----END PGP SIGNATURE-----


From: James Barrett Date:   Monday, October 01, 2007
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

wrote in message:
wrote in message:
I have narrowed it down but I do not 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/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() does not 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-----



Next Message: Runtime process


Blogs related to JPanel not visible in JApplet

Swing-based tree layouts with CheckboxTree
Java Tip 114: Add ghosted draggable images to your JTrees” (Andrew Armstrong, JavaWorld, August 2001) builds on Java Tip 97 with instructions for replacing checkboxes or radio buttons with images (such as folders), then using a fade ...

if an object of class A needs to have a reference to an object of ...
he's right java ClassName you're executing _CLASSES_ not files. I think sun's forums have an applet section if it's AWT/Swing related (and not a security issue), you can still ask it here. Dumb quesion…but does anybody know or had any ...


Programming | Sports | Autos

copyright 2006
Valid XHTML 1.0 Transitional