• Post Reply Bookmark Topic Watch Topic
  • New Topic

Transparent frame using Robot class and its createScreenCapture() method - help pls!

 
Shashi Kanta
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys,

i am making a frame/window transparent, so that my application will look like a desktop application, since i will be able to have my own design instead of the regular rectangula shapes.
i have used java.awt.Robot class for this purpose.
i have managed to some extend to get what i want. ie if the frame sits idle at one location only, i am able to see the desktop screen right through the frame, though its components (in my case a single Button, for simplicity) reamins visible. thats perfectly fine!
the problem starts once i decide to drag the frame around the desktop!!! i keep on seeing the initial screen portion captured when the frame first became visible!!!
i have tried all possibilities of passing the Graphics context of the frame from the componentMoved() method, to just calling repaint() method from inside the componentMoved() method.
i can't understand this!
can anyone help me on this, on how to write a code inside the componentMoved() method or the paint() method ?
i would be really appreciated if you could!
i'm reproducing the code for your inspection and trial run.
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;

public class transFrame extends Frame{
Image img;
Rectangle rect;
Point p;
Robot r;
MediaTracker mt;
public transFrame(){
addWindowListener(new myWindowAdapter(this));
addComponentListener(new myComponentAdapter(this));
}
public void update(Graphics g){
paint(g);
}
public void paint(Graphics g){
try{
r=new Robot();
}
catch(AWTException awe){
System.out.println("Robot exception occurred");
}
catch(SecurityException se){
System.out.println("Robot object could not be instantiated");
}

p=this.getLocationOnScreen();
System.out.println("frame window at location : "+p.x+", "+p.y);
Dimension d=this.getSize();
img=r.createScreenCapture(new Rectangle(p.x,p.y,d.width,d.height));
mt=new MediaTracker(this);
mt.addImage(img,1);
try{
mt.waitForAll();
}
catch(InterruptedException ie){
System.out.println("Problem in loading the transparent image");
}

g.drawImage(img,0,0,this);
img.flush();

} // end of paint()
public static void main(String args[]){

transFrame transparent=new transFrame();
Button b1=new Button("Click");
transparent.setSize(400,300);
transparent.setLayout(new FlowLayout());
transparent.add(b1);
transparent.setVisible(true);
Graphics g=transparent.getGraphics();
transparent.paint(g);

}

} // end of transFrame

class myWindowAdapter extends WindowAdapter{
transFrame frame;
public myWindowAdapter(transFrame frame){
this.frame=frame;
}
public void windowClosing(WindowEvent we){
frame.setVisible(false);
frame.dispose();
System.exit(0);
}
} // end of myWindowAdapter
class myComponentAdapter extends ComponentAdapter{
transFrame frame;
Image img;
Robot rbt;
public myComponentAdapter(transFrame frame){
this.frame=frame;
}
public void componentMoved(ComponentEvent ce){
//Graphics g=frame.getGraphics();
//frame.paint(g);
frame.repaint();
}

} // end of myComponentAdapter
// end of program
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've tried the same thing before... Unfortunately, the problem is that when you do a screen capture of the location of the frame you get a shot of the frame itself. If you try to toggle setVisible() it doesn't work because the frame stops moving when it goes invisible. So, unfortunately, you can't do what you are trying to do...

This technique can make floating animations on the desktop though... provided the area they cover is small enough so that it doesn't flicker... and you use a Window instead of a Frame so the title bar doesn't show up. And instead of captureing the current area, you capture the area you are moving into.

-Nate
 
Shashi Kanta
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nate,
its ok if u cudn't help, bcoz the concept is a new one and not enough resources have been provided by java team of Sun Microsytems.
and about using window instead of a frame, well thats what have to do ultimately in the end once it works with the frame. i used frame so that i can close the frame window. its just a trial. once its a success, i can use windows and right some mouse events to close the window.
now, what i don't understand, is how do i know in what area the frame is moving into?!! that can be decided only after the frame has been moved.
if u can show a snippet of a code on that, it'll be great!
rgds,
Shash
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at JavaRanch's naming policy. . We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please log in with a new name which meets the requirements.
Thanks.
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is one of the simple animations I was doing... just a ball that bounces around the screen...



-Nate

P.S. - Sorry about not warning you to change your name in my first post... I was distracted answering your actual post... you still need to change it so it is in the format "First Name" + space + "Last Name". You can change it easily here.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!