hi..

I don't know why the circle doesn't move ..

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


public class MouseGameV2 extends JFrame implements MouseMotionListener {	

	CircleClass a[];
	Graphics gx;
	int m = 5;
	Thread move1;
//---------------------//
	JPanel p1 = new JPanel();
	JPanel p2 = new JPanel();
	JPanel p3 = new JPanel();
	JPanel p4 = new JPanel();
	JPanel p5 = new JPanel();

	JLabel tL = new JLabel(" Your Time >> ");
	JLabel mL = new JLabel("  Minute(s)");
	JTextField mF = new JTextField ("0");
	JLabel sL = new JLabel("  Second(s)");
	JTextField sF = new JTextField ("0");
	JLabel spaceL = new JLabel (" ");
//---------------------//


    public MouseGameV2() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	setTitle("MouseGame");
    setSize(500, 500);
    
    p1.setLayout(new GridLayout(1,5));
    
    mF.setEditable(false);
    sF.setEditable(false);
    
    p1.add(tL);  	p1.add(mF); 	p1.add(mL);  	p1.add(sF); 	p1.add(sL); 	
    add(p1, BorderLayout.NORTH);
    
    p2.setLayout(new FlowLayout());
    
    p2.add(p3);
    p3.setBackground(Color.WHITE);
    p2.setBackground(Color.WHITE);
    add(p2,BorderLayout.CENTER);
    p2.addMouseMotionListener(this);
    
    a = new CircleClass[m];
    
    for(int i = 0 ; i < a.length ; i++){
    	a[i] = new CircleClass(32+(i*2),66-(i*7),50,50,1,1);
    }
    
    setVisible(true);	
    	
    }

    public class ToMove{
    	public ToMove(){
    	Thread move1 = new Thread(){
    	    public void run(){
    	while (true){
   			try{Thread.sleep(20);} catch(InterruptedException e){System.out.println("Ooops ..!!"); System.exit(0);}
//   			for(int i = 0; i < m; i++){
//   				a[i].move();
   				System.out.println(" moveFOR");
//   			}
   			System.out.println(" move2");
   			repaint();
    	}
    }    
    
    public void update(Graphics g){
    	paint(g); // to kill the blinking ..
    }	
    	};
    	move1.start();
    }
    

    	}
    	
 
    

    

//--------Timer Class--------//
    public class TimerClass {	
	public TimerClass(){
	Thread time1 = new Thread(){
	public void run(){
    
    int sec = 55;
	int min = 0;
	int i = 0;	
			
	while(true){
		sF.setText(""+ sec++);
		if(sec == 60){
		sec=0;
		mF.setText(""+(++min));	
		new CircleClass(50,50,50,50,1,1).paint(getGraphics());		
		}
  			try{
			Thread.sleep(1000);
		} catch(InterruptedException ex){
			System.out.println("ERROR");
			System.exit(0);
		}
    }
	}	
	};
	time1.start();
	}

}
//--------Timer Class--------// 

 
    public void mouseDragged(MouseEvent e) {
    }   
    	
    public void mouseMoved(MouseEvent e) {
    	System.out.println(e.getX() + " .... " + e.getY());
    }    
    
   
   public static void main(String [] args){
   	new MouseGameV2().new TimerClass();
   	
   	
   }

    }

Recommended Answers

All 4 Replies

Why have you not implemented Runnable on any of the classes you are trying to run threads in?

public class TimerClass implements Runnable {
public class MouseGameV2 extends JFrame implements MouseMotionListener,Runnable {

Excuse me .. I forgor to post Circle Class ..

here it is :

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

public class CircleClass {
	
	int x,y,dx,dy,w,h;
	int r = 50;
	Graphics g;
	
    public CircleClass(int x, int y, int w, int h , int dx , int dy ) {
    	this.x = x;
    	this.y = y;
    	this.w = w;
    	this.h = h;
    	this.dx = dx;
    	this.dy = dy;
    }
    
    public void move(){
    	x+=dx;
    	y+=dy;
    	
    	if(x < 0 || x > w - (r*2)){
    		dx=-dx;
    		x+=dx;
    	}
    	
    	if(y < 0 || y > h - (r*2)){
    		dy=-dy;
    		y+=dy;
    	}    	
    }
    
    public void paint(Graphics g){
    	this.g = g;
    	g.setColor(Color.BLACK);
    	g.fillOval(x,y,r,r);
    	System.out.println("Reached");
    }
    
}

Why have you not implemented Runnable on any of the classes you are trying to run threads in?

public class TimerClass implements Runnable {
public class MouseGameV2 extends JFrame implements MouseMotionListener,Runnable {

mmm .. I will try it now ..

this is my code after correction .. but now Timer thread and move thread , they didn't work ..

????

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


public class MouseGameV2 extends JFrame implements Runnable, MouseMotionListener {	

	CircleClass a[];
	Graphics gx;
	int m = 5;
	Thread move1;
//---------------------//
	JPanel p1 = new JPanel();
	JPanel p2 = new JPanel();
	JPanel p3 = new JPanel();
	JPanel p4 = new JPanel();
	JPanel p5 = new JPanel();

	JLabel tL = new JLabel(" Your Time >> ");
	JLabel mL = new JLabel("  Minute(s)");
	JTextField mF = new JTextField ("0");
	JLabel sL = new JLabel("  Second(s)");
	JTextField sF = new JTextField ("0");
	JLabel spaceL = new JLabel (" ");
//---------------------//


    public MouseGameV2() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	setTitle("MouseGame");
    setSize(500, 500);
    
    p1.setLayout(new GridLayout(1,5));
    
    mF.setEditable(false);
    sF.setEditable(false);
    
    p1.add(tL);  	p1.add(mF); 	p1.add(mL);  	p1.add(sF); 	p1.add(sL); 	
    add(p1, BorderLayout.NORTH);
    
    p2.setLayout(new FlowLayout());
    
    p2.add(p3);
    p3.setBackground(Color.WHITE);
    p2.setBackground(Color.WHITE);
    add(p2,BorderLayout.CENTER);
    p2.addMouseMotionListener(this);
    
    a = new CircleClass[m];
    
    for(int i = 0 ; i < a.length ; i++){
    	a[i] = new CircleClass(32+(i*2),66-(i*7),50,50,1,1);
    }
    
    setVisible(true);	
    	
    }

//    public class ToMove{
    	public void start(){
    	Thread move1 = new Thread(this);
    	move1.start();
    	}
    	public void run(){
    	while (true){
   			try{Thread.sleep(20);} catch(InterruptedException e){System.out.println("Ooops ..!!"); System.exit(0);}
//   			for(int i = 0; i < m; i++){
//   				a[i].move();
   				System.out.println(" moveFOR");
//   			}
   			System.out.println(" move2");
   			repaint();
    	}  
    	
    	}
    	
    	    public void update(Graphics g){
    	paint(g); // to kill the blinking ..
    }
    

//--------Timer Class--------//
    public class TimerClass implements Runnable{	
	public void start(){
	Thread time1 = new Thread();
	time1.start();
	}
	public void run(){
    
    int sec = 55;
	int min = 0;
	int i = 0;	
			
	while(true){
		sF.setText(""+ sec++);
		if(sec == 60){
		sec=0;
		mF.setText(""+(++min));	
		new CircleClass(50,50,50,50,1,1).paint(getGraphics());		
		}
  			try{
			Thread.sleep(1000);
		} catch(InterruptedException ex){
			System.out.println("ERROR");
			System.exit(0);
		}
    }
	}	
	}
	


//--------Timer Class--------// 

 
    public void mouseDragged(MouseEvent e) {
    }   
    	
    public void mouseMoved(MouseEvent e) {
    	System.out.println(e.getX() + " .... " + e.getY());
    }    
    
   
   public static void main(String [] args){
   	new MouseGameV2().start();
   	
   	
   }
   }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.