java ImageObserver停止更新动画gif

时间:2012-04-29 20:25:00

标签: java graphics2d animated-gif

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 个答案:

答案 0 :(得分:2)

  1. 每次重新加载时都不会加载一次图像
  2. 每个图片的
  3. 都会显示您在上一个paint
  4. 期间绘制的列表
  5. imageUpdate中使用给定的img选择您在paintComponent中为该图片填充的坐标列表,并仅为该坐标重新绘制区域
  6. 注意:文字中当前未使用的图片仍会调用imageUpdate,但会自动跳过,因为他们的坐标列表应为空