Java ImageObserver停止更新动画gif。

4

textPane包含文本和动画gif图像。使用imageUpdate更新每个新的gif帧。当我从textPane中删除一个图片时,imageupdate继续更新它。如何停止它?如何使imageupdate只更新得到新帧的图像而不是整个textPane?imageupdate始终显示x = 0和y = 0,尽管图像在其他坐标中,我无法获得特定图像矩形。
Image 001.gif http://plasmon.rghost.ru/37834058.image
Image 000.gif http://plasmon.rghost.ru/37834053.image

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.StyleSheet;
import javax.swing.*;
import java.awt.BorderLayout;
import javax.swing.text.*;

public class HighlightExample {
    public static JTextPane textPane;
    public static HTMLEditorKit kit = new HTMLEditorKit();
    public static char c = (char)(int)10022007;
    public static void main(String[] args) {
        final JTextField tf = new JTextField();
        JFrame f = new JFrame("Highlight example");
        textPane = new JTextPane(){
            @Override
            public void paintComponent(Graphics g) {
                Graphics2D graphics2d = (Graphics2D) g;
                graphics2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
                try {
                    Document d = (this).getDocument();
                    String content = d.getText(0, d.getLength()).toLowerCase();
                    int lastIndex = 0;
                    super.paintComponent(g);
                    Image[] image=new Image[] {Toolkit.getDefaultToolkit().getImage("000.gif"), Toolkit.getDefaultToolkit().getImage("001.gif")};
                    while ((lastIndex = content.indexOf(c, lastIndex)) != -1) {
                        g.drawImage(image[Integer.parseInt(content.substring(lastIndex+1, lastIndex+4))],(int)(this).modelToView(lastIndex).getX(),(int)(this).modelToView(lastIndex).getY(),this); 
                        ++lastIndex;
                    }

                } catch (BadLocationException e) {}
            }
            public boolean imageUpdate( Image img, int flags, int x, int y, int w, int h ) 
            {
                System.out.println("Image update:" + img + " flags="+flags+" x="+x+" y="+y+" w="+w+" h="+h);
                repaint(); //repaint(x, y, w, h);
                return true;
            }
        };
        tf.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                textPane.setText(tf.getText().trim());
            }
        });
        textPane.setEditorKit(kit);
        StyleSheet styleSheet = kit.getStyleSheet();
        styleSheet.addRule("sm {color: red;}");
        JPanel pane = new JPanel();
        pane.setLayout(new BorderLayout());
        pane.add(tf, "Center");
        f.getContentPane().add(pane, "South");
        f.getContentPane().add(new JScrollPane(textPane), "Center");
        textPane.setEditable(false);
        textPane.setContentType("text/html");
        textPane.setText("ab<span style=\"font-size: 0px;color: white;\">"+c+"001</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;пїЅdefghijkl bпїЅlmnop12345678<span style=\"font-size: 0px;color: white;\">"+c+"000</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;;)<br><br><br><br><br><br><br><br>");

        f.setSize(400, 400);
        f.setVisible(true);
    }
}
1个回答

2
  1. 仅在重绘时加载图片一次,而不是每次都加载
  2. 对于每个图像,保留一个列表,记录它在上次paint中绘制的位置
  3. imageUpdate中,使用给定的img来选择你在paintComponent中为该图像填充的坐标列表,并仅重新绘制该坐标区域。

注意:当前未在文本中使用的图像仍会调用imageUpdate,但由于它们的坐标列表应为空,因此会自动跳过。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接