Java在半透明框架/面板/組件上重新繪畫(huà)。
我有一些運(yùn)氣可以擴(kuò)展JLabel和實(shí)現(xiàn),Icon以使半透明組件按我想要的方式工作。你可以在此AlphaCompositeDemo中看到各種規(guī)則組合的結(jié)果。下面的示例是100%白色,50%黑色。
附錄:請(qǐng)注意,此示例如何在半透明框架背景上的透明屏幕外背景上合成不透明文本。
附錄:這是使整個(gè)框架透明的一種方法。不幸的是,它也使內(nèi)容變暗。
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(); }}); }}解決方法
我正在嘗試在OSX上使用Java創(chuàng)建一個(gè)半透明窗口并將其添加JLabel到其中。
這JLabel每秒更改一次文本。
替代文字
但是,該組件不能很好地重新粉刷。
我怎么解決這個(gè)問(wèn)題?
我已經(jīng)找到了這些 文章,但是我不知道該如何解決。
如果可能,請(qǐng)粘貼修復(fù)源代碼,這是我的:
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.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++ ); }},1000 ); } }
相關(guān)文章:
1. css - 新手做響應(yīng)式布局, 斷點(diǎn)過(guò)后右側(cè)出現(xiàn)空白,求幫助,謝謝。2. javascript - 關(guān)于<a>元素與<input>元素的JS事件運(yùn)行問(wèn)題3. css3 - 純css實(shí)現(xiàn)點(diǎn)擊特效4. mysql - 查詢(xún)字段做了索引為什么不起效,還有查詢(xún)一個(gè)月的時(shí)候數(shù)據(jù)都是全部出來(lái)的,如果分拆3次的話就沒(méi)問(wèn)題,為什么呢。5. mysql - 記得以前在哪里看過(guò)一個(gè)估算時(shí)間的網(wǎng)站6. 大家好,我想請(qǐng)問(wèn)一下怎么做搜索欄能夠搜索到自己網(wǎng)站的內(nèi)容。7. ID主鍵不是自增的嗎 為什么還要加null8. MySQL中的enum類(lèi)型有什么優(yōu)點(diǎn)?9. python - 啟動(dòng)Eric6時(shí)報(bào)錯(cuò):’qscintilla_zh_CN’ could not be loaded10. javascript - vue 怎么渲染自定義組件
