Java-如何在Java中编写渐变文本?

Java-How to Write Gradient Text in java?(Java-如何在Java中编写渐变文本?)

本文介绍了Java-如何在Java中编写渐变文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用Java编写渐变文本

如下:-https://i.stack.imgur.com/0qAKt.png

但我不知道该怎么做。

请帮帮我!

推荐答案

您将需要使用Graphics2D绘制,并根据LinearGradientPaint API

使用LinearGRadientPaint对象设置其Paint属性

例如:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.LinearGradientPaint;
import java.awt.Paint;
import java.awt.RenderingHints;
import javax.swing.*;

@SuppressWarnings("serial")
public class GradientText extends JPanel {
    private static final int PREF_W = 360;
    private static final int PREF_H = 200;
    private static final Font FONT = new Font(Font.DIALOG, Font.BOLD, 46);
    private static final String TEXT = "This is my text";
    private static final int COLOR_COUNT = 30;
    private static final Color BG = Color.BLACK;
    private Paint myPaint;

    public GradientText() {
        setBackground(BG);
        setPreferredSize(new Dimension(PREF_W, PREF_H));
        float[] fractions = new float[COLOR_COUNT];
        Color[] colors = new Color[COLOR_COUNT];
        for (int i = 0; i < colors.length; i++) {
            fractions[i] = ((float)i) / COLOR_COUNT;
            float hue = fractions[i];
            colors[i] = Color.getHSBColor(hue, 1f, 1f);
        }
        myPaint = new LinearGradientPaint(0, 0, PREF_W, 0, fractions, colors);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g2.setPaint(myPaint);
        g2.setFont(FONT);
        g2.drawString(TEXT, 20, 100);
    }

    private static void createAndShowGui() {
        GradientText mainPanel = new GradientText();

        JFrame frame = new JFrame("GradientText");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}

在此代码中,使用分成30个子部分的色相/饱和度/亮度颜色设置线性绘画对象

这篇关于Java-如何在Java中编写渐变文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Java-如何在Java中编写渐变文本?

基础教程推荐