【问题标题】:Multicolored changing text in javajava中的多色变化文本
【发布时间】:2015-10-19 16:15:11
【问题描述】:

我喜欢五彩变化的文字,我列出了所有颜色的列表

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

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

因为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件事不清楚,所以请在cmets中对我大喊大叫^-^

感谢阅读!

【问题讨论】:

  • 到目前为止做得很好。你的问题是什么?这只是您所做工作的清单。
  • 你能把你的问题更简洁吗?一个问题似乎有多个问题。
  • “因为 tick 方法每 0.0000000000000000001 秒调用一次” - 哇,你不喜欢你的用户...
  • @MadProgrammer 嘿,现在,五分之一秒是一个完全合理的更新时间:P
  • 我不明白这个问题?

标签: java arrays list graphics colors


【解决方案1】:

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

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

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

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


    }
}

【讨论】:

  • 我怎样才能让它向右而不是向左?
  • 您基本上必须减少 colorOffset... 并确保在模运算后索引为正。 (后者是无论如何我都会添加的,因为否则,在运行几年后,偏移量可能会溢出......)。代码已相应更新。
  • OHHHHMAGOD 看起来棒极了!
猜你喜欢
  • 2014-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-17
相关资源
最近更新 更多