在半透明框架和組件上重新繪製
在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中文網其他相關文章!