2015-08-20 132 views
0

我想双缓冲透明JWindow然而,它看起来像使用的技术没有影响(不同的周期值相互绘制)。JWindow的双缓冲

public final class Overlay extends JWindow { 

    public static final Color TRANSPARENT = new Color(0, true); 
    public static Font standardFont = null; 

    public static Overlay open() { 
     return new Overlay(); 
    } 

    private Overlay() { 
     Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
     setAlwaysOnTop(true); 
     setBounds(0, 0, screenSize.width, screenSize.height); 
     setBackground(TRANSPARENT); 
    } 

    @Override 
    public void paint(Graphics g) { 
     BufferedImage bufferedImage = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_4BYTE_ABGR); 
     Graphics2D g2d = bufferedImage.createGraphics(); 
     paintUs(g2d); 

     Graphics2D g2dComponent = (Graphics2D) g; 
     g2dComponent.drawImage(bufferedImage, null, 0, 0); 
    } 

    private void paintUs(Graphics2D g) { 
     int height = 420; 
     int x = 20; 
     g.setColor(TRANSPARENT); 
     g.fillRect(0, 0, getWidth(), getHeight()); 
     g.setFont(standardFont == null ? standardFont = g.getFont().deriveFont(17f) : standardFont); 
     for (Plugin plugin : Abendigo.plugins()) { 
      g.setColor(Abendigo.plugins().isEnabled(plugin) ? Color.GREEN : Color.RED); 
      g.drawString(plugin.toString(), x + 5, getHeight() - height); 
      height += 20; 
     } 
     height += 20; 
     g.setColor(Color.YELLOW); 
     g.drawString("Cycle: " + Abendigo.elapsed + "ms", x, getHeight() - height); 
    } 

    @Override 
    public void update(Graphics g) { 
     paint(g); 
    } 

} 

回答

1

为什么!?!? Swing组件已经被双缓冲了?简单地创建一个自定义组件,从JPanel之类的延伸,覆盖它的paintComponent并在那里执行您的自定义绘画。请务必将组件设置为透明(setOpaque(false)),这增加的JWindow

实例见Painting in AWT and SwingPerforming Custom Painting的更多细节。

你面临的一个直接问题是,Swing窗口有一系列已经附加到它们的复合组件(JRootPane,contentPane等),所有这些都可以独立于你的窗口绘制,这意味着它们可以覆盖您试图直接在窗口上绘画的内容。相反,避免直接在窗口上绘画,而是使用自定义组件。

+0

它似乎没有双缓冲。不断变化的循环文本如下所示:http://i.imgur.com/HSPLJ1v.png – Jire

+0

可能的是,您已经打断了绘画链,但未能调用一个或多个超级绘画方法(如在您的示例中) – MadProgrammer

+0

谢谢,我创建了一个'JPanel'并在其中重写了'paintComponent',然后将其设置为opaque并将其背景设置为'TRANSPARENT'。很棒! – Jire