Showing posts with label Java Swing Moving String. Show all posts
Showing posts with label Java Swing Moving String. Show all posts

Wednesday, June 12, 2024

Moving String with Random Color in Java Swing

// Moving String in Swing

import java.awt.*;

import javax.swing.*;

import java.util.*;


public class demo3 extends JFrame

{

JFrame f;

JPanel p;

public demo3()

{

f=new JFrame();


p=new JPanel()

{

int x=60;

int y=60;

int flag=1;


public void paint(Graphics g1)

{

Random r1=new Random();

Float r=r1.nextFloat();

Float g=r1.nextFloat();

Float b=r1.nextFloat();

Color c=new Color(r,g,b);

g1.setColor(c);

Font f1=new Font("Lucida Sans",Font.BOLD,30);

g1.setFont(f1);

super.paintComponent(g1);

g1.drawString("Sidhdhapura Amit's Blog",x,y);

x=x+10*flag;

if(x>800)

{

flag=-1;

}


if(x<100)

{

flag=1;

}


try

{

Thread.sleep(100);

}

catch(Exception e)

{

}

repaint();

}

};

f.add(p);

f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);

f.setSize(800,500);

f.setVisible(true);

}

public static void main(String args[])

{

new demo3();

}

}