Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (535)
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  
  (another) Translucent window  (Read 4913 times)
0 Members and 1 Guest are viewing this topic.
Offline JuddMan

Senior Member


Medals: 1


Your Ad Here


« Posted 2007-04-27 17:19:20 »

you have probably seen this before, it is another screenshot-based implementation of a translucent window. (is there any other way besides JNI? if so, this is news to me.)

some things to note about it:
- initialises with a string for the window image(found using getClass().getResource() on it)
- it doesn't refresh its background, except when being dragged, and even then, it only grabs a shot of the area it is moving into. good, because the window never flickers bad because the background more easily gets out of sync if you never move it.
- you can put ordinary components onto its ContentPane
- uses double buffering so should be very flicker-free.
- performance is better with GIF images than PNG images. (ie, transparency is much faster than translucency)
- for best results, use with setUndecorated(true) to hide the JFrame's window decorations.

a picture is worth a thousand words as they say:



here is the class file:
BackgroundWindow.java
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  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
import java.awt.*;
import javax.swing.*;
import java.awt.image.*;
import javax.imageio.*;
import java.awt.event.*;


class InvisiblePanel extends JPanel
{
   public void paint(Graphics g)
   {
      paintChildren(g);
   }
}

public class BackgroundWindow extends JFrame implements MouseListener, MouseMotionListener
{
   final int     delay = 1;
   int           x, y, w, h,
                 oldX, oldY,
                 mouseX = 0,
                 mouseY = 0;
   long          time;
   
   BufferedImage image,
                 bgImage,
                 bgBuffer,
                 windowBuffer,
                 nativeImage;
   Robot         rob;
   
   public BackgroundWindow(String picture)
   {
      x = 0; y = 0;
      try{
         image = ImageIO.read(getClass().getResource(picture));
      } catch (Throwable e) {image = null;}
      w = image.getWidth();
      h = image.getHeight();
      setSize(w, h);
      addMouseListener(this);
      addMouseMotionListener(this);
      setContentPane(new InvisiblePanel());
   }
   
   public void paint(Graphics windowGraphics)
   {
      if(nativeImage == null || bgImage == null) {
         nativeImage = getGraphicsConfiguration().createCompatibleImage(w, h);
         windowBuffer = getGraphicsConfiguration().createCompatibleImage(w, h);
         setVisible(false);
         try{
            Thread.sleep(100);
            rob = new Robot(getGraphicsConfiguration().getDevice());
         }
         catch(Exception e) {System.exit(1);}
         bgImage = rob.createScreenCapture(new Rectangle(x, y, w, h));
         bgBuffer = rob.createScreenCapture(new Rectangle(x, y, w, h));
         setVisible(true);
      }
      Graphics g = windowBuffer.getGraphics();
      nativeImage.getGraphics().drawImage(bgImage, 0, 0, this);
      nativeImage.getGraphics().drawImage(image, 0, 0, this);
      g.drawImage(nativeImage, 0, 0, this);
      getContentPane().paint(g);
      windowGraphics.drawImage(windowBuffer, 0, 0, this);
     
   }
   public void repaint()
   {
      bgImage = null;
      super.repaint();
   }
   public void setLocation(int newX, int newY)
   {
      if(bgBuffer != null) {
         int dx = newX - x;
         int dy = newY - y;
         //first, copy background image onto iteslf displaced.
        bgBuffer.getGraphics().drawImage(bgImage, -dx, -dy, this);
         
         //second, copy new region into background...
        if(dx > 0) {
            int my = dy;
            if(my < 0) my = -my;
            Rectangle r = new Rectangle(x + w, y-my, dx, h+my*2);
            Image strip = rob.createScreenCapture(r);
            bgBuffer.getGraphics().drawImage(strip, w-dx, -dy-my, this);
         }
         else if(dx < 0) {
            int my = dy;
            if(my < 0) my = -my;
            Rectangle r = new Rectangle(x+dx, y-my, -dx, h+my*2);
            Image strip = rob.createScreenCapture(r);
            bgBuffer.getGraphics().drawImage(strip, 0, -dy-my, this);
         }
         if(dy > 0) {
            Rectangle r = new Rectangle(x, y+h, w, dy);
            Image strip = rob.createScreenCapture(r);
            bgBuffer.getGraphics().drawImage(strip, -dx, h-dy, this);
         }
         else if(dy < 0) {
            Rectangle r = new Rectangle(x, y+dy, w, -dy);
            Image strip = rob.createScreenCapture(r);
            bgBuffer.getGraphics().drawImage(strip, -dx, 0, this);
         }
         //finally, put bgBuffer back to bgImage.
        bgImage.getGraphics().drawImage(bgBuffer, 0, 0, this);
         
         this.x = newX;
         this.y = newY;
         paint(getGraphics());
      }
      super.setLocation(x, y);
      pause(1); //stops jitter
  }
   
   
   public void mousePressed(MouseEvent e){mouseX = e.getX(); mouseY = e.getY();}
   public void mouseReleased(MouseEvent e){mouseX = e.getX(); mouseY = e.getY();}
   public void mouseClicked(MouseEvent e){mouseX = e.getX(); mouseY = e.getY();}
   public void mouseEntered(MouseEvent e){mouseX = e.getX(); mouseY = e.getY();}
   public void mouseExited(MouseEvent e){mouseX = e.getX(); mouseY = e.getY();}
   
   //for dragging the window by any blank area
  public void mouseDragged(MouseEvent e){
      //first, calculate the distance moved...
     long now = System.currentTimeMillis();
      if(now - time < delay) return;
      time = now;
     
      int dx = e.getX() - mouseX;
      int dy =  e.getY() - mouseY;
     
      setLocation(x + dx, y + dy);
      mouseX = e.getX() - dx; mouseY = e.getY() - dy;
   }
   public void mouseMoved(MouseEvent e){}
   
   static void pause(long time) {
      try{ Thread.sleep(time);}
      catch(Exception e){}
   }
}


and here's how i used it in the screenshot above:
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  
import java.awt.*;
import javax.swing.*;
import java.awt.image.*;

public class Demo
{
   public static void main(String[] args)
   {
      JFrame mainFrame = new BackgroundWindow("winda.png");
      mainFrame.setUndecorated(true);
      JPanel cp = (JPanel)mainFrame.getContentPane();
      cp.setLayout(null);
     
      JButton b = new JButton("Hi there!");
      b.setLocation(130,10);
      b.setSize(100,30);  
      cp.add(b);
     
     
      JTextArea text = new JTextArea("this doesnt work quite right.");
      text.setFont(new Font("Arial", Font.BOLD, 14));
      text.setForeground(Color.blue);
      text.setLineWrap(true);       //line wrapping
     text.setWrapStyleWord(true);  //wrap on end of words.
     
      JScrollPane scroll = new JScrollPane(text);
     
      scroll.setSize(300,70);
      scroll.setLocation(40, 70);
      cp.add(scroll);
     
      mainFrame.setVisible(true);
   }
}
Offline Coinerson

Junior Member




Introducing the world's cutest zombie, Timmy


« Reply #1 - Posted 2007-05-20 23:23:02 »

I gave up trying to do it on PCs, on a mac its just setting the window to translucent.
Offline Cutter

Senior Newbie





« Reply #2 - Posted 2007-06-07 21:50:00 »

Nice work JuddMan looks great!!
I've been having a problem and you might be the person to help me, do you know how to make a color chooser translucent (well the background anyway) ? I'm talking about the panels on a color chooser that isn't displayed in a JDialog.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline JuddMan

Senior Member


Medals: 1


Your Ad Here


« Reply #3 - Posted 2007-08-25 18:11:14 »

what you could do is make a new class extending it and override its paint method to skip paintComponent and paintBorder or something. that's what i had to do with the content pane.
though it might be more difficult than that.

I decided to make something a bit more interactive with it: a bouncy ball that you can pick up and throw around your screen
http://www.adam.com.au/kellyjones/YABB/Bouncy.jar
http://juddman.googlepages.com/Bouncy.jar

it's an 'executable' jar. just double click it to run if your java is installed correctly.

(all source is included in the jar so just unzip and have a look it if you are curious / paranoid)

They say simple things ammuse small minds.
This kept me ammused for a couple of minutes  Grin

you could add things like pinball bumpers that the user could place on screen and see how many points they can get with a single throw.
Offline irreversible_kev

Junior Member





« Reply #4 - Posted 2007-08-25 18:51:14 »

really really cool Grin
Offline SwampChicken
« Reply #5 - Posted 2008-05-15 04:58:02 »

I just tried the code at the top, opened a graphics program, created a blue box and saved it as 'winda.png'.
The demo worked but sadly the window wasn't translucent at all.
Offline JuddMan

Senior Member


Medals: 1


Your Ad Here


« Reply #6 - Posted 2008-05-18 07:58:33 »

Was the picture you saved translucent in the graphics program?

here's winda.png used in the screenshot above:
http://juddman.googlepages.com/winda.png
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #7 - Posted 2008-05-19 00:05:24 »

I've always just set the background color of the JFrame to have alpha in it. But you can't get curved edges too easily that way.

See my work:
OTC Software
Offline JuddMan

Senior Member


Medals: 1


Your Ad Here


« Reply #8 - Posted 2008-05-19 14:13:51 »

what java version and OS? and can you post an example? if i try it on 1.4 or 1.6, it just shows the solid colour.

Here's another demo.
<img src="http://juddman.googlepages.com/iconnect.jpg">
Download:
http://juddman.googlepages.com/ConnectionTest.jar

- Makes a noise if the net drops out. handy for gamers.
- For best results, change www.google.com in NetConn.java to your own ISP (unzip the JAR to get at the source).

I should have made it do something smarter like prompt for a hostname to ping on the first run, but i didn't.
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #9 - Posted 2008-05-19 20:33:31 »

I do it in Mac OS X, and looking above it appears that this is the only OS you can do that on. Bummer.

See my work:
OTC Software
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline SwampChicken
« Reply #10 - Posted 2008-05-20 07:31:03 »

Was the picture you saved translucent in the graphics program?

here's winda.png used in the screenshot above:
http://juddman.googlepages.com/winda.png

Ahhh...
No wonder I couldn't see the code was that setting the transparency of the loaded image...
Wink
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.

E.R. Fleming (12 views)
2014-07-29 03:07:13

E.R. Fleming (4 views)
2014-07-29 03:06:25

pw (39 views)
2014-07-24 01:59:36

Riven (39 views)
2014-07-23 21:16:32

Riven (26 views)
2014-07-23 21:07:15

Riven (28 views)
2014-07-23 20:56:16

ctomni231 (59 views)
2014-07-18 06:55:21

Zero Volt (50 views)
2014-07-17 23:47:54

danieldean (42 views)
2014-07-17 23:41:23

MustardPeter (44 views)
2014-07-16 23:30:00
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!