2012-04-25 58 views
1

applet的播放GIF动画文件:的java的Graphics2D 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。对不起,有很多代码,它是完全可计算的。

回答

2

好像问题是要传递null作为ImageObserver绘制图像(靠近你的代码的结束)时:

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

所以,图像将触发一个更新,当一个新的帧已准备就绪,但由于没有听众,该更新被忽略。您可能想要通过c作为ImageObserver,以便在图像更改时重新绘制自身。

编辑:

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

+0

非常感谢,它的工作 – Dmitriy 2012-04-25 23:59:15

+0

您可以举一个例子来创建自己的ImageObserver及其应用程序吗? – Dmitriy 2012-04-26 00:05:24