Java Forum / GUI / January 2004
Problem with JLabels in a special window ...
Arnaud - 14 Jan 2004 20:10 GMT 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 - 15 Jan 2004 00:36 GMT JLabel default is transparent. You have to call setOpaque(true).
Arnaud - 15 Jan 2004 10:04 GMT > 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 - 15 Jan 2004 13:25 GMT > 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.
Arnaud - 15 Jan 2004 17:36 GMT 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
Andrew Thompson - 15 Jan 2004 14:00 GMT | > 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 - 16 Jan 2004 13:23 GMT I found the solution :
just add label1.paint(g); at the end of the paint() method
quite simple isn't it ?
-- Arnaud
Arnaud - 16 Jan 2004 13:59 GMT > I found the solution : > [quoted text clipped - 4 lines] > -- > Arnaud 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 - 16 Jan 2004 14:11 GMT > 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() ?
____________
http://reader.imagero.com the best java image reader.
Arnaud - 17 Jan 2004 08:57 GMT > public void paint(Graphics g) { > .... [quoted text clipped - 6 lines] > > 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 - 17 Jan 2004 11:17 GMT 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 - 17 Jan 2004 11:43 GMT | new problem :) | [quoted text clipped - 4 lines] | when the mouse enter the JLabel. Problem : the image changes, but it is | painted with an opaque gray background. 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? ]
-- Andrew Thompson * http://www.PhySci.org/ PhySci software suite * http://www.1point1C.org/ 1.1C - Superluminal! * http://www.AThompson.info/andrew/ personal site
Arnaud - 17 Jan 2004 15:48 GMT > 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 - 17 Jan 2004 15:54 GMT ..
| 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?
-- Andrew Thompson * http://www.PhySci.org/ PhySci software suite * http://www.1point1C.org/ 1.1C - Superluminal! * http://www.AThompson.info/andrew/ personal site
Arnaud - 18 Jan 2004 10:10 GMT > ..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 - 18 Jan 2004 10:48 GMT | > ..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..
Andrew Thompson - 16 Jan 2004 16:00 GMT | > 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.
-- Andrew Thompson * http://www.PhySci.org/ PhySci software suite * http://www.1point1C.org/ 1.1C - Superluminal! * http://www.AThompson.info/andrew/ personal site
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|