2014-04-22 95 views
0

我在模糊背景中創建透明矩形時出現問題。我正在嘗試在glasspane上執行此任務。這是我的代碼片段。在jframe中模糊背景創建透明矩形

void createBlur() { 
    alpha = 1.0f; 
    JRootPane root = SwingUtilities.getRootPane(jf); 
    blurBuffer = GraphicsUtilities.createCompatibleImage(jf.getWidth(), jf.getHeight()); 
    Graphics2D g2d = blurBuffer.createGraphics(); 
    root.paint(g2d); 
    g2d.dispose(); 

    backBuffer = blurBuffer; 
    blurBuffer = GraphicsUtilities.createThumbnailFast(blurBuffer, jf.getWidth()/2); 
    blurBuffer = new GaussianBlurFilter(5).filter(blurBuffer, null); 
} 

其中,後備緩衝和blurBuffer是BufferedImage & JF = JFrame的目的,阿爾法用於不透明度。 上述方法非常好地創建了模糊效果。

這裏是一個在小組

protected void paintComponent(Graphics g) { 
    int x = 34; 
    int y = 34; 
    int w = getWidth() - 68; 
    int h = getHeight() - 68; 
    int arc = 30; 

    //Graphics2D g2 = currentGraphics.createGraphics(); 
    //g2.drawImage(currentGraphics, 0, 0, null); 
    Graphics2D g2 = (Graphics2D) g.create(); 
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 

    g2.setColor(new Color(0, 0, 0, 220)); 
    g2.fillRoundRect(x, y, w, h, arc, arc); 

    g2.setStroke(new BasicStroke(1f)); 
    g2.setColor(Color.WHITE); 
    g2.drawRoundRect(x, y, w, h, arc, arc); 

    g2.dispose(); 
} 

現在在哪兒,我堅持是我怎麼畫在同一時間的模糊效果和透明的矩形創建一個透明的矩形的代碼。 我沒有在這裏發佈整個代碼,如果有人希望看到這裏的代碼link

這裏是樣本輸出的所需圖像。提前致謝。

enter image description here

+0

爲了更好地幫助越早,張貼[MCVE](http://stackoverflow.com/help/mcve)(最小完備和可驗證的實施例)。並請澄清屏幕截圖。這是你想要的效果還是你現在得到的?如果'現在',它有什麼問題? –

+1

關於鏈接到您的Google共享驅動器。很少有人會遵循它,更少的人會「請求訪問」。至少讓人們將共享文件標記爲「公開」給任何有鏈接的人。 –

+0

@AndrewThompson這是所需的輸出和這個輸出應該來。我能夠創建一個透明的矩形和背景模糊,但我不知道如何將它們合併在一起。對不起,沒有將文件分享爲'Public' –

回答

2

我試圖使頭和你的代碼的尾巴......

你不能叫super.paintComponent ......這可能會導致你到同一個嚴重的問題,如果你不小心。一般的經驗法則,只是把它;)

modifiying一個Graphics上下文的狀態時,例如要小心......

g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 

會影響這一塊塗上之後的所有組件,並可能導致一些有趣故障的圖形,你沒有想到......

jf.getGlassPane().setVisible(false); 
glassPanel = new GlassPanel(); 
jf.getGlassPane().setVisible(true); 

似乎毫無意義,因爲使用jf.setGlassPane(glassPanel);依然會當你調用jf.getGlassPane().setVisible(true);是可見的組件的組件集。這也意味着,GlassPane組件是從未使用過...

檢查isVisiblepaintComponent是毫無意義的,因爲Swing是足夠聰明的知道不畫無形成分...

現在,說了這麼多。 ..

如果你想畫的,你要麼...的blurBuffer後漆的內容繪製BlurPanel的頂部,因此您繪製ontop的它或添加其他組件到BlurPanel窗格中包含繪圖邏輯你想申請...

這是這個概念的一個基本例子。這將另一個面板添加到玻璃板上,根據需要繪製面板的自定義框架。

本示例使用個人庫代碼,僅作爲概念的示例,而不是完全可運行的示例。

BeforeAfter

import core.ui.GlowEffectFactory; 
import core.ui.GraphicsUtilities; 
import core.util.ByteFormatter; 
import java.awt.BasicStroke; 
import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Container; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.Font; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.Insets; 
import java.awt.Window; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.geom.RoundRectangle2D; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 
import java.text.DateFormat; 
import java.util.Date; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.imageio.ImageIO; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JRootPane; 
import javax.swing.SwingUtilities; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 
import javax.swing.border.EmptyBorder; 

public class TransparentTest { 

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

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

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       frame.add(new TestPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class TestPane extends JPanel { 

     private BufferedImage background; 
     private BlurredGlassPane blurredGlassPane; 

     private InfoPane infoPane; 

     public TestPane() { 
      try { 
       background = ImageIO.read(new File("get your own image")); 
      } catch (IOException ex) { 
       ex.printStackTrace(); 
      } 

      blurredGlassPane = new BlurredGlassPane(); 
      blurredGlassPane.setLayout(new GridBagLayout()); 
      infoPane = new InfoPane(); 
      infoPane.setFile(new File("get your own image")); 
      blurredGlassPane.add(infoPane); 

      JButton click = new JButton("Click"); 
      click.addActionListener(new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        Window win = SwingUtilities.getWindowAncestor(TestPane.this); 
        if (win instanceof JFrame) { 
         JFrame frame = (JFrame) win; 
         frame.setGlassPane(blurredGlassPane); 
         blurredGlassPane.setVisible(true); 
        } 
       } 
      }); 

      setLayout(new GridBagLayout()); 
      add(click); 
     } 

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

     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      if (background != null) { 
       Graphics2D g2d = (Graphics2D) g.create(); 
       int x = (getWidth() - background.getWidth())/2; 
       int y = (getHeight() - background.getHeight())/2; 
       g2d.drawImage(background, x, y, this); 
       g2d.dispose(); 
      } 
     } 
    } 

    public static class InfoPane extends JPanel { 

     protected static final int RADIUS = 20; 
     protected static final int FRAME = 4; 
     protected static final int INSET = RADIUS + FRAME; 
     protected static final DateFormat DATE_FORMAT = DateFormat.getDateTimeInstance(); 

     private JLabel name; 
     private JLabel path; 
     private JLabel length; 
     private JLabel lastModified; 
     private JLabel canExecute; 
     private JLabel canRead; 
     private JLabel canWrite; 
     private JLabel isDirectory; 
     private JLabel isHidden; 

     public InfoPane() { 
      setBorder(new EmptyBorder(INSET, INSET, INSET, INSET)); 
      setOpaque(false); 
      setLayout(new GridBagLayout()); 
      GridBagConstraints gbc = new GridBagConstraints(); 
      gbc.gridx = 0; 
      gbc.gridy = 0; 
      gbc.weightx = 1; 
      gbc.anchor = GridBagConstraints.WEST; 
      gbc.gridwidth = GridBagConstraints.REMAINDER; 

      name = createLabel(Font.BOLD, 48); 
      add(name, gbc); 

      gbc.gridy++; 
      path = createLabel(); 
      add(path, gbc); 

      gbc = new GridBagConstraints(); 
      gbc.gridx = 0; 
      gbc.gridy = 2; 
      gbc.anchor = GridBagConstraints.WEST; 

      length = createLabel(); 
      lastModified = createLabel(); 
      add(createLabel("Size: "), gbc); 

      gbc.gridx++; 
      gbc.insets = new Insets(0, 0, 0, 10); 
      add(length, gbc); 

      gbc.insets = new Insets(0, 0, 0, 0); 
      gbc.gridx++; 
      add(createLabel("Last Modified: "), gbc); 

      gbc.gridx++; 
      add(lastModified, gbc); 
     } 

     public JLabel createLabel(String text) { 

      JLabel label = new JLabel(text); 
      label.setForeground(Color.WHITE); 
      return label; 

     } 

     public JLabel createLabel() { 

      return createLabel(""); 

     } 

     public JLabel createLabel(int style, float size) { 

      JLabel label = createLabel(); 
      label.setFont(label.getFont().deriveFont(style, size)); 
      return label; 
     } 

     public void setFile(File file) { 

      name.setText(file.getName()); 
      try { 
       path.setText(file.getParentFile().getCanonicalPath()); 
      } catch (IOException ex) { 
       ex.printStackTrace(); 
      } 
      length.setText(ByteFormatter.format(file.length())); 
      lastModified.setText(DATE_FORMAT.format(new Date(file.lastModified()))); 
      file.canExecute(); 
      file.canRead(); 
      file.canWrite(); 
      file.isDirectory(); 
      file.isHidden(); 

     } 

     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      Graphics2D g2d = (Graphics2D) g.create(); 
      GraphicsUtilities.applyQualityRenderingHints(g2d); 
      int width = getWidth() - 1; 
      int height = getHeight() - 1; 
      int buffer = FRAME/2; 
      RoundRectangle2D base = new RoundRectangle2D.Double(buffer, buffer, width - FRAME, height - FRAME, RADIUS, RADIUS); 
      g2d.setColor(new Color(0, 0, 0, 128)); 
      g2d.fill(base); 
      g2d.setColor(Color.WHITE); 
      g2d.setStroke(new BasicStroke(FRAME, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)); 
      g2d.draw(base); 
      g2d.dispose(); 
     } 

    } 

    public class BlurredGlassPane extends JPanel { 

     private BufferedImage background; 

     @Override 
     public void setVisible(boolean visible) { 
      if (visible) { 
       Container parent = SwingUtilities.getAncestorOfClass(JRootPane.class, this); 
       if (parent != null) { 
        JRootPane rootPane = (JRootPane) parent; 

        BufferedImage img = new BufferedImage(rootPane.getWidth(), rootPane.getHeight(), BufferedImage.TYPE_INT_ARGB); 
        Graphics2D g2d = img.createGraphics(); 
        rootPane.printAll(g2d); 
        g2d.dispose(); 

        background = GlowEffectFactory.generateBlur(img, 40); 
       } 
      } 
      super.setVisible(visible); 
     } 

     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      g.drawImage(background, 0, 0, this); 
     } 

    } 

} 
+0

感謝您給我一個演示,但我沒有太多關於Graphics2D中RenderingHints和許多事情的知識,因爲java文檔didn' t提供了關於RenderingHints及其常量的很多信息。我需要時間來理解你的答案,但無論我明白告訴我是對還是錯? 1.創建一個面板,其中保存背景圖像或漸變 2.創建它的模糊效果並最終創建詳細信息面板(透明矩形)。 3.在該面板上繪製透明矩形 4.最後在根葉片上繪製圖像。 –

+0

'jf.getGlassPane()。setVisible(false); glassPanel = new GlassPanel();在這些語句中,我試圖改變glasspane的內容,因爲swing的規則說不要嘗試改變任何可見組件的內容。這就是爲什麼我試圖做這些事情。 –

+0

不是。 1.創建背景面板(圖像/漸變)。 2.創建此模糊圖像。 3.創建可以繪製模糊圖像的面板。 4.添加到「模糊面板」。模糊面板必須出現在背景面板上方,並且所有東西都必須出現在背景面板上方... – MadProgrammer