Get the App
SLTechnology News&Howtos  ›  Development  › 

Using Javaswing to realize the discoloration effect of desktop lyric font in music player

Shulou Source: shulou.com Published: 2022-06-03 11:58:43 10月05日 Update

This article introduces the knowledge of "using Javaswing to realize the discoloration effect of desktop lyric fonts in music players". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

You can change the ratio of the two colors with the following code

StringTwoColor stc = new StringTwoColor (jl1, Color.RED, Color.gray, 0.8)

Of course, you can use only one word.

The following is the complete code that contains the test section:

Import java.awt.BorderLayout;import java.awt.Color;import java.awt.Font;import java.awt.FontMetrics;import java.awt.Graphics2D;import java.awt.Image;import java.awt.RenderingHints;import java.awt.image.BufferedImage; import javax.swing.ImageIcon;import javax.swing.JFrame;import javax.swing.JLabel; public class StringTwoColor {private BufferedImage buffImg / * * @ param label display font JLabel * @ param C1 color 1 * @ param c2 color 2 * @ param ratio ratio of color 1 to color 2 * / public StringTwoColor (JLabel label, Color C1, Color c2, double ratio) {/ / get the width of the string (px displayed on the screen) FontMetrics metrics = label.getFontMetrics (label.getFont ()); int width = metrics.stringWidth (label.getText ()) Int height = metrics.getHeight (); height+=label.getFont (). GetSize (); / / construct a BufferedImage buffImg = new BufferedImage (width,height,BufferedImage.TYPE_4BYTE_ABGR) with specified size and type as one of the predefined image types; BufferedImage buffImg1 = new BufferedImage (width,height,BufferedImage.TYPE_4BYTE_ABGR); BufferedImage buffImg2 = new BufferedImage (width,height,BufferedImage.TYPE_4BYTE_ABGR); / / create a Graphics2D object Graphics2D G1 = buffImg1.createGraphics () through BufferedImage Graphics2D G2 = buffImg2.createGraphics (); / / set anti-aliasing g1.setRenderingHint (RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setRenderingHint (RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); / / set font g1.setFont (label.getFont ()); g2.setFont (label.getFont ()); / / set color g1.setColor (C1); g2.setColor (c2) / draw the string g1.drawString (label.getText (), 0, height-label.getFont (). GetSize ()); g2.drawString (label.getText (), 0, height-label.getFont (). GetSize ()); / / proportionally clear the relevant pixels if (ratio0) {int rgb = 0x00000000; / / clear buffImg1 for (int, y, rgb) {buffImg1.setRGB (x, y, rgb) {buffImg1.setRGB (x, y, rgb);} / clear buffImg2 for (int

Tags: Colors fonts ratios players effects desktops lyrics music discoloration code pixels content characters strings more knowledge types parts processing practical Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Apple Microsoft Xiaomi Docker