textPane 包含文本和动画 gif 图像。imageUpdate 用于更新每个新的 gif 帧。当我从 textPane 中删除图像时,imageupdate 会继续更新它。我怎样才能阻止它?如何使 imageupdate 仅更新获得新框架而不是整个 textPane 的图像?imageupdate 始终显示 x = 0 和 y = 0,尽管图像位于其他坐标中并且我无法获得特定的图像矩形。
图片 001.gif http://plasmon.rghost.ru/37834058.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> пїЅdefghijkl bпїЅlmnop12345678<span style=\"font-size: 0px;color: white;\">"+c+"000</span> ;)<br><br><br><br><br><br><br><br>");
f.setSize(400, 400);
f.setVisible(true);
}
}