I am probably just misunderstanding something but I cannot get this to work.
Basically I am trying to display an animated graphic of a clock while my
program completes initialization. That is I try to make the application
window visible as soon as possible so the user knows that the program has
actually started, but it cannot do any real work for the user until it has
built all of the internal tables (mostly by reading an XML file).
I do not want to put the graphic into a JInternalFrame because I do not want
the title and frame that a JInternalFrame puts around its contents, so I am
trying to just put the Image into a JLabel and adding that to the
JDesktopPane. Although the description of JDesktopPane only discusses the
behavior of JInternalFrames, JDesktopPane does extend JLayeredPane which
clearly does support the addition of JLabels, as demonstrated by the example
on the java.sun.com web site. Also the only post I could find in
groups.google.com related to this also indicated that the poster had
succeeded in adding a JLabel to a JDesktopPane.
In the following code I have two problems:
1) the JLabel does not appear.
2) I cannot get the graphics context of the JLabel in order to draw the
initial position of the moving portion of the image. I do get it when the
timer pops, although of course since I cannot see the JLabel I do not
actually see the animation. I want to get the graphics context before I
display the label, but this does not appear to be permitted.
public class BusyDialog extends JLabel
implements ActionListener
{
...
public BusyDialog(JDesktopPane desk) {
this.desk = desk; // save the desktop
...
clockHeight = clockImage.getIconHeight();
clockWidth = clockImage.getIconWidth();
pendulumHeight = pendulumLeftImage.getIconHeight();
pendulumWidth = pendulumLeftImage.getIconWidth();
pendulumX = (clockWidth - pendulumWidth)>>1;
pendulumY = (clockHeight - pendulumHeight) - 5;
setIcon(clockImage); // image
// center label in desktop
Rectangle bounds = getBounds();
Dimension deskSize= desk.getSize();
if (bounds.width < deskSize.width)
bounds.x = (deskSize.width - bounds.width)/2;
else
bounds.x = 0;
if (bounds.height < deskSize.height)
bounds.y = (deskSize.height - bounds.height)/2;
else
bounds.y = 0;
setBounds(bounds);
// get the graphics context of the label, note that this does NOT work
graphics = getGraphics();
// draw the pendulum at its initial position
if (graphics != null)
{
pendulumLeftImage.paintIcon(this,
graphics,
pendulumX,
pendulumY);
}
else
{
System.err.println("graphics == null");
}
pendulumPos = 1; // draw pendulum centre next
// add the label to the desktop
desk.add(this, 1); // add to desktop
setVisible(true); // display the label
// start a timer to wake up every second
timer = new Timer(1000, this);
timer.start();
} // BusyDialog.BusyDialog

Signature
Jim Cobban jcobban@magma.ca
34 Palomino Dr.
Kanata, ON, CANADA
K2M 1M1
+1-613-592-9438
ak - 27 Apr 2004 21:58 GMT
> 1) the JLabel does not appear.
JDesktopPane doesn't have LayoutManager so you have to set size or bounds of
your panel.
--
____________
http://reader.imagero.com the best java image reader.
Jim Cobban - 28 Apr 2004 02:03 GMT
> > 1) the JLabel does not appear.
>
> JDesktopPane doesn't have LayoutManager so you have to set size or bounds of
> your panel.
I certainly see a setBounds function call in the code I posted, after the
chunk of code that calculates the x and y coordinates required to center the
label in the desktop. I use the exact same code when I position
JInternalFrames in the desktop. If the code works for JInternalFrame I do
not see why it would not work on a JLabel.
Jim Cobban - 30 Apr 2004 02:08 GMT
Please, I know I must be doing something stupid, but I just cannot see what
it is. I just want someone to take a look at the code I posted and let me
know what my mistake is.
Andrew Thompson - 30 Apr 2004 04:18 GMT
> Please, I know I must be doing something stupid, but I just cannot see what
> it is. I just want someone to take a look at the code I posted and let me
> know what my mistake is.
It is much harder to determine the problem
with non-compileable code than it is for
compileable code. (Speaking for myself,
Java looks like Martian until I see it
with all the pretty colors in my IDE)
Maybe you need to go the extra distance
of preparing a self contained example
<http://www.physci.org/codes/sscce.jsp>

Signature
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology