在半透明框架和组件上重新绘制
在 OSX 上的 Java 中,创建半透明窗口并添加每秒更新其文本的 JLabel 可以导致重新喷漆出现问题。为了解决这个问题,可以自定义组件的重画行为。
一种解决方案是扩展 JLabel 并实现 Icon,以更好地控制透明度和重画。正如 AlphaCompositeDemo 中所示,可以应用各种规则组合来实现所需的透明效果。在此示例中,100% 白色文本覆盖在 50% 黑色背景上。
或者,您可以使整个框架半透明,但这也会使内容变暗。这可以通过重写 PaintComponent() 方法来调整透明度并正确绘制更新的内容来实现。
以下示例代码片段演示了如何创建半透明框架并在其上绘制不透明文本:
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 TranslucentFrame extends JPanel implements ActionListener { // Frame configurations 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 TranslucentFrame() { super(true); 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 = getWidth(); int H = getHeight(); G2D.setComposite(AlphaComposite.Clear); G2D.fillRect(0, 0, W, H); G2D.setComposite(AlphaComposite.Src); G2D.setPaint(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 TranslucentFrame()); F.pack(); F.setLocationRelativeTo(null); F.setVisible(true); } @Override public void actionPerformed(ActionEvent E) { NOW.setTime(System.currentTimeMillis()); repaint(); } public static void main(String[] ARGS) { EventQueue.invokeLater(() -> create()); } }
通过AlphaComposite实现自定义绘制逻辑并使用透明效果,可以有效解决半透明Java组件的重画问题OSX.
以上是如何解决 OSX 上半透明 Java 组件的重画问题?的详细内容。更多信息请关注PHP中文网其他相关文章!