java+文字颜色上下不同_Java位图字体:使用不同颜色的1位图像

您可以将每个位图转换为Shape(或其中许多)并绘制Shape.获取Shape的过程见

Smoothing a jagged path.

例如.

import java.awt.*;

import java.awt.event.*;

import java.awt.image.*;

import java.awt.geom.*;

import javax.swing.*;

import javax.swing.border.*;

import javax.swing.event.*;

import java.util.Random;

/* Gain the outline of an image for further processing. */

class ImageShape {

private BufferedImage image;

private BufferedImage ImageShape;

private Area areaOutline = null;

private JLabel labelOutline;

private JLabel output;

private BufferedImage anim;

private Random random = new Random();

private int count = 0;

private long time = System.currentTimeMillis();

private String rate = "";

public ImageShape(BufferedImage image) {

this.image = image;

}

public void drawOutline() {

if (areaOutline!=null) {

Graphics2D g = ImageShape.createGraphics();

g.setColor(Color.WHITE);

g.fillRect(0,0,ImageShape.getWidth(),ImageShape.getHeight());

g.setColor(Color.RED);

g.setClip(areaOutline);

g.fillRect(0,0,ImageShape.getWidth(),ImageShape.getHeight());

g.setColor(Color.BLACK);

g.setClip(null);

g.draw(areaOutline);

g.dispose();

}

}

public Area getOutline(Color target, BufferedImage bi) {

// construct the GeneralPath

GeneralPath gp = new GeneralPath();

boolean cont = false;

int targetRGB = target.getRGB();

for (int xx=0; xx

for (int yy=0; yy

if (bi.getRGB(xx,yy)==targetRGB) {

if (cont) {

gp.lineTo(xx,yy);

gp.lineTo(xx,yy+1);

gp.lineTo(xx+1,yy+1);

gp.lineTo(xx+1,yy);

gp.lineTo(xx,yy);

} else {

gp.moveTo(xx,yy);

}

cont = true;

} else {

cont = false;

}

}

cont = false;

}

gp.closePath();

// construct the Area from the GP & return it

return new Area(gp);

}

public JPanel getGui() {

JPanel images = new JPanel(new GridLayout(1,2,2,2));

JPanel gui = new JPanel(new BorderLayout(3,3));

JPanel originalImage = new JPanel(new BorderLayout(2,2));

final JLabel originalLabel = new JLabel(new ImageIcon(image));

originalImage.add(originalLabel);

images.add(originalImage);

ImageShape = new BufferedImage(

image.getWidth(),

image.getHeight(),

BufferedImage.TYPE_INT_RGB

);

labelOutline = new JLabel(new ImageIcon(ImageShape));

images.add(labelOutline);

anim = new BufferedImage(

image.getWidth()*2,

image.getHeight()*2,

BufferedImage.TYPE_INT_RGB);

output = new JLabel(new ImageIcon(anim));

gui.add(output, BorderLayout.CENTER);

updateImages();

gui.add(images, BorderLayout.NORTH);

animate();

ActionListener al = new ActionListener() {

public void actionPerformed(ActionEvent ae) {

animate();

}

};

Timer timer = new Timer(1,al);

timer.start();

return gui;

}

private void updateImages() {

areaOutline = getOutline(Color.BLACK, image);

drawOutline();

}

private void animate() {

Graphics2D gr = anim.createGraphics();

gr.setColor(Color.BLUE);

gr.fillRect(0,0,anim.getWidth(),anim.getHeight());

count++;

if (count%100==0) {

long now = System.currentTimeMillis();

long duration = now-time;

double fraction = (double)duration/1000;

rate = "" + (double)100/fraction;

time = now;

}

gr.setColor(Color.WHITE);

gr.translate(0,0);

gr.drawString(rate, 20, 20);

int x = random.nextInt(image.getWidth());

int y = random.nextInt(image.getHeight());

gr.translate(x,y);

int r = 128+random.nextInt(127);

int g = 128+random.nextInt(127);

int b = 128+random.nextInt(127);

gr.setColor(new Color(r,g,b));

gr.draw(areaOutline);

gr.dispose();

output.repaint();

}

public static void main(String[] args) throws Exception {

int size = 150;

final BufferedImage outline = javax.imageio.ImageIO.read(new java.io.File("img.gif"));

ImageShape io = new ImageShape(outline);

JFrame f = new JFrame("Image Outline");

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.add(io.getGui());

f.pack();

f.setResizable(false);

f.setLocationByPlatform(true);

f.setVisible(true);

}

}

我必须指出蓝色图像左上角的FPS计数有十倍的误差. 50 FPS我可以相信,但500 FPS似乎……错了.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值