a simple Java applet game

Create a Naughty Circle Game in Java

“Create a Naughty Circle Game in Java” is a game that user try to click on a red circle, which is not possible because the red circle always moves according to the mouse coordination.

There are two Java Classes:

1- NaughtyCircle Class: which is the main class, which inherent the JFrame class with two Interface MouseListener MouseMotionListener.

2-NaughtyCircleControler Class: The Controller for the NaughtyCircle Class.

The NaughtyCircle Class:



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

public class NaughtyCircle extends JFrame implements MouseListener, MouseMotionListener {

private int width;
private int height;
private int x;
private int y;
private boolean mouseClick;

public NaughtyCircle(int widthIn, int heightIn) {
width = widthIn;
height = heightIn;
x = 0;
y = 0;
setTitle("The Naughty Circle Game");
setSize(width, height);
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocation(100,100);
setBackground(Color.LIGHT_GRAY);
addMouseListener(this);
addMouseMotionListener(this);
setVisible(true);
}

public void paint(Graphics g) {
super.paint(g);
g.drawString("Try to Click on Red Cirlce", width /2 - 60, 40);
g.setColor(Color.red);
g.fillOval(x - 30, y - 30, 30, 30);
if(mouseClick) {
g.drawString("Keep Triyng You are Almost There", width /2 - 90, height- 20);
}

}

public void mouseMoved(MouseEvent e) {
x = e.getX();
y = e.getY();
repaint();
}

public void mouseDragged(MouseEvent e) {
x = e.getX();
y = e.getY();
repaint();
}

public void mouseClicked(MouseEvent e) {

}

public void mouseEntered(MouseEvent e) {

}

public void mousePressed(MouseEvent e) {
mouseClick = true;
repaint();
}

public void mouseReleased(MouseEvent e) {
mouseClick = false;
repaint();
}

public void mouseExited(MouseEvent e) {

}

}

The NaughtyCircleControler Class:


public class NaughtyCircleControler {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new NaughtyCircle(400,400);
}

}


Leave a Reply

Your email address will not be published. Required fields are marked *