Chapter 10
Download
1 / 40

Chapter 10 - PowerPoint PPT Presentation


  • 276 Views
  • Updated On :

Chapter 10. Event Handling. Events. Mouse move Key press Button click . . . originate from event source processed by event listener. Event classes. Event class (e.g. MouseEvent ). Contains information about the event (e.g. mouse location)

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Chapter 10' - Sharon_Dale


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Chapter 10 l.jpg
Chapter 10

Event Handling


Events l.jpg
Events

Mouse move

Key press

Button click

. . .

originate from event source

processed by event listener


Event classes l.jpg
Event classes

Event class (e.g. MouseEvent). Contains information about the event (e.g. mouse location)

Event listener. Knows what to do when the event occurs. Implements listener interface (e.g. MouseListener)

Event source. Keeps a list of listeners and notifies them when the event occurs


Slide4 l.jpg

Figure 1

Event Classes


Listening to mouse events l.jpg
Listening to mouse events

  • MouseListener interface has 5 methods

    • void mouseClicked(MouseEvent e)

    • void mouseEntered(MouseEvent e)

    • void mouseExited(MouseEvent e)

    • void mousePressed(MouseEvent e)

    • void mouseReleased(MouseEvent e)


Spying on events l.jpg
Spying on events

  • class MouseSpy implements MouseListener{ public void mouseClicked(MouseEvent e) { System.out.println ("Mouse clicked. x=" + e.getX() + " y=" + e.getY()); } // same for other 4 listener methods}


Adding the listener l.jpg
Adding the listener

  • public class MouseSpyApplet{ public MouseSpyApplet() { MouseSpy listener = new MouseSpy(); addMouseListener(listener); }}


Slide8 l.jpg

Figure 2

Spying on Mouse Events


Event adapters l.jpg
Event Adapters

  • Most mouse listeners only need one or two of the 5 listener methods

  • Boring to redefine the others to do nothing

  • MouseAdapter implements all 5 methods to do nothing.

  • Extend MouseAdapter and just override the methods you need

  • Other listener interfaces with > 1 method also have adapters


Move egg to mouse position l.jpg
Move egg to mouse position

  • public class EggApplet extends Applet{ public EggApplet() { egg = new Ellipse2D.Double(...); } public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; g2.draw(egg); } private Ellipse2D.Double egg;}


Add mouse listener l.jpg
Add mouse listener

  • public EggApplet(){ . . . MouseClickListener listener = new MouseClickListener(); addListener(listener);}

  • class MouseClickListener extends MouseAdapter{ public void mouseClicked(MouseEvent e) { // set egg center to // e.getX(), e.getY() }}


Make the listener an inner class l.jpg
Make the listener an inner class

  • Listener can't access egg instance variable

  • Make it an inner class

  • class EggApplet{ . . .private class MouseClickListener extends MouseAdapter { . . . }}

  • Inner class methods can access outer class instance variables


Inner classes l.jpg
Inner classes

  • class EggApplet extends Applet{ . . . private class MouseClickListener extends MouseAdapter { public void mouseClicked (MouseEvent e) { int mouseX = e.getX(); int mouseY = e.getY(); egg.setFrame(...); repaint(); } }}


Accessing outer class features l.jpg
Accessing outer class features

  • Inner class has no egg instance variable

  • Accesses outer class variable

  • Inner class has no repaint method

  • Calls outer class method

  • On which EggApplet instance?

  • The one that constructed the inner class object—the inner class object remembers who constructed it


Slide15 l.jpg

Figure 3

An Inner-Class Object Remembers

the Outer-Class Object That

Constructed It


Slide16 l.jpg

Program EggApplet.java

import java.applet.Applet;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import java.awt.geom.Ellipse2D;

public class EggApplet extends Applet

{ public EggApplet()

{ egg = new Ellipse2D.Double(0, 0, EGG_WIDTH, EGG_HEIGHT);

// add mouse click listener

MouseClickListener listener = new MouseClickListener();

addMouseListener(listener);

}


Slide17 l.jpg

public void paint(Graphics g)

{ Graphics2D g2 = (Graphics2D)g;

g2.draw(egg);

}

private Ellipse2D.Double egg;

private static final double EGG_WIDTH = 30;

private static final double EGG_HEIGHT = 50;

// inner class definition

private class MouseClickListener extends MouseAdapter

{ public void mouseClicked(MouseEvent event)

{ int mouseX = event.getX();

int mouseY = event.getY();

// now move the ellipse to(mouseX, mouseY)


Slide18 l.jpg

egg.setFrame(mouseX - EGG_WIDTH / 2,

mouseY - EGG_HEIGHT / 2, EGG_WIDTH, EGG_HEIGHT);

repaint();

}

}

}


Slide19 l.jpg

Figure 4

Handling Mouse Events


Graphical applications l.jpg
Graphical Applications

  • Live inside frame window

  • Set size and title

  • Call show method

  • That ends the main method, but the program keeps running

  • GUI thread handles events


Slide21 l.jpg

Figure 5

A Frame Window


Slide22 l.jpg

Program FrameTest1.java

import javax.swing.JFrame;

public class FrameTest1

{ public static void main(String[] args)

{ EmptyFrame frame = new EmptyFrame();

frame.setTitle("Frame Test");

frame.show();

}

}

class EmptyFrame extends JFrame

{ public EmptyFrame()

{ final int DEFAULT_FRAME_WIDTH = 300;

final int DEFAULT_FRAME_HEIGHT = 300;

setSize(DEFAULT_FRAME_WIDTH, DEFAULT_FRAME_HEIGHT);

}

}


Quitting graphical apps l.jpg
Quitting Graphical Apps

  • Define window closer:class WindowCloser extends WindowAdapter{ public void windowClosing (WindowEvent e) { System.exit(0); }}

  • Add as window listener to the frame


Slide24 l.jpg

Program FrameTest2.java

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import javax.swing.JFrame;

public class FrameTest2

{ public static void main(String[] args)

{ EmptyFrame frame = new EmptyFrame();

frame.setTitle("Close me!");

frame.show();

}

}


Slide25 l.jpg

class EmptyFrame extends JFrame

{ public EmptyFrame()

{ final int DEFAULT_FRAME_WIDTH = 300;

final int DEFAULT_FRAME_HEIGHT = 300;

setSize(DEFAULT_FRAME_WIDTH, DEFAULT_FRAME_HEIGHT);

WindowCloser listener = new WindowCloser();

addWindowListener(listener);

}

private class WindowCloser extends WindowAdapter

{ public void windowClosing(WindowEvent event)

{ System.exit(0);

}

}

}


Add gui components l.jpg
Add GUI components

  • Panels (for drawing)

  • Text fields (for text input)

  • Buttons

  • Menus


Drawing on a panel l.jpg
Drawing on a Panel

  • public class MyPanel extends JPanel{ public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; // your drawing code here }}


Slide28 l.jpg

Figure 6

Anatomy of a Swing Frame


Layout manager l.jpg
Layout Manager

  • Content pane uses the border layout by default

  • Container contentPane = getContentPane();contentPane.add(myPanel, "Center");


Slide30 l.jpg

Figure 7

Component Areas of a Border Layout


Text input l.jpg
Text input

  • JTextField textField = new JTextField();contentPane.add(textField, "South");

  • JTextField generates action events

  • ActionListener interface has a single method: actionPerformed


Slide32 l.jpg

Figure 8

A Frame with a Text Field


Processing text input l.jpg
Processing Text Input

  • class TextFieldListener implements ActionListener{ public void actionPerformed (ActionEvent event) { // get user input String input = textField.getText(); // process input . . . // clear text field input.setText(""); }}


Slide34 l.jpg

Figure 9

Drawing Random Eggs


Slide35 l.jpg

Program Eggs.java

import java.awt.Container;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.awt.geom.Ellipse2D;

import java.util.Random;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JTextField;


Slide36 l.jpg

public class Eggs

{ public static void main(String[] args)

{ EggFrame frame = new EggFrame();

frame.setTitle("Enter number of eggs");

frame.show();

}

}

class EggFrame extends JFrame

{ public EggFrame()

{ final int DEFAULT_FRAME_WIDTH = 300;

final int DEFAULT_FRAME_HEIGHT = 300;

setSize(DEFAULT_FRAME_WIDTH, DEFAULT_FRAME_HEIGHT);

addWindowListener(new WindowCloser());

// construct components

panel = new EggPanel();


Slide37 l.jpg

textField = new JTextField();

textField.addActionListener(new TextFieldListener());

// add components to content pane

Container contentPane = getContentPane();

contentPane.add(panel, "Center");

contentPane.add(textField, "South");

}

private JTextField textField;

private EggPanel panel;

private class TextFieldListener implements ActionListener

{ public void actionPerformed(ActionEvent event)

{ // get user input

String input = textField.getText();

// process user input


Slide38 l.jpg

panel.setEggCount(Integer.parseInt(input));

// clear text field

textField.setText("");

}

}

private class WindowCloser extends WindowAdapter

{ public void windowClosing(WindowEvent event)

{ System.exit(0);

}

}

}

class EggPanel extends JPanel

{ public void paintComponent(Graphics g)

{ super.paintComponent(g);

Graphics2D g2 = (Graphics2D)g;


Slide39 l.jpg

// draw eggCount ellipses with random centers

Random generator = new Random();

for (int i = 0; i < eggCount; i++)

{ double x = getWidth() * generator.nextDouble();

double y = getHeight() * generator.nextDouble();

Ellipse2D.Double egg = new Ellipse2D.Double(x, y,

EGG_WIDTH, EGG_HEIGHT);

g2.draw(egg);

}

}

/**

Sets the number of eggs to be drawn and repaints

the panel.

@param count the new number of eggs

*/


Slide40 l.jpg

public void setEggCount(int count)

{ eggCount = count;

repaint();

}

private int eggCount;

private static final double EGG_WIDTH = 30;

private static final double EGG_HEIGHT = 50;

}