重新油漆半透明帧/板/组件问题 [英] Re-paint problem on translucent frame/panel/component

查看:249
本文介绍了重新油漆半透明帧/板/组件问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建与Java在OSX上的半透明窗口,并添加的JLabel 它。

的JLabel 改变其文字每一秒......

然而该组件不重画好。

我该如何解决这个问题呢?

我已经找到了这些 <一个href=\"http://www.curious-creature.org/2007/04/10/translucent-swing-windows-on-mac-os-x/\">articles,但我无法弄清楚如何解决它。

如果可能,请粘贴固定源$ C ​​$ C,这是我的:

 进口javax.swing.JFrame中;
进口javax.swing.JPanel中;
进口javax.swing.JLabel中;
进口java.awt.Color中;
进口java.awt.Font中;
进口java.util.Timer中;
进口java.util.TimerTask中;公共类半透明{
    公共静态无效的主要(字串[] args){        JFrame的帧=新的JFrame();        frame.setBackground(新彩(0.0,0.0,0.0,0.3f));        最终的JLabel标签=新的JLabel(霍拉);
        label.setFont(新字体(label.getFont()getFamily(),Font.PLAIN,46));
        label.setForeground(Color.white);        frame.add(标签);
        frame.pack();
        frame.setLocationRelativeTo(NULL);
        frame.setVisible(真);        定时器定时器=新的Timer();
        timer.schedule(新的TimerTask(){
            INT I = 0;
            公共无效的run(){
                label.setText(霍拉+我+ +);
            }
        },0,1000);
    }
}


解决方案

我有一些运气延长的JLabel 和实施图标来得到一个半透明的部分工作就是我想要的。你可以看到在这个 AlphaCompositeDemo 各种规则组合的结果。 下面的例子是100%的白色顶上50%的黑色。

附录:请注意本例中如何复合背景屏幕之外在半透明边框背景上的一个明确的不透明文本

附录:这里有一个方法,使<一个href=\"http://devdaily.com/blog/post/jfc-swing/transparent-translucent-java-frame-jframe-mac-slider\">whole框架半透明。不幸的是,变暗的内容了。

 进口java.awt.AlphaComposite中;
进口java.awt.Color中;
进口java.awt.Dimension中;
进口java.awt.EventQueue中;
进口java.awt.Font中;
进口java.awt.FontMetrics中;
进口java.awt.Graphics;
进口java.awt.Graphics2D中;
进口java.awt.RenderingHints中;
进口java.awt.event.ActionEvent中;
进口java.awt.event.ActionListener;
进口java.awt.image.BufferedImage中;
进口java.text.SimpleDateFormat的;
进口java.util.Date;
进口javax.swing.JFrame中;
进口javax.swing.JPanel中;
进口javax.swing.Timer中;公共类半透明继承JPanel实现的ActionListener {    私有静态最终诠释W = 300;
    私有静态最终诠释H = 100;
    私有静态最后字体的字体=
        新的字体(衬线,Font.PLAIN,48);
    私有静态最后的SimpleDateFormat DF =
        新的SimpleDateFormat(HH:MM:SS);
    私人最终日期现在=新的日期();
    私人最终定时器定时器=新定时器(1000本);
    私人的BufferedImage时间;
    私人的Graphics2D timeG;    公共半透明(){
        超(真);
        this.set preferredSize(新尺寸(W,H));
        timer.start();
    }    @覆盖
    保护无效paintComponent(图形G){
        Graphics2D的G2D =(Graphics2D的)克;
        g2d.setRenderingHint(
            RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
        INT W = this.getWidth();
        INT H = this.getHeight();
        g2d.setComposite(AlphaComposite.Clear);
        g2d.fillRect(0,0,W,H);
        g2d.setComposite(AlphaComposite.Src);
        g2d.setPaint(g2d.getBackground());
        g2d.fillRect(0,0,W,H);
        渲染时间(G2D);
        INT W2 = time.getWidth()/ 2;
        INT H2 = time.getHeight()/ 2;
        g2d.setComposite(AlphaComposite.SrcOver);
        g2d.drawImage(时间,瓦特/ 2 - W2,H / 2 - H 2,空);
    }    私人无效的渲染(Graphics2D的G2D){
        g2d.setFont(字体);
        字符串s = df.format(现在的);
        FontMetrics对象FM = g2d.getFontMetrics();
        INT W = fm.stringWidth(S);
        INT H = fm.getHeight();
        如果(时间== NULL和放大器;&安培; timeG == NULL){
            时间=新的BufferedImage(W,H,BufferedImage.TYPE_INT_ARGB);
            timeG = time.createGraphics();
            timeG.setRenderingHint(
                RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
            timeG.setFont(字体);
        }
        timeG.setComposite(AlphaComposite.Clear);
        timeG.fillRect(0,0,W,H);
        timeG.setComposite(AlphaComposite.Src);
        timeG.setPaint(Color.green);
        timeG.drawString(S,0,fm.getAscent());
    }    私有静态无效创建(){
        JFrame的F =新的JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setBackground(新颜色(0F,0F,0F,0.3f));
        f.setUndecorated(真);
        f.add(新半透明());
        f.pack();
        f.setLocationRelativeTo(NULL);
        f.setVisible(真);
    }    @覆盖
    公共无效的actionPerformed(ActionEvent的五){
        now.setTime(System.currentTimeMillis的());
        this.repaint();
    }    公共静态无效的主要(字串[] args){
        EventQueue.invokeLater(新的Runnable(){
            @覆盖
            公共无效的run(){
                创建();
            }
        });
    }
}

I'm trying to create a translucent window with Java on OSX and add a JLabel to it.

This JLabel changes its text every second....

However the component is not repainting well.

How can I solve this problem?

I've found the these articles, but I can't figure out how to solve it.

If possible, please paste the fixing source code, here's mine:

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Color;
import java.awt.Font;
import java.util.Timer;
import java.util.TimerTask;

public class Translucent {
    public static void main( String [] args ) {

        JFrame frame = new JFrame();

        frame.setBackground( new Color( 0.0f,0.0f,0.0f,0.3f));

        final JLabel label =  new JLabel("Hola");
        label.setFont( new Font( label.getFont().getFamily(), Font.PLAIN, 46 ) );
        label.setForeground( Color.white );

        frame.add( label );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible( true );

        Timer timer = new Timer();
        timer.schedule( new TimerTask(){
            int i = 0;
            public void run() {
                label.setText("Hola "+ i++ );
            }
        }, 0, 1000 );


    }   
}

解决方案

I've had some luck extending JLabel and implementing Icon to get a translucent component working the way I want. You can see the result of various rule combinations in this AlphaCompositeDemo. The example below is 100% white atop 50% black.

Addendum: Note how this example composites opaque text on a clear offscreen background over the translucent frame background.

Addendum: Here's a way to make the whole frame translucent. Unfortunately, it dims the content, too.

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Translucent extends JPanel implements ActionListener {

    private static final int W = 300;
    private static final int H = 100;
    private static final Font font =
        new Font("Serif", Font.PLAIN, 48);
    private static final SimpleDateFormat df =
        new SimpleDateFormat("HH:mm:ss");
    private final Date now = new Date();
    private final Timer timer = new Timer(1000, this);
    private BufferedImage time;
    private Graphics2D timeG;

    public Translucent() {
        super(true);
        this.setPreferredSize(new Dimension(W, H));
        timer.start();
    }

    @Override
    protected void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(
            RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
        int w = this.getWidth();
        int h = this.getHeight();
        g2d.setComposite(AlphaComposite.Clear);
        g2d.fillRect(0, 0, w, h);
        g2d.setComposite(AlphaComposite.Src);
        g2d.setPaint(g2d.getBackground());
        g2d.fillRect(0, 0, w, h);
        renderTime(g2d);
        int w2 = time.getWidth() / 2;
        int h2 = time.getHeight() / 2;
        g2d.setComposite(AlphaComposite.SrcOver);
        g2d.drawImage(time, w / 2 - w2, h / 2 - h2, null);
    }

    private void renderTime(Graphics2D g2d) {
        g2d.setFont(font);
        String s = df.format(now);
        FontMetrics fm = g2d.getFontMetrics();
        int w = fm.stringWidth(s);
        int h = fm.getHeight();
        if (time == null && timeG == null) {
            time = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
            timeG = time.createGraphics();
            timeG.setRenderingHint(
                RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
            timeG.setFont(font);
        }
        timeG.setComposite(AlphaComposite.Clear);
        timeG.fillRect(0, 0, w, h);
        timeG.setComposite(AlphaComposite.Src);
        timeG.setPaint(Color.green);
        timeG.drawString(s, 0, fm.getAscent());
    }

    private static void create() {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setBackground(new Color(0f, 0f, 0f, 0.3f));
        f.setUndecorated(true);
        f.add(new Translucent());
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        now.setTime(System.currentTimeMillis());
        this.repaint();
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                create();
            }
        });
    }
}

这篇关于重新油漆半透明帧/板/组件问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆