java ImageObserver停止更新动画gif

java ImageObserver停止更新动画gif,java,graphics2d,animated-gif,Java,Graphics2d,Animated Gif,文本窗格包含文本和动画gif图像。imageUpdate用于更新每个新的gif帧。当我从文本窗格中删除图像时,imageupdate会继续更新它。我怎样才能阻止它? 如何使imageupdate仅更新获得新帧的图像,而不是更新整个文本窗格?imageupdate始终显示x=0和y=0,尽管图像位于其他坐标中,我无法获得特定的图像矩形。 Image 001.gif Image 000.gif 导入java.awt.event.ActionEvent; 导入java.awt.event.Action

文本窗格包含文本和动画gif图像。imageUpdate用于更新每个新的gif帧。当我从文本窗格中删除图像时,imageupdate会继续更新它。我怎样才能阻止它? 如何使imageupdate仅更新获得新帧的图像,而不是更新整个文本窗格?imageupdate始终显示x=0和y=0,尽管图像位于其他坐标中,我无法获得特定的图像矩形。
Image 001.gif
Image 000.gif

导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.awt.*;
导入javax.swing.text.html.HTMLEditorKit;
导入javax.swing.text.html.StyleSheet;
导入javax.swing.*;
导入java.awt.BorderLayout;
导入javax.swing.text.*;
公共类HighlightExample{
公共静态JTextPane textPane;
公共静态HTMLEditorKit=新的HTMLEditorKit();
公共静态字符c=(字符)(int)10022007;
公共静态void main(字符串[]args){
最终JTextField=新JTextField();
JFrame f=新JFrame(“突出显示示例”);
textPane=新的JTextPane(){
@凌驾
公共组件(图形g){
Graphics2D Graphics2D=(Graphics2D)g;
graphics2d.setRenderingHint(RenderingHints.KEY_抗锯齿,
RenderingHints.VALUE_ANTIALIAS_ON);
试一试{
文档d=(this).getDocument();
字符串内容=d.getText(0,d.getLength()).toLowerCase();
int lastIndex=0;
超级组件(g);
Image[]Image=newimage[]{Toolkit.getDefaultToolkit().getImage(“000.gif”),Toolkit.getDefaultToolkit().getImage(“001.gif”)};
而((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));
++最后指数;
}
}捕获(BadLocationException e){}
}
公共布尔图像更新(图像img、int标志、int x、int y、int w、int h)
{
System.out.println(“图像更新:“+img+”flags=“+flags+”x=“+x+”y=“+y+”w=“+w+”h=“+h”);
repaint();//重新绘制(x,y,w,h);
返回true;
}
};
tf.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件evt){
textPane.setText(tf.getText().trim());
}
});
textPane.setEditorKit(工具包);
StyleSheet StyleSheet=kit.getStyleSheet();
addRule(“sm{color:red;}”);
JPanel窗格=新的JPanel();
setLayout(新的BorderLayout());
窗格。添加(tf,“中间”);
f、 getContentPane().add(窗格“南”);
f、 getContentPane().add(新的JScrollPane(textPane),“中心”);
textPane.setEditable(false);
setContentType(“text/html”);
textPane.setText(“ab”+c+“001ПїЅdefghijkl bПїЅlmnop12345678”+c+“000;)








”; f、 设置大小(400400); f、 setVisible(真); } }
  • 仅加载一次图像,而不是每次重新绘制时
  • 为每个图像保留一个列表,其中列出了您在上一次绘制时绘制的图像
  • imageUpdate
    中,使用给定的
    img
    为该图像选择在
    paintComponent
    中填充的坐标列表,并仅为该坐标重新绘制区域
  • 注意:文本中当前未使用的图像仍将调用
    imageUpdate
    ,但会自动跳过,因为它们的坐标列表应为空

    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);
        }
    }