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; public class DotTester extends JPanel implements MouseListener { public static int WIDTH=800; public static int HEIGHT=600; private Font titleFont, regularFont; private int x,y; private ArrayList dots; public DotTester() { //initialize variables here... titleFont = new Font("Roman", Font.BOLD, 18); regularFont = new Font("Helvetica", Font.PLAIN, 12); x=0; y=0; dots = new ArrayList(); } 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.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(); dots.add(new Dot(x, y)); repaint(); } @Override public void mouseEntered(MouseEvent e) { } @Override public void mouseExited(MouseEvent e) { } }