Dmitriy
Dmitriy

Reputation: 161

java ImageObserver stop updating an animated gif

textPane contains text and animated gif images. imageUpdate used to update each new gif frame. When I delete a image from textPane, imageupdate continues to update it. How can I stop it? How to make imageupdate updated only image that got a new frame rather than the entire textPane? imageupdate always displays the x = 0 and y = 0, although the images are in the other coordinates and I can not get a specific image rectangle.
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);
    }
}

Upvotes: 4

Views: 1530

Answers (1)

stryba
stryba

Reputation: 2028

  1. load the images only once not every time you repaint
  2. for each image hold a list where you drew it during the last paint
  3. in imageUpdate use the given img to select the list of coordinates you populated in paintComponent for that very image and only repaint regions for that coordinates

Note: images not currently used in your text will still call imageUpdate but are automatically skipped since their list of coordinates should be empty

Upvotes: 2

Related Questions