Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7850743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:57:36+00:00 2026-06-02T18:57:36+00:00

The applet plays an animated gif file: public class Applet1 extends Applet { private

  • 0

The applet plays an animated gif file:

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;
    }
}

I need to add the updated image in another program:

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;
        }
    }
}

In this program always displays only the first frame of the image im3. Sorry for a lot of code, it is fully compillable.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T18:57:37+00:00Added an answer on June 2, 2026 at 6:57 pm

    It seems like the problem is that you are passing null as the ImageObserver when drawing the image (near the end of your code):

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

    So, the image will fire an update when a new frame is ready, but that update just gets ignored because there are no listeners. You probably want to pass c as the ImageObserver so that it will repaint itself when the image changes.

    Edit:

    This approach will c entirely. If you wanted to be clever, you could create your own ImageObserver that remembers the exact rectangle in c that the icon was painted in, and repaints just that rectangle when notified of an update.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

An applet's life cycle has four methods: - init() when it is created -
The first time a user plays Runescape (which is played via Java applet), the
The following code is used for playing a sound file in my java applet:
How can I use vlcj-1.2.0 to play a video file in an applet if
Is there any way to set an animated image like the Apple spinning gear
I want to display image in an applet and play audio clip. I wrote
I am trying to play audio in a Java applet but it is not
Do applet javascript calls serialize in any proper way? I'm considering implementing a model
My applet is getting unexpectedly terminated. From log, I could see JVM heartbeat ..
I have a sandboxed Applet, but that should not make a difference. The Applet

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.