hi !
I repost my question cause my last post doesn't appear anymore on the group.
Last time, I put two images for example. Maybe it is why my message does
not appear. So this time, take two images with transparent background or
let me your email adress so I can send you the right images I use.
My problem :
I still have a little problem with my transparent window. I put a piece of
code to understand :
- I put a JLabel in a "transparent" window (don't look at the transparency,
it's not perfect but it's enought to understand the mechanism)
- I put a MouseListener on the JLabel : when mouse enters, the image
changes.
My problem is that the image changes but it is painted with a grey opaque
background (default window background). BUT, but, if you exist and
re-enter, the image is painted correctly, with the "ransparent" background.
I can't manage to solve this little problem.
Here is my code :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TransparentWindow extends JWindow implements
MouseMotionListener, FocusListener {
JPanel contentPane;
ImageIcon img1 = new ImageIcon("package.png"); // put the right path
ImageIcon img2 = new ImageIcon("email.png");
JLabel label1 = new JLabel(img1);
Image img,tim;
Graphics tig;
Point mp;
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
public TransparentWindow() {
// window (475x100), centered and top screen
setBounds((int)(d.getWidth()-475)/2,0,475,100);
// captures the screen to make the "transparent" background
capture();
// add label
contentPane = (JPanel) getContentPane();
contentPane.setLayout(new FlowLayout(FlowLayout.CENTER,15,15));
contentPane.add(label1);
// add the MouseListener that changes the image
label1.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e){
label1.setIcon(img2);
paint(getGraphics());
}
});
// add listeners for transparency
addMouseMotionListener(this);
addFocusListener(this);
setVisible(true);
}
// Listeners for transparency
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();
}
// paint
public void paint(Graphics g)
{
// Creates an off-screen image (double-buffering)
if (tim == null)
{
tim = createImage(getWidth(),getHeight());
tig = tim.getGraphics();
}
// paints the screen capture then the grey semi transparent rectangle
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);
// paints the label with the "transparent" background
Rectangle rect2 = label1.getBounds();
Graphics lg = g.create(rect2.x, rect2.y, rect2.width, rect2.height);
label1.paint(lg);
}
public void update(Graphics g)
{
this.paint(g);
}
// Main
public static void main (String[] args)
{
new TransparentWindow();
}
}
Thanks for your help
--
Arnaud
Andrew Thompson - 22 Jan 2004 17:50 GMT
| hi !
Hi! Try..
| - I put a MouseListener on the JLabel : when mouse enters, the image
| changes.
|
| My problem is that the image changes but it is painted with a grey opaque
| background (default window background). BUT, but, if you exist and
| re-enter, the image is painted correctly, with the "ransparent" background.
.....
| // add the MouseListener that changes the image
| label1.addMouseListener(new MouseAdapter() {
| public void mouseEntered(MouseEvent e){
| label1.setIcon(img2);
| paint(getGraphics());
setVisible(false);
setVisible(true);
| }
| });
A bit kludgy, but it seems to solve the
problem this end. Unfortunately the
window flashes gray.
--
Andrew Thompson
* http://www.PhySci.org/ PhySci software suite
* http://www.1point1C.org/ 1.1C - Superluminal!
* http://www.AThompson.info/andrew/ personal site
HTH
Arnaud - 24 Jan 2004 10:23 GMT
> setVisible(false);
> setVisible(true);
Really, hugly and this didn't solve the problem for me (but is it the
platform ? I have a Linux distro, Mandrake 9.1).
Anyway, my solution is Ok for me cause I just need to create a new type of
JLabel and that's all. I don't need menuBars, etc ...
This app was just an exercise at the beguinning, to learn how to handle
paint and so on. Then I thought it would be funny to transform this app in
a main menubar for a professionnal app I am developping. This is just a
small app for my enterprise, which main job will be to search in a database
and present the results (there are a few other caracteristics too). We use
a professionnal software that cannot make some operations and so I have to
develop an external app. That's all !
I will send you the finall result if you want.
--
Arnaud
Andrew Thompson - 24 Jan 2004 11:34 GMT
"Arnaud" ...
| > setVisible(false);
| > setVisible(true);
|
| Really, hugly and this didn't solve the problem for me
It was rather kludgy on MS too..
Glad to see you solved it by overriding setIcon()
| I will send you the finall result if you want.
That would be great, I am eager to
see 'through' your final result.
Talk about transparent code. ;-)
My address is physci at physci dot org
--
Andrew Thompson
* http://www.PhySci.org/ PhySci software suite
* http://www.1point1C.org/ 1.1C - Superluminal!
* http://www.AThompson.info/andrew/ personal site
Arnaud - 22 Jan 2004 20:02 GMT
OK, problem solved : that came from the setIcon() method. I had to override
it to repaint the background too.
--
Arnaud
Andrew Thompson - 22 Jan 2004 20:33 GMT
| OK, problem solved : that came from the setIcon() method. I had to override
| it to repaint the background too.
:-) Thought you'd find a better solution..
So you're finding you need to adjust
each type of component Arnaud,
or is it just particular occasional
-gotchas'- like that?
Out of curiosity, what is all this
'see through' app aimed at?
Music player, file finder...??
[ I assume you do have a specific,
particular app in mind.. ]
--
Andrew Thompson
* http://www.PhySci.org/ PhySci software suite
* http://www.1point1C.org/ 1.1C - Superluminal!
* http://www.AThompson.info/andrew/ personal site