Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Problem with JLabels in a special window ...

21 views
Skip to first unread message

Arnaud

unread,
Jan 14, 2004, 3:10:22 PM1/14/04
to
hi !

I've created a special window (transparent), but I've got problem : when I
add JLabels to it, they don't appear in the window !

Here's my code (yhe code for the transparent window isn't optimised yet,
that's just a short version, not too long, to understand the basic
mechanism) :

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class TransparentWindow extends JWindow implements
MouseMotionListener, FocusListener {

JPanel contentPane;
JLabel label1 = new JLabel("Hello !");

Image img,tim;
Graphics tig;
Point mp;
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();

public TransparentWindow() {

setBounds((int)(d.getWidth()-475)/2,0,475,100);
capture();

contentPane = (JPanel) getContentPane();
contentPane.setLayout(new FlowLayout(10,10,FlowLayout.CENTER));
contentPane.add(label1);

addMouseMotionListener(this);
addFocusListener(this);

setVisible(true);
}


public void focusGained(FocusEvent fe)
{
Point p = getLocation();
setLocation(11111,0);
capture();
setLocation(p);
}

public void focusLost(FocusEvent fe)
{
}

public void capture()
{
try {
Robot r = new Robot();
Rectangle rect = new Rectangle(0,0,d.width,d.height);
img = r.createScreenCapture(rect);
}
catch(AWTException awe) {
System.out.println("robot excepton occurred");
}
}

public void mouseDragged(MouseEvent m)
{
Point p = m.getPoint();
int x = getX()+p.x-mp.x;
int y = getY()+p.y-mp.y;
setLocation(x,y);
Graphics g = getGraphics();
paint(g);
}

public void mouseMoved(MouseEvent m)
{
mp = m.getPoint();
}

public void paint(Graphics g)
{
if (tim == null)
{
tim = createImage(getWidth(),getHeight());
tig = tim.getGraphics();
}
tig.drawImage(img,0,0,getWidth(),getHeight(),
getX(),getY(),getX()+getWidth(),getY()+getHeight(),null);
tig.setColor(new Color(0,0,0,128));
tig.fillRoundRect(0,0,getWidth(),getHeight(),100,120);
g.drawImage(tim,0,0,null);
}

public void cupdate(Graphics g)
{
this.paint(g);
}

public static void main (String[] args)
{
new TransparentWindow();
}

}

hiwa

unread,
Jan 14, 2004, 7:36:35 PM1/14/04
to
Arnaud <p...@demail.svp> wrote in message news:<4005a010$0$6966$7a62...@news.club-internet.fr>...

JLabel default is transparent. You have to call setOpaque(true).

Arnaud

unread,
Jan 15, 2004, 5:04:38 AM1/15/04
to
hiwa wrote:

> JLabel default is transparent. You have to call setOpaque(true).

Sorry, but I forgot to mention that I had already tested this solution, that
does not work. I don't know why.
No, my problem is a bit more complex. I think it's in the paint method that
I've forgotten something, that's why I've posted the code in the message,
so anyone can test the solutions proposed to see immediatly if it works or
not.
Thx anyway

--
Arnaud

ak

unread,
Jan 15, 2004, 8:25:00 AM1/15/04
to
> Sorry, but I forgot to mention that I had already tested this solution,
that
> does not work. I don't know why.
> No, my problem is a bit more complex. I think it's in the paint method
that
> I've forgotten something

Yes, don't override paint(), but paintComponent();

____________

http://reader.imagero.com the best java image reader.


Andrew Thompson

unread,
Jan 15, 2004, 9:00:36 AM1/15/04
to
"Arnaud" <p...@demail.svp> wrote in message
news:40066396$0$6966$7a62...@news.club-internet.fr...

| hiwa wrote:
|
| > JLabel default is transparent. You have to call
setOpaque(true).
|
| Sorry, but I forgot to mention that I had already tested this
solution, that
| does not work. I don't know why.

I did not mention in my original response,
but recently I was helping someone with
an animation. You can see the code here.
http://www.physci.org/launcher.jsp#JAnimateFrame

It is not quite like your situation, but I
think it provides a clue. What I suspect
is that you need to specifically draw the
components, which would otherwise be
done automatically by 'paint'..

[ And no, I cannot quite figure how to do that! ]

--
Andrew Thompson
* http://www.PhySci.org/ PhySci software suite
* http://www.1point1C.org/ 1.1C - Superluminal!
* http://www.AThompson.info/andrew/ personal site


Arnaud

unread,
Jan 15, 2004, 12:36:56 PM1/15/04
to
I'll have a look at paintComponent();

I'll post a message later if this work (I'll test it as soon as possible,
but not before next week I think)

Thx to everybody for the responses.

--
Arnaud

Arnaud

unread,
Jan 16, 2004, 8:23:17 AM1/16/04
to
I found the solution :

just add label1.paint(g); at the end of the paint() method

quite simple isn't it ?

--
Arnaud

Arnaud

unread,
Jan 16, 2004, 8:59:35 AM1/16/04
to
Arnaud wrote:

hum, doesn't work so perfectly ... the JLabel isn't painted where it should
be according to the Layout specified : this means, when you have two
labels, they're painted at the same place.

I'll work on it :( (maybe JLayeredPane could help me)

--
Arnaud

ak

unread,
Jan 16, 2004, 9:11:47 AM1/16/04
to
> hum, doesn't work so perfectly ... the JLabel isn't painted where it
should
> be according to the Layout specified : this means, when you have two
> labels, they're painted at the same place.
>
> I'll work on it :( (maybe JLayeredPane could help me)

public void paint(Graphics g) {
....
JLabel label = ...;
Rectangle r = label.getBounds();
Graphics lg = g.create(g.x, r.y, r.width(), r.height);
label.paint(lg);

}

this should work.

PS. can you tell me why you override paint() instead of paintComponent() ?

Andrew Thompson

unread,
Jan 16, 2004, 11:00:59 AM1/16/04
to
"Arnaud" <p...@demail.svp> wrote in message
news:4007ec25$0$6970$7a62...@news.club-internet.fr...

| Arnaud wrote:
|
| > I found the solution :
| > just add label1.paint(g); at the end of the paint() method
| > quite simple isn't it ?
..

| hum, doesn't work so perfectly ... the JLabel isn't painted
where it should
| be according to the Layout specified : this means, when you
have two
| labels, they're painted at the same place.

I am replying to this message 'cos ak
snipped the attributions in their answer,
but basically I used ak's fix successfully
(after correcting a few minor typo's)

It is looking pretty good, but there is a
slight hitch that will probably fix itself
once you 'jar' the code. I put a picture
here.. (the .png..)
http://www.physci.org/test/10transparent/

Because it considers the DOS window to
be active, it appears 'in front of' the editor
window when the transparent window is
on top of it ..look at the .png.

I am gonna play with it a bit more, If I
can confirm it is not a probelm with a
jar file, would you mind if I display it on
my site? It is one of those things that is
asked on occasions, and it would be
nice to be able to supply an URL.

Arnaud

unread,
Jan 17, 2004, 3:57:19 AM1/17/04
to

> public void paint(Graphics g) {
> ....
> JLabel label = ...;
> Rectangle r = label.getBounds();
> Graphics lg = g.create(g.x, r.y, r.width(), r.height);
> label.paint(lg);
>
> }
>
> this should work.

this works perfectly, thanks. (working on computer all day long and learning
400 pages for an exam at the same time make your mind freeze a bit ;))

>
> PS. can you tell me why you override paint() instead of paintComponent() ?

I don't know. I don't really understand the difference between the both of
them. So as long as paint() is OK for me, I stay on it. But if you can
explain me the difference and the advantage of paintComponent(), I'll
probably code paintComponent instead of paint() (I am not an expert in
graphics, it was quite difficult for me to understand the mechanism of
getGraphics(), paint(), update() and so on).

Thanks again for your help

--
Arnaud

Arnaud

unread,
Jan 17, 2004, 6:17:57 AM1/17/04
to
new problem :)

take the last code and replace JLabel("Hello !") with a gif image that as a
transparent background. Everything works OK.

Then I add a MouseAdapter to the label that replaces the image by a new one
when the mouse enter the JLabel. Problem : the image changes, but it is
painted with an opaque gray background.

Why ?

--
Arnaud

Andrew Thompson

unread,
Jan 17, 2004, 6:43:23 AM1/17/04
to
"Arnaud" <p...@demail.svp> wrote in message
news:400917c1$0$6968$7a62...@news.club-internet.fr...

Maybe you need to setOpaque(false) again(?)

Failing that, example code?

[ And ..is it OK with you if I display a modified
form of your code on my site? ]

Arnaud

unread,
Jan 17, 2004, 10:48:27 AM1/17/04
to
> Maybe you need to setOpaque(false) again(?)

I'll see if that can help. I found that adding, as answered earlier by ak in
this post :

Rectangle r = label.getBounds();
Graphics lg = g.create(g.x, r.y, r.width(), r.height);
label.paint(lg);

is near to work (if you enter, the image changes and you get so an image
with opaque background ; then exit and reenter the JLabel, here it is,
you've got the right background). I've got to understand a little bit more
the mechanisms of paint and graphics to optimise this part of code.



> Failing that, example code?
>
> [ And ..is it OK with you if I display a modified
> form of your code on my site? ]

you can display what you want on your site, it's OK for me (the entire code
is OpenSource), but you should mention that this code is in part taken from
a post on sun forum :
http://forum.java.sun.com/thread.jsp?thread=391403&forum=4&message=2187306

In the last post, you will find a good version of transparent window, which
I use now for transparency. It is near to be perfect, just a problem when
you iconify an application behind your window : the background is not
refreshed.

bye

--
Arnaud

Andrew Thompson

unread,
Jan 17, 2004, 10:54:14 AM1/17/04
to
"Arnaud" <p...@demail.svp> wrote in message
news:40095726$0$6976$7a62...@news.club-internet.fr...
..

| you can display what you want on your site, it's OK for me

thanks, accreditation noted

| In the last post, you will find a good version of transparent
window, which
| I use now for transparency. It is near to be perfect, just a
problem when
| you iconify an application behind your window : the background
is not
| refreshed.

..hmmm. I am on uncertain ground here,
but perhaps a focusLost() event may help?

Arnaud

unread,
Jan 18, 2004, 5:10:23 AM1/18/04
to

> ..hmmm. I am on uncertain ground here,
> but perhaps a focusLost() event may help?

No, because ypur window never lost focus, that's the problem.

--
Arnaud

Andrew Thompson

unread,
Jan 18, 2004, 5:48:10 AM1/18/04
to
"Arnaud" <p...@demail.svp> wrote in message
news:400a5969$0$6972$7a62...@news.club-internet.fr...

|
| > ..hmmm. I am on uncertain ground here,
| > but perhaps a focusLost() event may help?
|
| No, because ypur window never lost focus, that's the problem.

You could always start a thread to refresh
it every half second or so..


0 new messages