Mouse Events in JDK 1.0.x

Mouse Clicks

import java.awt.*;

public class EventsOld extends Frame
{
    public boolean mouseDown(Event e, int x, int y)
    {
        click_x = x;
        click_y = y;
        repaint();
        return true;      // We handled the event
    }

    public void paint(Graphics g)
    {
        g.setColor(Color.white);
        g.drawString("Here you are!", click_x, click_y);
    }

    public EventsOld()
    {
        super("EventsOld");
        setBackground(Color.red);
        resize(300, 200);
        show();
    }

    public static void main(String[] arga)
    {
        Frame f = new EventsOld();
    }

    ///////////// Data //////////////////
    private int click_x = 0, click_y = 0;
}

Mouse Drags

import java.awt.*;

public class EventsOld extends Frame
{
    public boolean mouseDown(Event e, int x, int y)
    {
        start = new Point(x, y);
        end   = new Point(x, y);
        repaint();
        return true;      // We handled the event
    }

    public boolean mouseDrag(Event e, int x, int y)
    {
        end   = new Point(x, y);
        repaint();
        return true;      // We handled the event
    }

    public void paint(Graphics g)
    {
        g.setColor(Color.white);
        g.drawLine(start.x, start.y, end.x, end.y);
    }

    public EventsOld()
    {
        super("EventsOld");
        setBackground(Color.red);
        resize(300, 200);
        show();
    }

    public static void main(String[] arga)
    {
        Frame f = new EventsOld();
    }

    ///////////// Data //////////////////
    private Point start, end;
}

Mouse Double Clicks

import java.awt.*;

public class EventsOld extends Frame
{
    public boolean mouseDown(Event e, int x, int y)
    {
        start = new Point(x, y);
        end   = new Point(x, y);
        if (e.clickCount == 2)	// double click ?
           double_click = true;
        repaint();
        return true;      // We handled the event
    }

    public boolean mouseDrag(Event e, int x, int y)
    {
        end   = new Point(x, y);
        repaint();
        return true;      // We handled the event
    }

    public void paint(Graphics g)
    {
        g.setColor(Color.white);
        if (double_click)
            g.drawOval(start.x, start.y, 70, 30);
        else
            g.drawLine(start.x, start.y, end.x, end.y);
        double_click = false;
    }

    public EventsOld()
    {
        super("EventsOld");
        setBackground(Color.red);
        resize(300, 200);
        show();
    }

    public static void main(String[] arga)
    {
        Frame f = new EventsOld();
    }

    ///////////// Data //////////////////
    private Point start, end;
    private boolean double_click = false;
}

Event Modifiers and Other Mouse Buttons

The Event class has a modifier field, and a number of event modifier constants:

ALT_MASK

Mouse button 2

META_MASK

Mouse button 3

Here's an example of its use:

import java.awt.*;

public class EventsOld extends Frame
{
    public boolean mouseDown(Event e, int x, int y)
    {
        start = new Point(x, y);
        end   = new Point(x, y);
        if (e.clickCount == 2)	// double click?
        {
            Graphics g = getGraphics();
            g.setColor(Color.cyan);
            g.drawOval(start.x, start.y, 70, 30);
        }
	// No repaint() !!!
        return true;      // We handled the event
    }

    public boolean mouseDrag(Event e, int x, int y)
    {
        end   = new Point(x, y);
        Graphics g = getGraphics();
        if ((e.modifiers & Event.META_MASK) != 0)
           g.setColor(Color.blue); // Mouse button 2
        else
           g.setColor(Color.white);// Mouse button 1
        g.drawLine(start.x, start.y, end.x, end.y);
        start = new Point(x, y);   // Drag from here next
	// No repaint() !!!
        return true;      // We handled the event
    }
    
    // No paint() -- using default

    public EventsOld()
    {
        super("EventsOld");
        setBackground(Color.red);
        resize(300, 200);
        show();
    }

    public static void main(String[] arga)
    {
        Frame f = new EventsOld();
    }

    ///////////// Data //////////////////
    private Point start, end;
}

This page was last modified on 02 October, 2007