Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (578)
games submitted by our members
Games in WIP (498)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Transparency issue again  (Read 1118 times)
0 Members and 1 Guest are viewing this topic.
Offline Coinerson

Junior Member




Introducing the world's cutest zombie, Timmy


« Posted 2007-03-12 03:35:22 »

Ive seen the numerous transparency threads come and go without really giveing any good information. Before I go on here is what I have so far:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;

public class TransparentBackground extends JComponent
        implements ComponentListener, WindowFocusListener,
        Runnable {
    private String OS;
    public boolean easy;
    private JFrame frame;
    private Image background;
    private long lastupdate = 0;
    public boolean refreshRequested = true;
    public TransparentBackground(JFrame frame) {
        this.frame = frame;
        OS = System.getProperty("os.name");
       
        if (OS.equals("Mac OS X")) {
            easy = true;
        }
        else {
            easy = false;
        }
        if (!easy) {
            updateBackground();
        }
        frame.addComponentListener(this);
        frame.addWindowFocusListener(this);
        if (!easy) {
            new Thread(this).start();
        }
    }
    public void componentShown(ComponentEvent evt) { repaint(); }
    public void componentResized(ComponentEvent evt) { repaint(); }
    public void componentMoved(ComponentEvent evt) { repaint(); }
    public void componentHidden(ComponentEvent evt) { }

    public void windowGainedFocus(WindowEvent evt) { refresh(); }    
    public void windowLostFocus(WindowEvent evt) { refresh(); }
   
   
    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) {
            ex.printStackTrace();
        }
    }
    public void paintComponent(Graphics g) {
        Point pos = this.getLocationOnScreen();
        Point offset = new Point(-pos.x,-pos.y);
        if (!easy) {
            g.drawImage(background,offset.x,offset.y,null);
        }
       
        g.setColor(Color.BLUE);
        g.drawRect(10, 10, 10, 10);
    }
   
   
    public void refresh() {
        if(frame.isVisible()) {
            repaint();
            refreshRequested = true;
            lastupdate = new Date().getTime();
        }
    }  
    public void run() {
        try {
            while(true) {
                Thread.sleep(250);
                long now = new Date().getTime();
                if(refreshRequested &&
                    ((now - lastupdate) > 1000)) {
                    if(frame.isVisible()) {
                        Point location = frame.getLocation();
                        frame.hide();
                        updateBackground();
                        frame.show();
                        frame.setLocation(location);
                        refresh();
                    }
                lastupdate = now;
                refreshRequested = false;
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
   
   
   
    public static void main(String[] args) {
        JFrame frame = new JFrame("Transparent Window");
        frame.setUndecorated(true);
   
        TransparentBackground bg = new TransparentBackground(frame);
        bg.updateBackground();
        bg.setLayout(new BorderLayout());


        frame.getContentPane().add("Center", bg);
        frame.pack();
        if (bg.easy) {
            frame.setBackground(new Color(0, 0, 0, 0));
        }
        frame.setSize(200, 200);
        frame.setLocation(500, 500);
        frame.show();
    }
}


The problem is the detecting when it needs to take a new screenshot, and the horrible delay between hide/show of the window. I was thinking for detecting the time to take a new screenshot. I could get the absolute position of the mouse and check it every once in a while (10-20ish ms maybe), and if its moved a significant ammoung (some number of pixels) it updates the window. THe current way I do it is from an old book hacking swing or something like that, its pretty slow and terrible and deprecated.

And any way to grease it up so its not so terribly slow? Ive seen some apps using this technique that did a better job than I have here, I even tinkered with one but I deleted the source for some unknow reason. It had a bit of a flicker every 10 or so frames but that was much better than what I have.
It was a ghosty halloween thingy if I remember properly.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (27 views)
2014-04-15 18:08:23

BurntPizza (22 views)
2014-04-15 03:46:01

UprightPath (38 views)
2014-04-14 17:39:50

UprightPath (20 views)
2014-04-14 17:35:47

Porlus (36 views)
2014-04-14 15:48:38

tom_mai78101 (61 views)
2014-04-10 04:04:31

BurntPizza (119 views)
2014-04-08 23:06:04

tom_mai78101 (219 views)
2014-04-05 13:34:39

trollwarrior1 (186 views)
2014-04-04 12:06:45

CJLetsGame (193 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!