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.Timer;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class DotTester extends JPanel implements MouseListener, ActionListener
{ public static int WIDTH=800;
public static int HEIGHT=600;
private Font titleFont, regularFont;
private int x,y;
private Dot[] dots;
private String message;
public DotTester()
{
//initialize variables here...
titleFont = new Font("Roman", Font.BOLD, 18);
regularFont = new Font("Helvetica", Font.PLAIN, 12);
x=0;
y=0;
message="Hit the red dot";
dots = new Dot[3];
dots[0] = new Dot(50,200, 50, Color.BLUE, this,400, 350);
dots[1] = new Dot(110,200, 50, Color.BLUE, this, 10, 490);
dots[2] = new Dot(170,200, 50, Color.RED, this, 10, 150);
//wait = new Timer(1000, this);
//duration = new Timer(100, this);
//wait.addActionListener(this);
//duration.addActionListener(this);
dots[0].setVisable(true);
dots[0].start();
}
public static void main(String[] args) {
DotTester app= new DotTester();
JFrame window = new JFrame("Dot Tester");
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 Tester", 20, 20);
g.drawString("X = "+x+" Y = "+y, 20, 100);
g.drawString(message, 500,200);
g.setColor(Color.BLACK);
g.setFont(regularFont);
g.drawString("Click with your mouse", 20, 40);
for (Dot d:dots)
d.draw(g);
}
// update is a workaround to cure Windows screen flicker problem
public void update(Graphics g){
paint(g);
}
/**
* These are the Methods needed to implement the MouseListener Interface
*/
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
x=e.getX();
y=e.getY();
if (dots[2].contains(x,y) )
message = "hit!";
else
message = "miss";
repaint();
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
public void actionPerformed(ActionEvent e) {
for(int i=0; i< dots.length; i++)
{
int next = (i+1)%3;
Timer wait = dots[i].getWaitTimer();
Timer duration = dots[i].getDurationTimer();
if (e.getSource()==duration){// time turn off
duration.stop();
dots[next].getWaitTimer().start();
dots[i].setVisable(false);
}
if (e.getSource()==wait)
{
wait.stop();
duration.start();
dots[i].setVisable(true);
}
}
repaint();
}
}