java 进程 sleep_java – 程序在Thread.sleep()和Timer期间冻结

原始问题:

该方法应该将在JFrame上显示的图像逐渐变为另一图像.但是,如果没有某种方法可以减慢速度,它似乎只会从一个图像变为新图像.为了减慢速度,我输入了一个Thread.sleep(1000),这样就不会立即发生变化.但是,有了这一行,我的程序完全冻结了.没有错误消息,没有任何消息.有人可以帮帮我吗?建议一种更好的方法来减慢速度,或者如何解决这个问题.

澄清:int k是变化中渐进步骤的数量. k = 1将是瞬间变化.任何更大的东西都是渐进的变化. int l同时控制每个图像显示的比例.

public void morphImg(int width, int height, BufferedImage morphImage, int k) {

//creates new image from two images of same size

BufferedImage image2 = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

for (int i = 0; i < width; i++) {

for (int j = 0; j < height; j++) {

//get color from original image

Color c = new Color(image.getRGB(i, j));

//get colors from morph image

Color c2 = new Color(morphImage.getRGB(i, j));

for (int l = 1; l <= k; l++) {

//gets colors at different stages

int r = ((k-l)*c.getRed()/k) + (l*c2.getRed()/k);

int g = ((k-l)*c.getGreen()/k) + (l*c2.getGreen()/k);

int b = ((k-l)*c.getBlue()/k) + (l*c2.getBlue()/k);

Color newColor = new Color(r, g, b);

//set colors of new image to average of the two images

image2.setRGB(i, j, newColor.getRGB());

//display new image

try {

imageLabel.setIcon(new ImageIcon(image2));

Thread.sleep(1000);

}

catch (InterruptedException e){

System.out.println("Exception caught.");

}

}

}

}

//sets modified image as "original" for further manipulation

setImage(image2);

}

更新的代码:使用计时器也会导致程序冻结…我没有使用它吗?

public void morphImg(int width, int height, BufferedImage morphImage, int k) {

//creates new image from two images of same size

final BufferedImage image2 = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

for (int l = 1; l <= k; l++) {

for (int i = 0; i < width; i++) {

for (int j = 0; j < height; j++) {

//get color from original image

Color c = new Color(image.getRGB(i, j));

//get colors from morph image

Color c2 = new Color(morphImage.getRGB(i, j));

//gets colors at different stages

int r = ((k-l)*c.getRed()/k) + (l*c2.getRed()/k);

int g = ((k-l)*c.getGreen()/k) + (l*c2.getGreen()/k);

int b = ((k-l)*c.getBlue()/k) + (l*c2.getBlue()/k);

Color newColor = new Color(r, g, b);

//set colors of new image to average of the two images

image2.setRGB(i, j, newColor.getRGB());

//display new image

imageLabel.setIcon(new ImageIcon(image2));

final Timer t = new Timer(500,null);

t.setInitialDelay(500);

t.start();

}

}

}

//sets modified image as "original" for further manipulation

setImage(image2);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值