Jump to content

Need Help With Tutorial

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
gszauer

gszauer

    Programmer

  • Members
  • PipPipPipPip
  • 113 posts
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

~Aristotle said:

It is the mark of an educated mind to entertain a tought without accepting it
If my post was helpful, please help me build some rep Posted Image

#2
amischiefr

amischiefr

    Newbie

  • Members
  • Pip
  • 9 posts
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.

#3
gszauer

gszauer

    Programmer

  • Members
  • PipPipPipPip
  • 113 posts
lol, that is the same as the first codeblock i posted.
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
If my post was helpful, please help me build some rep Posted Image