Java - 多彩色文本 [英] Java - Multi-colored text

查看:246
本文介绍了Java - 多彩色文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一些文字,但每个字符都有更多的颜色。这里是一个例子,
请忽略o的鸡蛋,这些只是我的标志的一部分。第一个图像用Photoshop创建,第二个用Word创建。

I would like to have some text but have each character have more colors. Here is an example, please ignore the eggs for o's, those are just part of my logo. The first image was created with Photoshop and the second with Word.

但而不是显示,将显示:

But instead of that showing, this is shown instead:

这是有意义的,因为它是一种字体,字体没有任何颜色,但我想知道是否有一种方法使文本的颜色和阴影不同的像'N'出现在TYCOON中。这将显示在JLabel中。

This does make sense because it is a font and the font has no color in it whatsoever but I want to know if there is a way to make the text colored and shaded differently like the 'N' appears in TYCOON. This would be displayed in a JLabel. Any help is appreciated!

推荐答案

首先,您需要为每个包含所需颜色的字符创建一个图像例如在PhotoShop或用于为您的游戏创建图像的任何其他应用程序)。

First, you need to create an image for each character containing the (fancy) color you need (e.g. in PhotoShop or whatever other application you use to create images for your game).

接下来,您需要使用这些图像来绘制文本,而不是使用常规字体和 JLabel 。所以这需要(稍微)更多的工作...

Next, you need to use these images to draw the texts, instead of using regular fonts and the JLabel. So that requires (slightly) more work...

由于你没有提供关于你使用的Java组件/库的任何细节,我只能提供一些伪代码作为示例:

As you do not provide any details on the Java components/libraries you use, I can only provide some pseudo-code as an example:

String text = "My colorful text";
int x = START_X; //< x-coordinate of next char
int y = START_y; //< y-coordinate of next char

for (int i = 0; i < text.length(); i++) {
  char c = text.charAt(i));
  switch(c) {
    case ' ': //Space, increase x-coordinate
      x += SPACE_WIDTH;
      break;
     case '\n': // New line, reset x-coordinate and increase y-coordinate
       x = START_X;
       y += LINE_HEIGHT;
     default: // Draw character at x, y and increase x-coordinate
       charImage = getImage(c);
       drawCharacter(x, y, charImage);
       x += charImage.getWidth();
   }
}

此(伪代码)示例假设您

This (pseudo-code) example assumes you have


  • 一个为给定字符找到正确图像的方法: getImage()

  • 一种绘制图像的方法: drawCharcter()

  • 的图片: getWidth()

  • a method that finds the correct image for a given character: getImage()
  • a method to draw the image: drawCharcter()
  • a method to find the width of an image: getWidth()

检查/ case -statements以处理可能遇到的其他特殊字符。
但是这个例子应该帮助你设置代码来解决你的问题。

Furthermore, you probably need some extra checks/case-statements to handle other special characters you might encounter. But the example should help you setup the code to tackle your problem.

这篇关于Java - 多彩色文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆