2014-10-05 20 views
0

enter image description hereJava自定義形狀的框架使用圖像

我想創建一個Java JFrame的這個樣子image.i已經裝箱jframes不同形狀的三角形,圓形,多邊形和一些瘋狂的形狀。但問題是太硬[99%不可能]像這樣image.so我怎樣才能使像this.i一個JFrame用於創建形窗口該代碼創建形狀..

setUndecorated(true); 
Polygon polygon = new Polygon(); 
polygon.addPoint(0, 0); 
polygon.addPoint(100,100); 

GeneralPath path = new GeneralPath(); 
path.append(polygon, true); 
setShape(path); 

現在可以我此圖像轉換成的形狀。然後設置setshapes.any的想法? 還是有反正使jframe的完全transperent和jlable哪些圖像完全可見?

+0

說實話,你會得到一個更好的結果,你使用透明的框架並將圖像繪製到背景中,您將獲得抗鋸齒而不是銳降像素。有了一點聰明的工作,你也可以生成一個剪切矩形,如果你需要它 – MadProgrammer 2014-10-05 20:25:37

+0

@MadProgrammer當我搜索,我發現你回答的答案數量。你可以解釋更多如何做到這一點。我不需要代碼,但程序。我不會說流利的英語。我試圖通過將框架不透明度設置爲0.但所有框架隱藏。我試圖設置不透明的錯誤。它不工作 – 2014-10-05 23:56:02

+0

所以@MadProgrammer我記得我在c#中使用透明key.but但我仍然沒有找到一種方法來使窗口透明和圖像完全可見 – 2014-10-06 00:06:30

回答

3

要透明窗口,您需要在框架背景色的alpha設置爲0 。這可能是我在一段時間內看到的最直觀的對話,就好像你對其他Swing組件做了這樣的事情,你會完全搞砸油漆過程。

你不想改變窗口的透明度,因爲它的有生力量整個窗口和它同樣的內容。

例如...

JWindow frame = new JWindow(); 
frame.setBackground(new Color(0, 0, 0, 0)); 

您不必使用JWindow,但這意味着我不需要去除裝飾它自己...

您還需要使確保您添加到窗口任何內容即是透明的(不透明= FALSE),所以它不會「隱藏」什麼是它的下面...

Leaf Window

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.GridBagLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.image.BufferedImage; 
import java.io.IOException; 
import javax.imageio.ImageIO; 
import javax.swing.JButton; 
import javax.swing.JPanel; 
import javax.swing.JWindow; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 
import javax.swing.border.CompoundBorder; 
import javax.swing.border.EmptyBorder; 
import javax.swing.border.LineBorder; 

public class LeafWindow { 

    public static void main(String[] args) { 
     new LeafWindow(); 
    } 

    public LeafWindow() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       JWindow frame = new JWindow(); 
       frame.setBackground(new Color(0, 0, 0, 0)); 
       frame.setContentPane(new LeafPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
       frame.setAlwaysOnTop(true); 
      } 
     }); 
    } 

    public class LeafPane extends JPanel { 

     private BufferedImage leaf; 

     public LeafPane() { 

      setBorder(new CompoundBorder(
          new LineBorder(Color.RED), 
          new EmptyBorder(0, 0, 250, 0))); 

      try { 
       leaf = ImageIO.read(getClass().getResource("/Leaf.png")); 
      } catch (IOException ex) { 
       ex.printStackTrace(); 
      } 

      setOpaque(false); 
      setLayout(new GridBagLayout()); 

      JButton button = new JButton("Close"); 
      button.addActionListener(new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        System.exit(0); 
       } 
      }); 

      add(button); 

     } 

     @Override 
     public Dimension getPreferredSize() { 
      return leaf == null ? new Dimension(200, 200) : new Dimension(leaf.getWidth(), leaf.getHeight()); 
     } 

     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      if (leaf != null) { 
       Graphics2D g2d = (Graphics2D) g.create(); 
       g2d.drawImage(leaf, 0, 0, this); 
       g2d.dispose(); 
      } 
     } 
    } 

} 

本示例故意爲內容添加一個線邊框,因爲您可以看到原始窗口邊界是什麼。它也使用EmptyBorder強制JButton到圖形上,但這僅僅是一個例子...

+0

謝謝你,太棒了.. – 2014-10-06 00:22:08

+0

你是爲了一個有趣的旅程,很高興它幫助;) – MadProgrammer 2014-10-06 00:24:43

0

您必須根據圖像創建一個形狀。在這裏有不同的線程提供了一些方法如何做到這一點。最好的(根據描述,我沒有自己嘗試)可能是Java - Create a shape from border around image。更復雜的圖像的另一種選擇可能是Image/Graphic into a Shape

另一種解決辦法可能是使用未修飾的框架與「每像素的透明度」一起,作爲甲骨文解釋這裏:http://docs.oracle.com/javase/tutorial/uiswing/misc/trans_shaped_windows.html