简体   繁体   English

字体问题需要改变

[英]Font problems to change

Hi iam trying to do some program that allowed me to change my font color so if i have used a check box but the problem is the combination of the color . 嗨,iam尝试做一些允许我更改字体颜色的程序,因此如果我使用了复选框,但问题是颜色的组合。 can i combine two colors to make it the color of my font ? 我可以将两种颜色组合成字体的颜色吗?

Font font = new Font("Arial", Font.BOLD, 12);
                field.setFont(font);
                field.setForeground(Color.YELLOW);// can i do this Color.YELLOW+GREEN ?

Why not? 为什么不?

import java.awt.Color;
import java.awt.Font;
import javax.swing.*;        

public class Example {
    private static void createAndShowGUI() {
        JFrame frame = new JFrame("HelloColorfulWorld");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Font font = new Font("Arial", Font.BOLD, 12);

        Color yellowColor = new Color(255, 255, 0); // Yellow  
        Color greenColor = new Color(0, 255, 0); // Green

        Color mixColor = mixTwoColors(yellowColor, greenColor); // Yellow + Green

        JLabel label = new JLabel("Hello, Colorful World!");

        label.setFont(font);
        label.setForeground(mixColor);
        frame.getContentPane().add(label);

        frame.pack();
        frame.setVisible(true);
    }

    public static Color mixTwoColors(Color color1, Color color2) {
        double alpha = color1.getAlpha() + color2.getAlpha();

        double weight1 = color1.getAlpha() / alpha;
        double weight2 = color2.getAlpha() / alpha;

        double r = weight1 * color1.getRed() + weight2 * color2.getRed();
        double g = weight1 * color1.getGreen() + weight2 * color2.getGreen();
        double b = weight1 * color1.getBlue() + weight2 * color2.getBlue();

        double a = Math.max(color1.getAlpha(), color2.getAlpha());

        return new Color((int) r, (int) g, (int) b, (int) a);
    }    

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

在此处输入图片说明

More information about operations with colors you can find there: 您可以在此处找到有关颜色操作的更多信息:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM