java中的多色变化文本

java中的多色变化文本,java,arrays,list,graphics,colors,Java,Arrays,List,Graphics,Colors,我喜欢五颜六色的文字,我列出了所有的颜色 我有5克的抽绳();函数运行时,每个函数都应该是列表中的下一个颜色(一个在另一个之上) 然后在我的构造函数中: 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

我喜欢五颜六色的文字,我列出了所有的颜色

我有5克的抽绳();函数运行时,每个函数都应该是列表中的下一个颜色(一个在另一个之上)

然后在我的构造函数中:

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是颜色,颜色[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
所以它会在每种颜色之间循环:

我试着做一个函数,我删除了代码,因为我是个白痴-_-

在我的主类中,我有一个游戏循环,它调用tick和render方法,tick first然后render

我有一个名为STATE的枚举,其中包含menu和game,然后STATE类型的变量gameState被设置为STATE.menu

public enum state {
    Menu,
    Game,
}

public state gameState = state.Menu;

当gameState等于STATE.menu时,它将调用menu.render(g)(如果我理解正确,主要有两个问题:

  • 在给定数组中循环绘制不同颜色的字母
  • 更新颜色(但在固定时间,而不是每个“勾号”)
通过引入“颜色偏移”可以实现颜色循环。您可以将此颜色偏移添加到用于访问数组中颜色的索引中,并将此值与数组长度进行模运算,以获得有效的数组索引:

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]);

到目前为止做得很好。你的问题是什么?这只是你所做工作的列表。你能让你的问题更简洁吗?一个问题中似乎有多个问题。“因为勾号方法每0.0000000000001秒调用一次”-哇,你不喜欢你的用户…@MadProgrammer嘿,现在,1/5百万分之一秒是一个完全合理的更新时间:PI不理解这个问题?我如何才能让它向右,而不是向左?你基本上必须减少
颜色偏移量
…并确保在模运算后索引是正的。(后者是我无论如何都会添加的东西,因为否则,在运行几年之后,偏移量可能会溢出…)。代码已经相应地更新了。Ohhhmagod它看起来棒极了!
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]
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]);
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); 
        }


    }
}