2012-08-07 25 views
8

マウスの後ろにある円形のウィンドウを作成しようとしています。Java透過ウィンドウ

私はこれをPythonとQt(Python overlay windowを参照)で行っていましたが、その後JavaとSwingに切り替えました。しかし、私はウィンドウを透明にすることができません。私はthis methodを試しましたが、動作しませんが、Screencast-O-Matic(Javaの場合)を開始すると、実際には透過的なので、私のシステムは透明性をサポートしていると思います。

どうすればいいですか?あなたが自分でそれをしたい場合は、外部のlibを使用せずに、あなたが実行するスレッドを開始することができ

+0

この問題の解決方法はありますか?私は同じ問題に直面しています。私はScreencast-O-Maticのようなアプリケーションを作っています。これはWindows OSでうまく動作しますが、Linuxでは動作しません。何か見つかったらここに提案してください。http://stackoverflow.com/questions/25009276/swing-works-different-on-different-platform – tarkikshah

+0

いいえ、私はJavaで他の問題があったので、スクリーンキャストアプリケーションのPyQtに戻ってきました。 –

+0

ok ...ありがとう.. – tarkikshah

答えて

2

(私はLinuxのKDE4によ):

  • 目に見えない透明な窓を設定します

それとも、使用することができ、あなたのウィンドウの背景画像として、このスクリーンショットを置くデスクトップ

  • のスクリーンショットを作るJavaFX

  • +0

    ウィンドウがマウスに追随するので、私はこれを行うことができないので、スクリーンショットをしばらくお待ちください。非効率的な方法だと思います。 –

    +0

    と他のオプションを使用するのはどうですか? –

    +0

    申し訳ありませんが、Linux用のJavaFXとJDK 6が見つかりません –

    9

    JavaチュートリアルHow to Create Translucent and Shaped Windowsはなぜ機能しませんでしたか? Java 6またはJava 7の最新バージョンを使用していますか? May/June issue of Java Magazineには、Java 7を必要とするシェイプされた透明なウィンドウに関するチュートリアルがありました。Javaの雑誌を読むためには、おそらくサインアップする必要があります。

    import java.awt.*; //Graphics2D, LinearGradientPaint, Point, Window, Window.Type; 
    import javax.swing.JComponent; 
    import javax.swing.JFrame; 
    import javax.swing.SwingUtilities; 
    
    /** 
    * From JavaMagazine May/June 2012 
    * @author josh 
    */ 
    public class ShapedAboutWindowDemo { 
    
        /** 
        * @param args the command line arguments 
        */ 
        public static void main(String[] args) { 
         //switch to the right thread 
         SwingUtilities.invokeLater(new Runnable() { 
          @Override 
          public void run() { 
           JFrame frame = new JFrame("About box"); 
           //turn of window decorations 
           frame.setUndecorated(true); 
           //turn off the background 
           frame.setBackground(new Color(0,0,0,0)); 
           frame.setContentPane(new AboutComponent()); 
           frame.pack(); 
           //size the window 
           frame.setSize(500, 200); 
           frame.setVisible(true); 
           //center on screen 
           frame.setLocationRelativeTo(null); 
          } 
         } 
         ); 
        } 
    
        private static class AboutComponent extends JComponent { 
         public void paintComponent(Graphics graphics) { 
          Graphics2D g = (Graphics2D) graphics; 
    
          //create a translucent gradient 
          Color[] colors = new Color[]{ 
              new Color(0,0,0,0) 
              ,new Color(0.3f,0.3f,0.3f,1f) 
              ,new Color(0.3f,0.3f,0.3f,1f) 
              ,new Color(0,0,0,0)}; 
          float[] stops = new float[]{0,0.2f,0.8f,1f}; 
          LinearGradientPaint paint = new LinearGradientPaint(
                 new Point(0,0), new Point(500,0), 
                 stops,colors); 
          //fill a rect then paint with text 
          g.setPaint(paint); 
          g.fillRect(0, 0, 500, 200); 
          g.setPaint(Color.WHITE); 
          g.drawString("My Killer App", 200, 100); 
         } 
        } 
    } 
    
    +0

    Java 6を使用しているため、ページ下部の互換性メソッドを使用しています...あなたの例で私は[this](https://dl.dropbox.com/u/9287758/Immagini/JavaNoTransparency.png) –

    +0

    OKです。 JavaチュートリアルのサイトにはJava 6 update 10が必要です。これがあれば動作するはずです。そうでなければ、Linuxの互換性の問題があるはずです。その場合、JDK 7にアップグレードし、それが修正されるかどうかを確認することをお勧めします。 – Thorn

    3

    Java 6を使用している場合は、プライベートAPI AWTUtilitiesを使用する必要があります。詳細

    例えばJava SE 6 Update 10 APIをチェック

    これは、クイックハックのビットですが、それはそれはJavaの下で

    を生成Windows 7では

    public class TransparentWindow { 
    
        /** 
        * @param args the command line arguments 
        */ 
        public static void main(String[] args) { 
    
         EventQueue.invokeLater(new Runnable() { 
          @Override 
          public void run() { 
    
           MyFrame frame = new MyFrame(); 
           frame.setUndecorated(true); 
    
           String version = System.getProperty("java.version"); 
           if (version.startsWith("1.7")) { 
    
    
            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); 
            GraphicsDevice graphicsDevice = ge.getDefaultScreenDevice(); 
    
            System.out.println("Transparent from under Java 7"); 
            /* This won't run under Java 6, uncomment if you are using Java 7 
            System.out.println("isPerPixelAlphaTranslucent = " + graphicsDevice.isWindowTranslucencySupported(GraphicsDevice.WindowTranslucency.PERPIXEL_TRANSLUCENT)); 
            System.out.println("isPerPixelAlphaTransparent = " + graphicsDevice.isWindowTranslucencySupported(GraphicsDevice.WindowTranslucency.PERPIXEL_TRANSPARENT)); 
            System.out.println("isPerPixelAlphaTranslucent = " + graphicsDevice.isWindowTranslucencySupported(GraphicsDevice.WindowTranslucency.TRANSLUCENT)); 
            */ 
            frame.setBackground(new Color(0, 0, 0, 0)); 
    
           } else if (version.startsWith("1.6")) { 
    
            System.out.println("Transparent from under Java 6"); 
            System.out.println("isPerPixelAlphaSupported = " + supportsPerAlphaPixel()); 
            setOpaque(frame, false); 
    
           } 
    
           frame.setSize(400, 400); 
           frame.setLocationRelativeTo(null); 
           frame.setVisible(true); 
    
          } 
         }); 
    
        } 
    
        public static class MyFrame extends JFrame { 
    
         public MyFrame() throws HeadlessException { 
    
          setContentPane(new MyContentPane()); 
          setDefaultCloseOperation(EXIT_ON_CLOSE); 
    
          addMouseListener(new MouseAdapter() { 
           @Override 
           public void mouseClicked(MouseEvent e) { 
    
            if (e.getClickCount() == 2) { 
    
             dispose(); 
    
            } 
    
           } 
          }); 
    
         } 
        } 
    
        public static class MyContentPane extends JPanel { 
    
         public MyContentPane() { 
    
          setLayout(new GridBagLayout()); 
          add(new JLabel("Hello, I'm a transparent frame under Java " + System.getProperty("java.version"))); 
    
          setOpaque(false); 
    
         } 
    
         @Override 
         protected void paintComponent(Graphics g) { 
    
          super.paintComponent(g); 
    
          Graphics2D g2d = (Graphics2D) g.create(); 
          g2d.setColor(Color.BLUE); 
    
          g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f)); 
          g2d.fillRoundRect(0, 0, getWidth() - 1, getHeight() - 1, 20, 20); 
    
         } 
        } 
    
        public static boolean supportsPerAlphaPixel() { 
    
         boolean support = false; 
    
         try { 
    
          Class<?> awtUtilsClass = Class.forName("com.sun.awt.AWTUtilities"); 
          support = true; 
    
         } catch (Exception exp) { 
         } 
    
         return support; 
    
        } 
    
        public static void setOpaque(Window window, boolean opaque) { 
    
         try { 
    
          Class<?> awtUtilsClass = Class.forName("com.sun.awt.AWTUtilities"); 
          if (awtUtilsClass != null) { 
    
           Method method = awtUtilsClass.getMethod("setWindowOpaque", Window.class, boolean.class); 
           method.invoke(null, window, opaque); 
    //    com.sun.awt.AWTUtilities.setWindowOpaque(this, opaque); 
    //    ((JComponent) window.getContentPane()).setOpaque(opaque); 
    
          } 
    
         } catch (Exception exp) { 
         } 
    
        } 
    
        public static void setOpacity(Window window, float opacity) { 
    
         try { 
    
          Class<?> awtUtilsClass = Class.forName("com.sun.awt.AWTUtilities"); 
          if (awtUtilsClass != null) { 
    
           Method method = awtUtilsClass.getMethod("setWindowOpacity", Window.class, float.class); 
           method.invoke(null, window, opacity); 
    
          } 
    
         } catch (Exception exp) { 
    
          exp.printStackTrace(); 
    
         } 
    
        } 
    
        public static float getOpacity(Window window) { 
    
         float opacity = 1f; 
         try { 
    
          Class<?> awtUtilsClass = Class.forName("com.sun.awt.AWTUtilities"); 
          if (awtUtilsClass != null) { 
    
           Method method = awtUtilsClass.getMethod("getWindowOpacity", Window.class); 
           Object value = method.invoke(null, window); 
           if (value != null && value instanceof Float) { 
    
            opacity = ((Float) value).floatValue(); 
    
           } 
    
          } 
    
         } catch (Exception exp) { 
    
          exp.printStackTrace(); 
    
         } 
    
    
         return opacity; 
    
        } 
    } 
    

    間でアイデアを取得します6 Java6

    Unde RのJava 7 Java7

    +0

    もう1つはコンパイルされていませんでした –

    +0

    私の例を調べてみるとわかりません – MadProgrammer

    +0

    私はJava 6でAWTUtilitiesでしかコンパイルできませんが、実行時に透明性はありません。 –

    3

    私はこれが動作すると思い、私はすでにit..toは、あなたが最初Undecorated(true)フレームをundecorateする必要があるのJFrameまたはウィンドウを透明にしようとしました。ここではサンプルコードは次のとおりです。

    import javax.swing.*; 
    import com.sun.awt.AWTUtilities; 
    import java.awt.Color; 
    
        class transFrame { 
         private JFrame f=new JFrame(); 
         private JLabel msg=new JLabel("Hello I'm a Transparent Window"); 
    
        transFrame() { 
         f.setBounds(400,150,500,500); 
         f.setLayout(null); 
         f.setUndecorated(true);  // Undecorates the Window 
         f.setBackground(new Color(0,0,0,25)); // fourth index decides the opacity 
         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
         msg.setBounds(150,250,300,25); 
         f.add(msg); 
         f.setVisible(true); 
         } 
    
         public static void main(String[] args) { 
         new transFrame(); 
         } 
    
        } 
    

    唯一の問題は、あなたが近いために独自のコードを追加して、ボタンを使用して最小化する必要があります。

    関連する問題