Google 網路論壇不再支援新的 Usenet 貼文或訂閱項目,但過往內容仍可供查看。

Problem with JLabels in a special window ...

瀏覽次數:21 次
跳到第一則未讀訊息

Arnaud

未讀,
2004年1月14日 下午3:10:222004/1/14
收件者:
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

未讀,
2004年1月14日 晚上7:36:352004/1/14
收件者:
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

未讀,
2004年1月15日 清晨5:04:382004/1/15
收件者:
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

未讀,
2004年1月15日 上午8:25:002004/1/15
收件者:
> 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

未讀,
2004年1月15日 上午9:00:362004/1/15
收件者:
"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

未讀,
2004年1月15日 中午12:36:562004/1/15
收件者:
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

未讀,
2004年1月16日 上午8:23:172004/1/16
收件者:
I found the solution :

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

quite simple isn't it ?

--
Arnaud

Arnaud

未讀,
2004年1月16日 上午8:59:352004/1/16
收件者:
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

未讀,
2004年1月16日 上午9:11:472004/1/16
收件者:
> 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

未讀,
2004年1月16日 上午11:00:592004/1/16
收件者:
"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

未讀,
2004年1月17日 凌晨3:57:192004/1/17
收件者:

> 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

未讀,
2004年1月17日 清晨6:17:572004/1/17
收件者:
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

未讀,
2004年1月17日 清晨6:43:232004/1/17
收件者:
"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

未讀,
2004年1月17日 上午10:48:272004/1/17
收件者:
> 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

未讀,
2004年1月17日 上午10:54:142004/1/17
收件者:
"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

未讀,
2004年1月18日 清晨5:10:232004/1/18
收件者:

> ..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

未讀,
2004年1月18日 清晨5:48:102004/1/18
收件者:
"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 則新訊息