Hi all, i need a bit of help applying a tutorial.
ONJava.com -- Hacking Swing: Translucent Windows
I'll start with the basic code, this is what the tut has:
[HIGHLIGHT="Java5"]public class TransparentBackground extends JComponent {
private JFrame frame;
private Image background;
public TransparentBackground(JFrame frame) {
this.frame = frame;
updateBackground( );
}
public void updateBackground( ) {
try {
Robot rbt = new Robot( );
Toolkit tk = Toolkit.getDefaultToolkit( );
Dimension dim = tk.getScreenSize( );
background = rbt.createScreenCapture(
new Rectangle(0,0,(int)dim.getWidth( ),
(int)dim.getHeight( )));
} catch (Exception ex) {
p(ex.toString( ));
ex.printStackTrace( );
}
}
public void paintComponent(Graphics g) {
Point pos = this.getLocationOnScreen( );
Point offset = new Point(-pos.x,-pos.y);
g.drawImage(background,offset.x,offset.y,null);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Transparent Window");
TransparentBackground bg = new TransparentBackground(frame);
bg.setLayout(new BorderLayout( ));
JButton button = new JButton("This is a button");
bg.add("North",button);
JLabel label = new JLabel("This is a label");
bg.add("South",label);
frame.getContentPane( ).add("Center",bg);
frame.pack( );
frame.setSize(150,100);
frame.show( );
}
}[/HIGHLIGHT]
(There are like 2 syntax errors)
Im trying to make it into 1 object, so when you try and make a window, its transparent.
[HIGHLIGHT="Java5"]import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.awt.*;
public class WindowTestApp {
public static void main(String[] args){
WindowTest wt = new WindowTest("Sample Window", 200, 200, 0, 100);
wt.makeVisible();
}
}
class WindowTest extends JFrame implements ActionListener{
// Variables
private Image background;
// Constructors
public AeroWindow (){
super();
this.setUndecorated(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.updateBackground();
}
public AeroWindow (String title, int w, int h, int x, int y) {
this();
this.setTitle(title);
this.setSize(w, h);
this.setLocation(x, y);
}
// Methods
public void makeVisible() {
this.show();
}
private void updateBackground( ) {
try {
Robot rbt = new Robot( );
Toolkit tk = Toolkit.getDefaultToolkit( );
Dimension dim = tk.getScreenSize( );
background = rbt.createScreenCapture(new Rectangle(0,0,(int)dim.getWidth( ),(int)dim.getHeight( )));
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Exception Occured:\n"+e.toString()+"\nProgram will now terminate", "ERROR!", -1);
System.exit(0);
}
}
public void paintComponent(Graphics g) {
Point pos = this.getLocationOnScreen( );
Point offset = new Point(-pos.x,-pos.y);
g.drawImage(background,offset.x,offset.y,null);
}
// Interface Methods
public void actionPerformed (ActionEvent ae) {}
}[/HIGHLIGHT]
Mine is not transparent :(
Any ideas?
Thanks
~Gabor
Need Help With Tutorial
Started by gszauer, Feb 21 2008 02:18 PM
2 replies to this topic
#1
Posted 21 February 2008 - 02:18 PM
~Aristotle said:
It is the mark of an educated mind to entertain a tought without accepting it
|
|
|
#2
Posted 22 February 2008 - 05:22 AM
Check out this link:
Java Crazy: Create A Transparent Window in Java
It looks like the way that you need to do it is sort of a hack without using the JNI package.
Java Crazy: Create A Transparent Window in Java
It looks like the way that you need to do it is sort of a hack without using the JNI package.
#3
Posted 22 February 2008 - 10:06 AM
lol, that is the same as the first codeblock i posted.
Its actually the same tutorial!
Thanks, good try.
Its actually the same tutorial!
Thanks, good try.
~Aristotle said:
It is the mark of an educated mind to entertain a tought without accepting it


Sign In
Create Account


Back to top









