Multi Threading in Java | Circle Moving Java Code | Simple to Understand |

import java.awt.*;
import java.util.*;
import javax.swing.*;

public class ANImation  extends JFrame implements Runnable
{ 
	int x,y,stepx,stepy,width,end;
	int x1,y1;
	int x2,y2;
	Thread t1=new Thread(this); 
	ANImation() 
	{ 
		//blue circle
		x=100;
		y=50;
		//red circle
		x1=100;
		y1=50;
		//orange circle
		x2=100;
		y2=50;
		
		width=20;
		end=500;
		stepy=2;
		stepx=2;
		setTitle("Visit here:- JerryIndia.home.blog");
		setSize(800,500);
		setLayout(null);
		setVisible(true);
		t1.start();
	}
	public void paint(Graphics g)
	{
		super.paint(g);
		g.setColor(Color.BLUE);
		g.fillOval(x,y,width,width);
		g.setColor(Color.RED);
		g.fillOval(x1,y1,width,width);
		g.setColor(Color.ORANGE);
		g.fillOval(x2,y2,width,width);
	}

	public void run()
	{
		while (true) 
		{
			x+=stepx;
			y+=0;

			x1+=0;
			y1+=stepy;

			x2+=stepx;
			y2+=stepy;

			if(x<=100 || x>=end-width)
			{
				stepx*=-1;
				stepy*=-1;
			}
			repaint();

			try
			{
				t1.sleep(10);
			}
			catch(Exception e)
			{
				JOptionPane.showMessageDialog(null,"e");
			}
		}
	}

	public static void main(String[] args) 
	{
		ANImation JerryIndia=new ANImation();
		
	}
}

Leave a comment