java彩色的世界,Java - 多彩色文本

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.

EVpuK.png

But instead of that showing, this is shown instead:

JW3IB.png

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!

解决方案

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).

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...

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

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()

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值