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 9009999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:20:41+00:00 2026-06-16T02:20:41+00:00

i’m working on applet that displays a moving banner horizontally and when this text

  • 0

i’m working on applet that displays a moving banner horizontally and when this text banner
reaches the right boundary of the applet window it should be appear reversed from the start of the left boundary, i write the following class to do the work, the problem is that when the text banner reaches the right banner it crashes, the applet goes to infinite loop:

    import java.applet.*;
    import java.awt.*;
    import java.net.*;
    import java.io.*;
    import javax.swing.*;

    /*
    <applet code="banner" width=300 height=50>
    </applet>
    */
    public class TextBanner extends Applet implements Runnable 
    {
        String msg = "Islam Hamdy", temp="";
        Thread t = null;
        int state;
        boolean stopFlag;
        int x;
        String[] revMsg;
        int msgWidth;
        boolean isReached;

         public void init() 
        {

            setBackground(Color.BLACK);
            setForeground(Color.YELLOW);
        }

        // Start thread

        public void start() 
        {

            t = new Thread(this);
            stopFlag = false;
            t.start();
        }

        // Entry point for the thread that runs the banner.

        public void run() 
        {

            // Display banner
            while(true) 
            {
                try 
                {
                    repaint();
                    Thread.sleep(550);
                    if(stopFlag)
                        break;
                } catch(InterruptedException e) {}
            }
        }

        // Pause the banner.

        public void stop() 
        {
            stopFlag = true;
            t = null;
        }

        // Display the banner.

        public void paint(Graphics g) 
        {
        String temp2="";
        System.out.println("Temp-->"+temp);
        int result=x+msgWidth; 
        FontMetrics fm = g.getFontMetrics();
        msgWidth=fm.stringWidth(msg);
        g.setFont(new Font("ALGERIAN", Font.PLAIN, 30));        
        g.drawString(msg, x, 40);
        x+=10;
        if(x>bounds().width){
        x=0;
    }
        if(result+130>bounds().width){
         x=0;
        while((x<=bounds().width)){
        for(int i=msg.length()-1;i>0;i--){
        temp2=Character.toString(msg.charAt(i));
        temp=temp2+temp;
                    // it crashes here
        System.out.println("Before draw");
            g.drawString(temp, x, 40);
        System.out.println("After draw");
        repaint();
               }
            x++;
            }       // end while
         }          //end if

      }

     }
  • 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-16T02:20:43+00:00Added an answer on June 16, 2026 at 2:20 am

    Let’s start with…

    • Use Swing over AWT components (JApplet instead of Applet)
    • Don’t override the paint methods of top level containers. There are lots of reasons, the major one that is going to effect you is top level containers are not double buffered.
    • DO NOT, EVER, update the UI from any thread other the Event Dispatching Thread, in fact, for what you are trying to do, a Thread is simply over kill.
    • DO NOT update animation states with the paint method. You’ve tried to perform ALL you animation within the paint method, this is not how paint works. Think of paint as a frame in film, it is up to (in your case) the thread to determine what frame where up to, and in fact, it should be preparing what should painted.
    • You do not control the paint system. repaint is a “request” to the paint sub system to perform an update. The repaint manager will decide when the actual repaint will occur. This makes performing updates a little tricky…

    Updated with Example

    enter image description hereenter image description here

    public class Reverse extends JApplet {
    
        // Set colors and initialize thread.
        public void init() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }
                    setBackground(Color.BLACK);
                    setForeground(Color.YELLOW);
                    setLayout(new BorderLayout());
                    add(new TextPane());
                }
            });
        }
    
        // Start thread
        public void start() {
        }
    
        // Pause the banner.
        public void stop() {
        }
    
        public class TextPane extends JPanel {
    
            int state;
            boolean stopFlag;
            char ch;
            int xPos;
            String masterMsg = "Islam Hamdy", temp = "";
            String msg = masterMsg;
            String revMsg;
            int msgWidth;
    
            private int direction = 10;
    
            public TextPane() {
                setOpaque(false);
                setBackground(Color.BLACK);
                setForeground(Color.YELLOW);
    
                setFont(new Font("ALGERIAN", Font.PLAIN, 30));
    
                // This only needs to be done one...
                StringBuilder sb = new StringBuilder(masterMsg.length());
                for (int index = 0; index < masterMsg.length(); index++) {
                    sb.append(masterMsg.charAt((masterMsg.length() - index) - 1));
                }
                revMsg = sb.toString();
    
                // Main animation engine.  This is responsible for making
                // the decisions on where the animation is up to and how
                // to react to the edge cases...
                Timer timer = new Timer(100, new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        xPos += direction;
                        FontMetrics fm = getFontMetrics(getFont());
                        if (xPos > getWidth()) { // this condition fires when the text banner reaches the right banner
                            direction *= -1;
                            msg = revMsg;
                        } else if (xPos < -fm.stringWidth(masterMsg)) {
                            direction *= -1;
                            msg = masterMsg;
                        }
                        repaint();
                    }
                });
                timer.setRepeats(true);
                timer.setCoalesce(true);
                timer.start();
    
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                System.out.println(xPos);
                FontMetrics fm = g.getFontMetrics();
                msgWidth = fm.stringWidth(msg);
                g.drawString(msg, xPos, 40);
            }
        }
    }
    

    Updated with additional example

    Now, if you want to be a little extra clever…you could take advantage of a negative scaling process, which will reverse the graphics for you…

    Updated timer…

                Timer timer = new Timer(100, new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        xPos += direction;
                        FontMetrics fm = getFontMetrics(getFont());
                        System.out.println(xPos + "; " + scale);
                        if (scale > 0 && xPos > getWidth()) { // this condition fires when the text banner reaches the right banner
                            xPos = -(getWidth() + fm.stringWidth(msg));
                            scale = -1;
                        } else if (scale < 0 && xPos >= 0) {
                            xPos = -fm.stringWidth(msg);
                            scale = 1;
                        }
                        repaint();
                    }
                });
    

    And the updated paint method…

            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g.create();
                g2d.scale(scale, 1);
                FontMetrics fm = g2d.getFontMetrics();
                msgWidth = fm.stringWidth(msg);
                g2d.drawString(msg, xPos, 40);
                g2d.dispose();
            }
    

    Updated with “bouncing”…

    This replaces the TextPane from the previous example

    As the text moves beyond the right boundary, it will “reverse” direction and move back to the left, until it passes beyond that boundary, where it will “reverse” again…

    public class TextPane    public class TextPane extends JPanel {
    
        int state;
        boolean stopFlag;
        char ch;
        int xPos;
        String msg = "Islam Hamdy";
        int msgWidth;
    
        private int direction = 10;
    
        public TextPane() {
            setBackground(Color.BLACK);
            setForeground(Color.YELLOW);
    
            setFont(new Font("ALGERIAN", Font.PLAIN, 30));
    
            Timer timer = new Timer(100, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    xPos += direction;
                    FontMetrics fm = getFontMetrics(getFont());
                    if (xPos > getWidth()) {
                        direction *= -1;
                    } else if (xPos < -fm.stringWidth(msg)) {
                        direction *= -1;
                    }
                    repaint();
                }
            });
            timer.setRepeats(true);
            timer.setCoalesce(true);
            timer.start();
    
        }
    
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            FontMetrics fm = g2d.getFontMetrics();
            msgWidth = fm.stringWidth(msg);
            g2d.drawString(msg, xPos, 40);
            g2d.dispose();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I know there's a lot of other questions out there that deal with this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace

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.