java graphics2d动画gif更新

时间:2012-04-25 21:42:54

标签: java swing graphics animated-gif

小程序播放动画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的第一帧。很抱歉很多代码,它完全可以填写。

1 个答案:

答案 0 :(得分:2)

问题是,在绘制图片时(在代码末尾附近),您将null作为ImageObserver传递:

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

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

修改

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