繁体   English   中英

Java中的多彩多姿的更改文本

[英]Multicolored changing text in java

我喜欢多变的文字,我列出了所有颜色

我有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

因此它会循环通过每种颜色:

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

在我的主类中,我有一个游戏循环,该循环调用tick和render方法,先进行tick然后进行渲染。

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

public enum state {
    Menu,
    Game,
}

public state gameState = state.Menu;

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

每个类都有自己的render和tick方法。 -Tick方法,用于设置变量等,如果语句,yada yada yada -Render方法,与绘制像素有关

由于滴答方法每0.0000000000000000001秒被调用一次,因此颜色每9百万分之一秒便会更改一次,并且看起来非常皮毛。

所以我需要一个可以用变量配置的计时器

我希望每个字母都使用不同的颜色,如果您不理解,可以在列表中一个接一个地参考上表,不过,举例来说,

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()方法覆盖了此时间)。

单车颜色

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


    }
}

暂无
暂无

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

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