Java Code for Moving Text | Applet | Thread

Before reading this article you need to know the Java Applet Basics and Java.lang.Thread class in Java .

In this article we shall be explaining the code to display moving text in Java. To print the string in the window we will use the drawString() method from java.awt package. The drawString method takes three arguments :

drawString(string, x, y)

  1. string : This parameter takes the string to be displayed.
  2. x : This parameters takes the x co-ordinates where the string will be displayedon the screen.
  3. y : This parameter takes the y co-ordinates where the string will be displayed
    on the screen.

We will be printing the string at (x, y) co-ordinates and then update the x co-ordinate and then repaint the screen again.

filter_none

edit
close

play_arrow

link
brightness_4
code

/*<APPLET code = "GFG.class" width = 500 height = 500 >
  </APPLET>
 */
  
// Java Code to implement Moving text using
// applet and thread.
  
import java.awt.*;
import java.applet.*;
  
public class GFG extends Applet implements Runnable {
    private String display;
    private int x, y, flag;
    Thread t;
  
    // initializing
    // called when the applet is
    // started.
    public void init()
    {
        display = "GeeksforGeeks";
        x = 100;
        y = 100;
        flag = 1;
  
        // creating thread
        t = new Thread(this, "MyThread");
  
        // start thread
        t.start();
    }
  
    // update the x co-ordinate
    public void update()
    {
  
        x = x + 10 * flag;
        if (x > 300)
            flag = -1;
        if (x < 100)
            flag = 1;
    }
  
    // run
    public void run()
    {
  
        while (true) {
  
            // Repainting the screen
            // calls the paint function
            repaint();
  
            update();
            try {
  
                // creating a pause of 1 second
                // so that the movement is recognizable
                Thread.sleep(1000);
            }
            catch (InterruptedException ie) {
                System.out.println(ie);
            }
        }
    }
  
    // drawString
    public void paint(Graphics g)
    {
        g.drawString(display, x, y);
    }
}

chevron_right


Output :

Note: The above function are a part of java.awt package and belongs to java.awt.Graphics class. Also, these codes might not run in an online compiler please use an offline compiler. The x and y coordinates can be changed by the programmer according to their need.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.