Java线程:我想淡入淡出图像,但编译图像时不会显示
这是我的代码,请帮助并解释我做错了什么,非常感谢。 我也有点困惑线程是否我做了正确的方式Java线程:我想淡入淡出图像,但编译图像时不会显示,java,multithreading,image,swing,user-interface,Java,Multithreading,Image,Swing,User Interface,这是我的代码,请帮助并解释我做错了什么,非常感谢。 我也有点困惑线程是否我做了正确的方式 public class Fade extends JPanel implements Runnable { static Image image; private float alpha = 0f; static JFrame frame; public static void main(String[] args) throws IOException {
public class Fade extends JPanel implements Runnable {
static Image image;
private float alpha = 0f;
static JFrame frame;
public static void main(String[] args) throws IOException {
image = new ImageIcon(ImageIO.read(new File("gummybear.jpg")))
.getImage();
frame = new JFrame("fade frame");
frame.add(new Fade());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(image.getWidth(frame), image.getHeight(frame));
// set picture in the center of screen
frame.setLocationRelativeTo(null);
frame.setVisible(true);
ExecutorService executor = Executors.newFixedThreadPool(1);
Runnable fade = new Fade();
executor.execute(fade);
// executor.shutdown();
// while (!executor.isTerminated()) {
// }
// System.out.println("Finished fade in / fade out threads");
}
public void run() {
while (alpha < 1) {
try {
System.out.println(alpha);
alpha += 0.1f;
this.repaint();
Thread.sleep(100);
} catch (InterruptedException ex) {
Logger.getLogger(Fader.class.getName()).log(Level.SEVERE, null,
ex);
}
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
// SRC_OVER: If pixels in the source and the destination overlap, only
// the source
// pixels outside of the overlapping area are rendered. The pixels in
// the overlapping area are not changed.
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
alpha));
g2d.drawImage(image, 0, 0, null);
// Color c = new Color(255, 255, 255, alpha);
// g2d.setColor(c);
// g2d.fillRect(0, 0, image.getWidth(frame), image.getHeight(frame));
System.out.println("repaint");
}
}
公共类扩展JPanel实现可运行{
静态图像;
专用浮点alpha=0f;
静态JFrame;
公共静态void main(字符串[]args)引发IOException{
image=newImageIcon(ImageIO.read(新文件(“gummybear.jpg”))
.getImage();
帧=新帧(“淡入帧”);
frame.add(新淡入淡出());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(image.getWidth(frame)、image.getHeight(frame));
//将图片设置在屏幕中央
frame.setLocationRelativeTo(空);
frame.setVisible(true);
ExecutorService executor=Executors.newFixedThreadPool(1);
可运行淡入淡出=新淡入淡出();
执行人。执行(淡入淡出);
//executor.shutdown();
//而(!executor.isTerminated()){
// }
//System.out.println(“完成的淡入/淡出线程”);
}
公开募捐{
而(α<1){
试一试{
系统输出打印项次(α);
α+=0.1f;
这个。重新绘制();
睡眠(100);
}捕获(中断异常例外){
Logger.getLogger(Fader.class.getName()).log(Level.SEVERE,null,
ex);
}
}
}
@凌驾
公共组件(图形g){
超级组件(g);
Graphics2D g2d=(Graphics2D)g;
//SRC_OVER:如果源和目标中的像素重叠,则仅
//源头
//将渲染重叠区域之外的像素。中的像素
//重叠区域不变。
g2d.setComposite(AlphaComposite.getInstance)(AlphaComposite.SRC_超过,
α);
g2d.drawImage(image,0,0,null);
//颜色c=新颜色(255、255、255、alpha);
//g2d.setColor(c);
//g2d.fillRect(0,0,image.getWidth(frame),image.getHeight(frame));
系统输出打印(“重绘”);
}
}
图像素材
在我看来,您希望为从JPG文件加载的图像添加透明度。JPG文件没有alpha通道(用于透明度),因此这将不起作用
您可以在其中一个Java教程中找到一个使用透明度(以及从JPG加载图像)的好例子:
在这里,您可以找到一个很好的示例,其中的代码几乎实现了您想要的功能:更改从JPG文件加载的图像的透明度。不同之处在于,不透明度值取自滑块控件,而不是定时变量
螺纹材料
编辑:我刚刚意识到,您使用的是Swing,这是一种不是为线程安全而设计的API
我只需要向您指出Java教程,然后。问题是您没有更改
alpha
值。至少,不是您正在显示的Fade
实例的alpha
值:
// Here you are adding a "Fade" instance to the frame.
frame.add(new Fade());
...
// Here you are creating a NEW "Fade" instance. Only in
// this instance, the alpha value will be affected
Runnable fade = new Fade();
executor.execute(fade);
将此更改为
// Create a Fade instance and add it to the frame
Fade fade = new Fade();
frame.add(fade);
...
// Submit the SAME Fade instance to the executor
executor.execute(fade);
您还必须验证alpha
值是否保留在[0,1]中,但这可以通过以下方法完成
alpha += 0.1f;
alpha = Math.min(1.0f, alpha);
JPG不支持透明性,Swing不是线程安全的,这两个事实都与这个问题没有任何关系。“我对线程是否正确有点困惑。”不。在调用
repain()
等方法时,最好使用Swing计时器。这将遵守EDT(事件调度线程)规则,即GUI更新应在EDT上执行。一般提示:1)为了更快地获得更好的帮助,发布一个(最简单的完整且可验证的示例)。2) 例如,获取图像的一种方法是热链接到中看到的图像。