import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyThread1 implements Runnable{
public void run(){
method();
}
public void method(){
while(true){
try{
Thread.sleep(1000);
System.out.println("Hello to Runnable");
}//end of try
catch(InterruptedException e){
}//end of catch
}//end of while
}//end of method
public static void main(String args[]){
Thread th1=new Thread(new MyThread1());
th1.start();
Thread th2=new Thread(new MyThread2());
th2.start();
}//end of main
}//end of thread1
class MyThread2 extends JPanel implements Runnable,MouseMotionListener{
JFrame frame=new JFrame("Graphis Animations");
int x=0,y=0;
Random ran=new Random();
MyThread2(){
frame.setSize(500,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400,400);
addMouseMotionListener(this);
setBackground(new Color(ran.nextInt(255),ran.nextInt(255),ran.nextInt(255)));
frame.add(this);
frame.setVisible(true);
}//end of constructor
public void run(){
while(true){
try{
Thread.sleep(20);
if(x<350){
x++;
y++;
repaint();
}
else
{
y--;
repaint();
}
}//end of try
catch(InterruptedException e){
}
}//end of while
}//end of run
public void paintComponent(Graphics g){
super.paintComponent(g);
g.fillRect(x,y,50,50);
}
public void mouseMoved(MouseEvent e){
x=e.getX()-30;
y=e.getY()-10;
}
public void mouseDragged(MouseEvent e){
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyThread1 implements Runnable{
public void run(){
method();
}
public void method(){
while(true){
try{
Thread.sleep(1000);
System.out.println("Hello to Runnable");
}//end of try
catch(InterruptedException e){
}//end of catch
}//end of while
}//end of method
public static void main(String args[]){
Thread th1=new Thread(new MyThread1());
th1.start();
Thread th2=new Thread(new MyThread2());
th2.start();
}//end of main
}//end of thread1
class MyThread2 extends JPanel implements Runnable,MouseMotionListener{
JFrame frame=new JFrame("Graphis Animations");
int x=0,y=0;
Random ran=new Random();
MyThread2(){
frame.setSize(500,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400,400);
addMouseMotionListener(this);
setBackground(new Color(ran.nextInt(255),ran.nextInt(255),ran.nextInt(255)));
frame.add(this);
frame.setVisible(true);
}//end of constructor
public void run(){
while(true){
try{
Thread.sleep(20);
if(x<350){
x++;
y++;
repaint();
}
else
{
y--;
repaint();
}
}//end of try
catch(InterruptedException e){
}
}//end of while
}//end of run
public void paintComponent(Graphics g){
super.paintComponent(g);
g.fillRect(x,y,50,50);
}
public void mouseMoved(MouseEvent e){
x=e.getX()-30;
y=e.getY()-10;
}
public void mouseDragged(MouseEvent e){
}
}
No comments:
Post a Comment