1

小程序播放动画 gif 文件:

public class Applet1 extends Applet {
    private Image m_image=null;
    public void init() {
        m_image=getImage(getDocumentBase(), "001.gif");
    }   
    public void paint(Graphics g) {
        g.drawImage(m_image,0,0,this); 
    }   
    public boolean imageUpdate( Image img, int flags, int x, int y, int w, int h ) {
        System.out.println("Image update: flags="+flags+" x="+x+" y="+y+" w="+w+" h="+h);
        repaint();
        return true;
    }
}

我需要在另一个程序中添加更新的图像:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.*;

public class HighlightExample {
    public static void main(String[] args) {
        JFrame f = new JFrame("Highlight example");
        final JTextPane textPane = new JTextPane();
        JPanel pane = new JPanel();
        pane.setLayout(new BorderLayout());
        final JTextField tf = new JTextField();
        pane.add(tf, "Center");
        f.getContentPane().add(pane, "South");
        f.getContentPane().add(new JScrollPane(textPane), "Center");

        textPane.setText("abсdefghijkl lmnop12345678");
        final WordSearcher searcher = new WordSearcher(textPane);

        tf.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                word = tf.getText().trim();
                int offset = searcher.search(word);
                if (offset != -1) {
                    try {
                        textPane.scrollRectToVisible(textPane.modelToView(offset));
                    } catch (BadLocationException e) {}
                }
            }
        });

        textPane.getDocument().addDocumentListener(new DocumentListener() {
            public void insertUpdate(DocumentEvent evt) {
                searcher.search(word);
            }
            public void removeUpdate(DocumentEvent evt) {
                searcher.search(word);
            }
            public void changedUpdate(DocumentEvent evt) {
            }
        });
        f.setSize(400, 400);
        f.setVisible(true);
    }
    public static String word;
}

class WordSearcher {
    protected JTextComponent comp;
    protected Highlighter.HighlightPainter painter;
    public WordSearcher(JTextComponent comp) {
        this.comp = comp;
        this.painter = new UnderlineHighlighter.UnderlineHighlightPainter(Color.red);
    }
    public int search(String word) {
        int firstOffset = -1;
        Highlighter highlighter = comp.getHighlighter();
        Highlighter.Highlight[] highlights = highlighter.getHighlights();
        for (int i = 0; i < highlights.length; i++) {
            Highlighter.Highlight h = highlights[i];
            if (h.getPainter() instanceof UnderlineHighlighter.UnderlineHighlightPainter) {
                highlighter.removeHighlight(h);
            }
        }
        if (word == null || word.equals("")) {
            return -1;
        }
        String content;
        try {
            Document d = comp.getDocument();
            content = d.getText(0, d.getLength()).toLowerCase();
        } catch (BadLocationException e) {
            return -1;
        }

        word = word.toLowerCase();
        int lastIndex = 0;
        int wordSize = word.length();

        while ((lastIndex = content.indexOf(word, lastIndex)) != -1) {
            int endIndex = lastIndex + wordSize;
            try {
                highlighter.addHighlight(lastIndex, endIndex, painter);
            } catch (BadLocationException e) {}
            if (firstOffset == -1) {
                firstOffset = lastIndex;
            }
            lastIndex = endIndex;
        }
        return firstOffset;
    }
}

class UnderlineHighlighter extends DefaultHighlighter{
    protected static final Highlighter.HighlightPainter sharedPainter = new UnderlineHighlightPainter(null);
    protected Highlighter.HighlightPainter painter;

    public static class UnderlineHighlightPainter extends LayeredHighlighter.LayerPainter {
        protected Color color; // The color for the underline
        public UnderlineHighlightPainter(Color c) {
            color = c;
        }
        public void paint(final Graphics g, int offs0, int offs1, Shape bounds, JTextComponent c) {
        }
        public Shape paintLayer(final Graphics g, int offs0, int offs1, Shape bounds, JTextComponent c, View view) {
            g.setColor(color == null ? c.getSelectionColor() : color);
            Rectangle alloc;
            try {
                Shape shape = view.modelToView(offs0,Position.Bias.Forward, offs1,Position.Bias.Backward, bounds);
                alloc = (shape instanceof Rectangle) ? (Rectangle) shape : shape.getBounds();
            } catch (BadLocationException e) {
                return null;
            }
            FontMetrics fm = c.getFontMetrics(c.getFont());
            int baseline = alloc.y + alloc.height - fm.getDescent() + 1;
            g.drawLine(alloc.x, baseline, alloc.x + alloc.width, baseline);

            Toolkit kit=Toolkit.getDefaultToolkit();
            Image im3 =kit.getImage("001.gif");
            g.drawImage(im3,alloc.x+15,alloc.y-6,null);
            return alloc;
        }
    }
}

在这个程序中总是只显示图像 im3 的第一帧。抱歉有很多代码,它是完全可编译的。

4

1 回答 1

2

似乎问题在于您在绘制图像时传递nullImageObserver(接近代码末尾):

g.drawImage(im3,alloc.x+15,alloc.y-6,null);

因此,当新帧准备好时,图像将触发更新,但该更新会被忽略,因为没有侦听器。您可能希望c作为 the传递,ImageObserver以便在图像更改时重新绘制自己。

编辑:

这种做法将c完全。如果你想聪明一点,你可以创建自己的ImageObserver,记住c绘制图标的确切矩形,并在收到更新通知时重新绘制该矩形。

于 2012-04-25T21:52:34.550 回答