java - java中的多彩多变的文本

标签 java arrays list graphics colors

我喜欢五彩变化的文本,我制作了一个包含所有颜色的列表

我有 5 g.drawString();正在运行的函数,每个函数都应该是列表中的下一个颜色(一个在另一个之上。)

private Color[] colors = new Color[12];

然后在我的构造函数中:

colors[0] = new Color(255, 0, 0);
colors[1] = new Color(255, 127, 0);
colors[2] = new Color(255, 255, 0);
colors[3] = new Color(127, 255, 0);
colors[4] = new Color(0, 255, 0);
colors[5] = new Color(0, 255, 127);
colors[6] = new Color(0, 255, 255);
colors[7] = new Color(0, 127, 255);
colors[8] = new Color(0, 0, 255);
colors[9] = new Color(127, 0, 255);
colors[10] = new Color(255, 0, 255);
colors[11] = new Color(255, 0, 127);

如何使每个字母具有不同的颜色?

Set The Color: g.setColor(Color object); 
Example: g.setColor(colors[5]);

Write Text: g.drawString(String, x, y);
Example: g.drawString("S", 200, 300);

所以,我希望 S 是颜色,colors[0],我做了一个表格如下:

Starting | First | Second | Fifth

S -- 0      11       10       7
N -- 1       0       11       8
A -- 2       1        0       9
K -- 3       2        1      10
E -- 4       3        2      11

所以它会循环遍历每种颜色:

我尝试为此创建一个函数,但我删除了代码,因为我是个白痴 -_-

在我的主类中,我有一个调用勾选和渲染方法的游戏循环,先勾选然后渲染。

我有一个名为 STATE 的枚举,其中包含菜单和游戏,然后状态类型的变量 gameState 设置为 STATE.menu

public enum state {
    Menu,
    Game,
}

public state gameState = state.Menu;

当 gameState 等于 STATE.menu 时,它将调用 menu.render(g ( <-- 用于 Graphics 的变量 im));

每个类都有自己的渲染和勾选方法。 -Tick方法,用于设置变量等,if语句,yada yada yada -渲染方法,与绘制像素有关的任何内容

因为tick方法每0.0000000000000000001秒调用一次,所以颜色每百万分之九秒就会改变一次,看起来非常呆板。

所以我需要某种可以用变量配置的计时器

我希望列表中的每个字母都具有不同的颜色 如果您不明白,可以引用上表,但作为示例,

the letter a should be colors[0]
and then b, colors[1]
and c, colors[2]

颜色应该改变,

so a would be colors[2]
so b would be colors[0]
so c would be colors[1]

我可能有1001件事不清楚,所以请在评论中骂我^-^

感谢您的阅读!

最佳答案

如果我理解正确的话,主要有两个问题:

  • 用不同的颜色绘制字母,循环遍历给定的数组
  • 更新颜色(但在固定时间,而不是每次“滴答”时更新)

可以通过引入“colorOffset”来实现颜色循环。您可以将此颜色偏移量添加到用于访问数组中颜色的索引,并对该数组长度取模以获得有效的数组索引:

int colorOffset = ... // Counted up or down all the time

// The index of the color for the i'th letter
int colorIndex = (i+colorOffset)%colors.length;
if (colorIndex < 0) colorIndex += colors.length;
g.setColor(colors[colorIndex]);

第二部分,关于更新:我假设您有一个在自己的线程中运行的游戏循环。然后,在 thisTickMethodThatYouHaveBeenTalkingAbout 中,您可以使用 System.nanoTime() 检查当前系统时间,并计算自上次更新以来耗时。如果时间大于所需的间隔,您可以通过增加 colorOffset 并触发 repaint() 来执行更新(如有必要 - 您可能已经用您的render() 方法)。

Cycling colors

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class MulticolorTextAnimation
{
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                createAndShowGUI();
            }
        });
    }

    private static void createAndShowGUI()
    {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        MulticolorTextAnimationPanel m = new MulticolorTextAnimationPanel();
        f.getContentPane().add(m);

        Thread thread = new Thread(new Runnable()
        {
            @Override
            public void run()
            {
                while (true)
                {
                    m.thisTickMethodThatYouHaveBeenTalkingAbout();
                    try
                    {
                        Thread.sleep(1);
                    }
                    catch (InterruptedException e)
                    {
                        Thread.currentThread().interrupt();
                        return;
                    }
                }
            }
        });
        thread.setDaemon(true);
        thread.start();

        f.setSize(500,200);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }
}


class MulticolorTextAnimationPanel extends JPanel
{
    private String string;
    private Color colors[];
    private int colorOffset = 0;
    private long lastUpdateNs = -1;
    private final long updateIntervalMs = 250;

    public MulticolorTextAnimationPanel()
    {
        setFont(new Font("Dialog", Font.BOLD, 45));

        string = "I am a string!";

        colors = new Color[12];
        colors[0] = new Color(255, 0, 0);
        colors[1] = new Color(255, 127, 0);
        colors[2] = new Color(255, 255, 0);
        colors[3] = new Color(127, 255, 0);
        colors[4] = new Color(0, 255, 0);
        colors[5] = new Color(0, 255, 127);
        colors[6] = new Color(0, 255, 255);
        colors[7] = new Color(0, 127, 255);
        colors[8] = new Color(0, 0, 255);
        colors[9] = new Color(127, 0, 255);
        colors[10] = new Color(255, 0, 255);
        colors[11] = new Color(255, 0, 127);
    }

    public void thisTickMethodThatYouHaveBeenTalkingAbout()
    {
        long ns = System.nanoTime();
        if (lastUpdateNs < 0)
        {
            lastUpdateNs = ns;
        }
        long passedNs = (ns - lastUpdateNs);
        long passedMs = passedNs / 1000000;
        if (passedMs > updateIntervalMs)
        {
            // Increase or decrease the color offset,
            // depending on whether the colors should
            // cycle forward or backward
            colorOffset--;
            repaint();
            lastUpdateNs = ns;
        }

    }

    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, getWidth(), getHeight());

        FontMetrics fontMetrics = g.getFontMetrics();

        int x = 100;
        int y = 100;
        for (int i=0; i<string.length(); i++)
        {
            char c = string.charAt(i);
            int colorIndex = (i+colorOffset)%colors.length;
            if (colorIndex < 0)
            {
                colorIndex += colors.length;
            }
            g.setColor(colors[colorIndex]);
            g.drawString(String.valueOf(c), x, y);
            x += fontMetrics.charWidth(c); 
        }


    }
}

关于java - java中的多彩多变的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31687628/

相关文章:

java - 将具有动态键的 json 对象映射到 java 对象

javascript - 如何在 angularJS 或 javascript 中将两个不同的数组值转换为字符串

c# - 将列表中的所有值复制/克隆到 C# 中的另一个列表

c# - .NET 列表最佳方法

java - NetBeans 6.7.1 中的 Unicode

java - 自定义微调器 : setSelection scrolling down

arrays - 是否有与 matlab ismember 函数等效的 fortran 函数?

c - 为什么我的数组在将其传递到另一个函数后仍保留垃圾值/段错误?

python - 在 Python 上比较两个列表

java - 应用程序在 KitKat 上崩溃