Mouse Listener Application

<< Move a Monster Application | Applications | Mouse Motion Listener Application >>

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class MouseListenerApplication extends JPanel implements MouseListener
{
	public static int WIDTH=800;
	public static int HEIGHT=600;
    private Font titleFont, regularFont;
    private int x,y;


    public MouseListenerApplication()
    {

    	//initialize variables here...
    	titleFont = new Font("Roman", Font.BOLD, 18);
    	regularFont = new Font("Helvetica", Font.PLAIN, 12);
    	x=0;
    	y=0;

    }
	public static void main(String[] args) {
		MouseListenerApplication app= new MouseListenerApplication();
		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("MouseListener Application", 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);
		g.setColor(Color.RED);
		g.fillOval(x-15, y-40, 30, 30);

	}
	// 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();
		repaint();
	}
	@Override
	public void mouseEntered(MouseEvent e) {

	}
	@Override
	public void mouseExited(MouseEvent e) {

	}



}