Im still learning GUI, still having some trouble getting my head around threads :/
Im making this GUI in which there are 2 circles, one big (100x100) and one small (50x50). The small one will go to the edge of the big circle and during 0.5seconds, the small circle will go to the center and that it when the user has to click. Whenever the user clicks when the circle is in the middle then the user scores. The only trouble im having is that the circle is not moving about as I suspect its something got to do with my threads, hence the reason why i m using threads, to get to know how to use them.
GUI
public class gui extends JPanel implements MouseListener,
Runnable {
Thread t = new Thread();
int score = 0;
int rnd;
static final int smallcircleposx = 75;
static final int smallcircleposy = 75;
int circleposx = 75;
int circleposy = 75;
int mousex, mousey;
Random random = new Random();
public gui() {
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLUE);
g.fillOval(50, 50, 100, 100);
g.setColor(Color.RED);
g.fillOval(circleposx, circleposy, 50, 50);
}
// THREAD FOR MOVING THE CIRCLE
public void run() {
rnd = random.nextInt(999);
if (rnd % 5 == 0) {
circleposx = circleposx + 25;
} else if (rnd % 4 == 0) {
circleposx = circleposx - 25;
} else if (rnd % 3 == 0) {
circleposy = circleposy + 25;
} else {
circleposy = circleposy - 25;
}
try {
Thread.sleep(500);
circleposx = smallcircleposx;
circleposy = smallcircleposy;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void mouseClicked(MouseEvent m) {
if (circleposx == smallcircleposx && circleposy == smallcircleposy) {
score++;
}
}
MAIN
public class main {
public static void main(String[] args) {
JFrame frame = new JFrame("Circle enlarger");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,400);
frame.setVisible(true);
gui co = new gui();
frame.add(co);
frame.addMouseListener(co);
Thread x = new Thread(new gui());
x.start();
}
}
I am aware that I haven't used all the mouselistener
methods.