java画图闪烁,学习Java ...为什么这张图片在闪烁/闪烁?

这篇博客讨论了一个Java程序中使用GIF图像时出现的闪烁问题。作者发现其中一个GIF在显示时底部会闪烁白色,而另一个则显示正常。经过检查,问题出在GIF文件本身,可能存在冗余帧导致闪烁。通过使用Photoshop修复了图片,并指出在某些情况下,即使图像在其他查看器中正常,也可能在Java中出现问题,可能是Java的bug。解决方案是替换有问题的GIF文件。
摘要由CSDN通过智能技术生成

There seem to be a lot of similar problems, but they implement different code...this is just the base code, so I have a feeling I'm doing something very strange/wrong. This is what my code looks like. The gifs are transparent and animated, if that changes things.

import java.awt.*;

import javax.swing.*;

public class NewClass extends JFrame {

private JLabel label;

private JButton button;

private JTextField textfield;

private ImageIcon image;

private JLabel label1;

private ImageIcon imaged;

private JLabel label2;

public NewClass (){

setLayout(new FlowLayout());

label = new JLabel("Hi, I am a label!");

add(label);

image = new ImageIcon(getClass().getResource("sprite15_2_1.gif"));

label1 = new JLabel(image);

add(label1);

imaged = new ImageIcon(getClass().getResource("sprite2_1_1.gif"));

label2 = new JLabel(imaged);

add(label2);

label1.setDoubleBuffered(true);

label2.setDoubleBuffered(true);

textfield = new JTextField(15);

add (textfield);

button = new JButton("aight");

add(button);

}

public static void main (String args[]){

NewClass gui = new NewClass();

gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

gui.pack();

gui.setVisible(true);

gui.setTitle("Elan Vital");

}

}

Edited to contain full code. It's just a simple program.

The Flashing GIF (sprite15_2_1.gif) :

6MXcd.gif

sprite_2_1_1.gif shows up perfectly, but the bottom half of sprite15_2_1.gif flickers white. I have no idea what's different I: " . I have double-checked the gif to be sure it's not a problem with the file itself...

解决方案

There were problems with the gif file indeed.

Use any one of the following image instead of the one you posted. Double buffering is not necessary.

The image has been corrected in photoshop. There were some redundant frames which may have been causing the flickering but the surprising fact is the image shows correctly on all image viewers but not in Java. So possibility of a java bug.

VcFNI.gif

3nqlH.gif

The first image is a bit faster, the second one is slower by 0.2s. Use any one.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值