Repainting Over Translucent Frames and Components
In Java on OSX, creating a translucent window and adding a JLabel that updates its text every second can result in issues with repainting. To resolve this problem, the component's repainting behavior can be customized.
One solution is to extend JLabel and implement Icon to gain greater control over transparency and redrawing. As seen in the AlphaCompositeDemo, various rule combinations can be applied to achieve the desired transparency effect. In this example, 100% white text is overlaid on a 50% black background.
Alternatively, you can make the entire frame translucent, though this will also dim the content. This can be achieved by overriding the paintComponent() method to adjust the transparency and draw the updated content correctly.
Here's a sample code snippet that demonstrates how to create a translucent frame and paint opaque text over it:
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()); } }
By implementing custom painting logic and employing transparency effects through AlphaComposite, you can effectively solve the repainting issue in translucent Java components on OSX.
The above is the detailed content of How to Solve Repainting Issues in Translucent Java Components on OSX?. For more information, please follow other related articles on the PHP Chinese website!