**Dot Clicker**
Here is a dot that has a "is hit" method. If you want an image instead, take a look at this [[https://mathorama.com/apcs/pmwiki.php?n=Main.ImageMovementApplication|Image Movement Example]]
import java.awt.Graphics;
import java.awt.Color;
public class Dot
{
private int x,y,size;
/**
* Constructor for objects of class Dot
*/
public Dot()
{
x = 50+(int)(500*Math.random());
y = 50+(int)(300*Math.random());
size = 10 +(int)(20*Math.random());
}
public Dot(int x,int y,int size)
{
this.x = x;
this.y=y;
this.size = size;
}
public void draw(Graphics g){
g.setColor(Color.BLACK);
g.fillOval(x,y,size,size);
}
/**
* returns true if (x0,y0) is inside the dot
*/
public boolean isHit(int x0, int y0)
{
double x2 = x0 - (x+size/2.0);
double y2 = y0 - (y+size/2.0);
return x2*x2 + y2*y2 - size*size <0;// Pythagorean theorem
}
}
Here is a basic Timer with spawning of dots and expiration of dots
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class DotClicker extends JPanel implements MouseListener, ActionListener
{
public static int WIDTH=800;
public static int HEIGHT=600;
private Font titleFont, regularFont;
private int x,y, hits, misses;
private Timer spawn, expire;
private ArrayList dots;
public DotClicker()
{
titleFont = new Font("Roman", Font.BOLD, 18);
regularFont = new Font("Helvetica", Font.PLAIN, 12);
spawn = new Timer(1000,this);
expire = new Timer(3000,this);
dots = new ArrayList();
dots.add(new Dot());
spawn.start();
expire.start();
}
public static void main(String[] args) {
DotClicker app= new DotClicker();
JFrame window = new JFrame("Mouse Listener Application");
window.setSize(WIDTH, HEIGHT);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().add(app);
window.getContentPane().addMouseListener(app);
window.setVisible(true);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.WHITE);
g.fillRect(0, 0, getWidth(),getHeight());
g.setColor(Color.BLUE);
g.setFont(titleFont);
g.drawString("Dot Clicker Starter Code", 20, 20);
g.setColor(Color.BLACK);
g.setFont(regularFont);
g.drawString("Hits: "+hits, 20, 40);
g.drawString("Misses: "+misses, 20, 60);
//Draw objects
for(Dot d:dots)
d.draw(g);
}
/**
* These are the Methods needed to implement the MouseListener Interface
*/
public void mouseClicked(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {
x=e.getX();
y=e.getY();
for(int i=dots.size()-1; i >= 0; i--)
{
if (dots.get(i).isHit(x,y))
{
hits++;
dots.remove(i);
repaint();
}
}
repaint();
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) { }
public void actionPerformed(ActionEvent e) {
if (e.getSource()==spawn){
dots.add( new Dot() );
}else if (dots.size()>0){// remove oldest
dots.remove(0);
misses++;
}
repaint();
}
}