为什么显示.GIF图像会不断增加内存?

2024-02-28

我正在展示简单的代码示例。我在 a 中展示了一张 gif 图像Jlabel。运行程序时,任务管理器显示内存不断增加。为什么会发生这种情况?

Edited:

请尝试此代码...在显示玻璃按钮上,玻璃面板显示有 gif 图像,其中有一个隐藏玻璃按钮,并且内存将开始增加。单击隐藏玻璃按钮时,玻璃面板将被隐藏,并且内存增加将停止。

@mKorbel:我已经调试过它,构造函数将被调用一次,因此不需要重新初始化 JFrame 并且还包括: setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

public class circle extends JFrame {
public ImageIcon pic;
final JPanel glass;
public JButton glass_show;
public JButton hide_glass;

public circle() {
    super("Hi shamansdsdsd");
    setSize(500, 300);
    // Image icon initialize once :
    pic = new ImageIcon("images/loadinag.gif");
    glass_show = new JButton("Show Glass panel");
    this.add(glass_show);

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    glass = (JPanel) this.getGlassPane();
    hide_glass = new JButton("Hide Glass panel");
    glass.add(hide_glass);
    glass.add(new JLabel(pic));
    glass.setOpaque(false);

}
public void initialize_listeners(){
    glass_show.addActionListener(new ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent A) {
            glass.setVisible(true);
        }
    });

    hide_glass.addActionListener(new ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent A) {
            glass.setVisible(false);
        }
    });
}
 public static void main(String[] args) {
    circle mFrame = new circle();
    mFrame.initialize_listeners();
    mFrame.setVisible(true);
}

}


Java 中的 GIF 动画图像存在一个错误。其他图像不会增加内存。

Edit;

下面的例子运行时没有内存泄漏;但你需要Eclipse SWT 库 http://www.eclipse.org/downloads/download.php?file=/eclipse/downloads/drops/R-3.6.2-201102101200/swt-3.6.2-win32-win32-x86.zip from Eclipse 的网站 http://www.eclipse.org/downloads/download.php?file=/eclipse/downloads/drops/R-3.6.2-201102101200/swt-3.6.2-win32-win32-x86.zip

import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTException;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.ImageLoader;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;

public class GIFExample {
    static Display display;
    static Shell shell;
    static GC shellGC;
    static Color shellBackground;
    static ImageLoader loader;
    static ImageData[] imageDataArray;
    static Thread animateThread;
    static Image image;
    static final boolean useGIFBackground = false;

    public static void main(String[] args) {
        display = new Display();
        shell = new Shell(display);
        shell.setSize(300, 300);
        shell.open();
        shellGC = new GC(shell);
        shellBackground = shell.getBackground();

        FileDialog dialog = new FileDialog(shell);
        dialog.setFilterExtensions(new String[] {"*.gif"});
        String fileName = dialog.open();
        if (fileName != null) {
            loader = new ImageLoader();
            try {
                imageDataArray = loader.load(fileName);
                if (imageDataArray.length > 1) {
                    animateThread = new Thread("Animation") {
                        @Override
                        public void run() {
                            /* Create an off-screen image to draw on, and fill it with the shell background. */
                            Image offScreenImage = new Image(display, loader.logicalScreenWidth, loader.logicalScreenHeight);
                            GC offScreenImageGC = new GC(offScreenImage);
                            offScreenImageGC.setBackground(shellBackground);
                            offScreenImageGC.fillRectangle(0, 0, loader.logicalScreenWidth, loader.logicalScreenHeight);

                            try {
                                /* Create the first image and draw it on the off-screen image. */
                                int imageDataIndex = 0;  
                                ImageData imageData = imageDataArray[imageDataIndex];
                                if (image != null && !image.isDisposed()) image.dispose();
                                image = new Image(display, imageData);
                                offScreenImageGC.drawImage(
                                        image,
                                        0,
                                        0,
                                        imageData.width,
                                        imageData.height,
                                        imageData.x,
                                        imageData.y,
                                        imageData.width,
                                        imageData.height);

                                /* Now loop through the images, creating and drawing each one
                                 * on the off-screen image before drawing it on the shell. */
                                int repeatCount = loader.repeatCount;
                                while (loader.repeatCount == 0 || repeatCount > 0) {
                                    switch (imageData.disposalMethod) {
                                    case SWT.DM_FILL_BACKGROUND:
                                        /* Fill with the background color before drawing. */
                                        Color bgColor = null;
                                        if (useGIFBackground && loader.backgroundPixel != -1) {
                                            bgColor = new Color(display, imageData.palette.getRGB(loader.backgroundPixel));
                                        }
                                        offScreenImageGC.setBackground(bgColor != null ? bgColor : shellBackground);
                                        offScreenImageGC.fillRectangle(imageData.x, imageData.y, imageData.width, imageData.height);
                                        if (bgColor != null) bgColor.dispose();
                                        break;
                                    case SWT.DM_FILL_PREVIOUS:
                                        /* Restore the previous image before drawing. */
                                        offScreenImageGC.drawImage(
                                                image,
                                                0,
                                                0,
                                                imageData.width,
                                                imageData.height,
                                                imageData.x,
                                                imageData.y,
                                                imageData.width,
                                                imageData.height);
                                        break;
                                    }

                                    imageDataIndex = (imageDataIndex + 1) % imageDataArray.length;
                                    imageData = imageDataArray[imageDataIndex];
                                    image.dispose();
                                    image = new Image(display, imageData);
                                    offScreenImageGC.drawImage(
                                            image,
                                            0,
                                            0,
                                            imageData.width,
                                            imageData.height,
                                            imageData.x,
                                            imageData.y,
                                            imageData.width,
                                            imageData.height);

                                    /* Draw the off-screen image to the shell. */
                                    shellGC.drawImage(offScreenImage, 0, 0);

                                    /* Sleep for the specified delay time (adding commonly-used slow-down fudge factors). */
                                    try {
                                        int ms = imageData.delayTime * 10;
                                        if (ms < 20) ms += 30;
                                        if (ms < 30) ms += 10;
                                        Thread.sleep(ms);
                                    } catch (InterruptedException e) {
                                    }

                                    /* If we have just drawn the last image, decrement the repeat count and start again. */
                                    if (imageDataIndex == imageDataArray.length - 1) repeatCount--;
                                }
                            } catch (SWTException ex) {
                                System.out.println("There was an error animating the GIF");
                            } finally {
                                if (offScreenImage != null && !offScreenImage.isDisposed()) offScreenImage.dispose();
                                if (offScreenImageGC != null && !offScreenImageGC.isDisposed()) offScreenImageGC.dispose();
                                if (image != null && !image.isDisposed()) image.dispose();
                            }
                        }
                    };
                    animateThread.setDaemon(true);
                    animateThread.start();
                }
            } catch (SWTException ex) {
                System.out.println("There was an error loading the GIF");
            }
        }

        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) display.sleep();
        }
        display.dispose();
    }
}

代码来源 http://www.java2s.com/Code/Java/SWT-JFace-Eclipse/DisplayananimatedGIF.htm

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么显示.GIF图像会不断增加内存? 的相关文章

  • 有没有创建 Cron 表达式的 Java 代码? [关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我需要一个 Java 代码来根据用户输入创建一个 cron 表达式 用户输入是时间 频率和执行次数 只需从评论中添加 自己创建 即可
  • TreeMap 删除所有大于某个键的键

    在项目中 我需要删除键值大于某个键的所有对象 键类型为Date 如果重要的话 据我所知TreeMapJava中实现的是红黑树 它是一种二叉搜索树 所以我应该得到O n 删除子树时 但除了制作尾部视图并一一删除之外 我找不到任何方法可以做到这
  • eclipse行号状态行贡献项是如何实现的?

    我需要更新状态行编辑器特定的信息 我已经有了自己的实现 但我想看看 eclipse 贡献项是如何实现的 它显示状态行中的行号 列位置 谁能指点一下 哪里可以找到源代码 提前致谢 亚历克斯 G 我一直在研究它 它非常复杂 我不确定我是否了解完
  • 什么是抽象类? [复制]

    这个问题在这里已经有答案了 当我了解抽象类时 我说 WT H 问题 创建一个无法实例化的类有什么意义呢 为什么有人想要这样的课程 什么情况下需要抽象类 如果你明白我的意思 最常见的是用作基类或接口 某些语言有单独的interface构建 有
  • 如何调试“com.android.okhttp”

    在android kitkat中 URLConnection的实现已经被OkHttp取代 如何调试呢 OkHttp 位于此目录中 external okhttp android main java com squareup okhttp 当
  • 如何在 Java 中向时间戳添加/减去时区偏移量?

    我正在使用 JDK 8 并且玩过ZonedDateTime and Timestamp很多 但我仍然无法解决我面临的问题 假设我得到了格式化的Timestamp在格林威治标准时间 UTC 我的服务器位于某处 假设它设置为Asia Calcu
  • java inputstream 打印控制台内容

    sock new Socket www google com 80 out new BufferedOutputStream sock getOutputStream in new BufferedInputStream sock getI
  • 提供节点名或服务名,或未知 Java

    最近我尝试运行我的 Java 项目 每当我运行它并将其打开到我得到的服务器地址时 Unable to determine host name java net UnknownHostException Caused by java net
  • Mockito 使用 @Mock 时将 Null 值注入到 Spring bean 中?

    由于我是 Spring Test MVC 的新手 我不明白这个问题 我从以下代码中获取了http markchensblog blogspot in search label Spring http markchensblog blogsp
  • 如何在 Spring 中使 @PropertyResource 优先于任何其他 application.properties ?

    我正在尝试在类路径之外添加外部配置属性资源 它应该覆盖任何现有的属性 但以下方法不起作用 SpringBootApplication PropertySource d app properties public class MyClass
  • Akka 与现有 java 项目集成的示例

    如果我已经有现有的javaWeb 应用程序使用spring and servlet容器 将 Akka 集成到其中的正确方法是什么 就像我将会有Actor1 and Actor2互相沟通的 开始使用这些演员的切入点是什么 例如 1 把它放在那
  • 如何在.NET中使用java.util.zip.Deflater解压缩放气流?

    之后我有一个转储java util zip Deflater 可以确认它是有效的 因为 Java 的Inflater打开它很好 并且需要在 NET中打开它 byte content ReadSample sampleName var inp
  • 如何在JPanel中设置背景图片

    你好 我使用 JPanel 作为我的框架的容器 然后我真的想在我的面板中使用背景图片 我真的需要帮助 这是我到目前为止的代码 这是更新 请检查这里是我的代码 import java awt import javax swing import
  • 在 Java 中获取并存储子进程的输出

    我正在做一些需要我开始子处理 命令提示符 并在其上执行一些命令的事情 我需要从子进程获取输出并将其存储在文件或字符串中 这是我到目前为止所做的 但它不起作用 public static void main String args try R
  • 不可变的最终变量应该始终是静态的吗? [复制]

    这个问题在这里已经有答案了 在java中 如果一个变量是不可变的并且是final的 那么它应该是一个静态类变量吗 我问这个问题是因为每次类的实例使用它时创建一个新对象似乎很浪费 因为无论如何它总是相同的 Example 每次调用方法时都会创
  • Java Swing - 如何禁用 JPanel?

    我有一些JComponents on a JPanel我想在按下 开始 按钮时禁用所有这些组件 目前 我通过以下方式显式禁用所有组件 component1 setEnabled false 但是有什么办法可以一次性禁用所有组件吗 我尝试禁用
  • Log4j2 ThreadContext 映射不适用于parallelStream()

    我有以下示例代码 public class Test static System setProperty isThreadContextMapInheritable true private static final Logger LOGG
  • 抛出 Java 异常时是否会生成堆栈跟踪?

    这是假设我们不调用 printstacktrace 方法 只是抛出和捕获 我们正在考虑这样做是为了解决一些性能瓶颈 不 堆栈跟踪是在构造异常对象时生成的 而不是在抛出异常对象时生成的 Throwable 构造函数调用 fillInStack
  • Java 11 - 将 Spring @PostConstruct 替换为 afterPropertiesSet 或使用 initMethod

    我正在使用 spring 应用程序 有时会使用 PostConstruct用于代码和测试中的设置 看来注释将被排除在外Java 11 https www baeldung com spring postconstruct predestro
  • 由 Servlet 容器提供服务的 WebSocket

    上周我研究了 WebSockets 并对如何使用 Java Servlet API 实现服务器端进行了一些思考 我没有花费太多时间 但在使用 Tomcat 进行一些测试时遇到了以下问题 如果不修补容器或至少对 HttpServletResp

随机推荐