Tag: Java/J2EE/J2MEMouse and Keyboard Events

Jan 02

Subclass of MouseAdapter

!!!!!!!!!!!! ClickListener.java A simple subclass of MouseAdapter that reports where the mouse was pressed. When attached to an applet, look for the report in the Java Console. !!!!!!!!!!!! import java.awt.event.*; /** The listener used by ClickReporter. * ************** public class ClickListener extends MouseAdapter { public void mousePressed(MouseEvent event) { System.out.println("Mouse pressed at (" + event.getX() …

Continue reading

Jan 02

Applet handle mouse events

%%%%%%%%%%%%%% ClickReporter.java A simple applet that uses the class to handle mouse events %%%%%%%%%%%%%% import java.applet.Applet; import java.awt.*; /** Prints a message saying where the user clicks. * Uses an external listener. * ******************* public class ClickReporter extends Applet { public void init() { setBackground(Color.yellow); addMouseListener(new ClickListener()); } } %%%%%%%%%%%%%% Note: Brought from our old …

Continue reading

Jan 02

A simple applet that uses the ClickListener class

ClickReporter.java A simple applet that uses the ClickListener class to handle mouse events. *************** import java.applet.Applet; import java.awt.*; /** Prints a message saying where the user clicks. * Uses an external listener. * ****** public class ClickReporter extends Applet { public void init() { setBackground(Color.yellow); addMouseListener(new ClickListener()); } } Note: Brought from our old site: …

Continue reading

Jan 02

Tiny applet that uses CircleListener to handle mouse events.

import java.applet.Applet; import java.awt.*; /** Draw circles centered where the user clicks. * Uses an external listener. * *********** public class CircleDrawer1 extends Applet { public void init() { setForeground(Color.blue); addMouseListener(new CircleListener()); } } Note: Brought from our old site: http://www.salearningschool.com/example_codes/ on Jan 2nd, 2017

Jan 02

(No title)

import java.applet.Applet; import java.awt.*; import java.awt.event.*; /** Draw circles centered where the user clicks. * Attaches itself as a listener. * ************* public class CircleDrawer2 extends Applet implements MouseListener { private int radius = 25; public void init() { setForeground(Color.blue); addMouseListener(this); } // Remaining methods are from the MouseListener interface. public void mouseEntered(MouseEvent event) {} …

Continue reading

Jan 02

CircleDrawer3.java Applet that uses a named nested class to handle mouse events and draw circles. Illustrates third approach to event-handling with listeners: using inner classes.

import java.applet.Applet; import java.awt.*; import java.awt.event.*; /** Draw circles centered where the user clicks. * Uses named inner classes. * ****************** public class CircleDrawer3 extends Applet { public void init() { setForeground(Color.blue); addMouseListener(new CircleListener()); } private class CircleListener extends MouseAdapter { private int radius = 25; public void mousePressed(MouseEvent event) { Graphics g = getGraphics(); …

Continue reading

Jan 02

Applet that uses a anonymous nested class to handle mouse events and draw circles. Variation on third approach to event-handling: using inner classes.

import java.applet.Applet; import java.awt.*; import java.awt.event.*; /** Draw circles centered where the user clicks. * Uses anonymous inner classes. * ********************** public class CircleDrawer4 extends Applet { public void init() { setForeground(Color.blue); addMouseListener (new MouseAdapter() { private int radius = 25; public void mousePressed(MouseEvent event) { Graphics g = getGraphics(); g.fillOval(event.getX()-radius, event.getY()-radius, 2*radius, 2*radius); } …

Continue reading

Jan 02

Applet that uses processXxx methods to print detailed reports on mouse events. Illustrates low-level alternative to handling events with listeners.

import java.applet.Applet; import java.awt.*; import java.awt.event.*; /** Prints non-detailed reports of mouse events. * Uses the low-level processXxxEvent methods instead * of the usual event listeners. * ***************** public class MouseReporter extends Applet { public void init() { setBackground(Color.blue); // So you can see applet in page enableEvents(AWTEvent.MOUSE_EVENT_MASK | AWTEvent.MOUSE_MOTION_EVENT_MASK); } public void processMouseEvent(MouseEvent event) …

Continue reading

Jan 02

A TextField that uses key events to correct the spelling of the names of computer languages entered into it

import java.awt.*; import java.awt.event.*; /** A spelling-correcting TextField for entering * a language name. * ******************* public class LanguageField extends TextField { private String[] substrings = { "", "J", "Ja", "Jav", "Java" }; public LanguageField() { addKeyListener(new SpellingCorrector()); addActionListener(new WordCompleter()); addFocusListener(new SubliminalAdvertiser()); } // Put caret at end of field. private void setCaret() { setCaretPosition(5); …

Continue reading

Jan 02

JavaTextField.java

import java.applet.Applet; import java.awt.*; /** Lets the user enter the name of any * good programming language. Or does it? * ********************* public class JavaTextField extends Applet { public void init() { setFont(new Font("Serif", Font.BOLD, 14)); setLayout(new GridLayout(2, 1)); add(new Label("Enter a Good Programming Language", Label.CENTER)); LanguageField langField = new LanguageField(); Font langFont = new …

Continue reading

Jan 02

An applet that permits freehand drawing

import java.applet.Applet; import java.awt.*; import java.awt.event.*; /** An applet that lets you perform freehand drawing. * **************** public class SimpleWhiteboard extends Applet { protected int lastX=0, lastY=0; public void init() { setBackground(Color.white); setForeground(Color.blue); addMouseListener(new PositionRecorder()); addMouseMotionListener(new LineDrawer()); } protected void record(int x, int y) { lastX = x; lastY = y; } // Record position …

Continue reading

Skip to toolbar