Event Handling - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Event Handling

Description:

Java for Video Games Fall 2005. http://www.duke.edu/~cjj1/tip/eStudiesFall2005 ... Make the JButton text change to X when the user clicks on it. ... – PowerPoint PPT presentation

Number of Views:52
Avg rating:3.0/5.0
Slides: 16
Provided by: duke
Category:
Tags: event | handling

less

Transcript and Presenter's Notes

Title: Event Handling


1
Event Handling
2
The Plan
  • Sequential (Single Thread) Model
  • Event Model
  • Making the GUI interactive
  • Examples
  • Practice

3
Sequential (Single Thread) Model
Program Start
Program End
4
Event Model
Program Thread
AWT Event Loop
5
Event Model
  • Executes sequentially
  • Termination of thread does not terminate program
    when GUI present
  • Generally sets up the GUI and displays the
    controls, then terminates

Program Thread
6
Event Model
  • Check for input
  • Notify all listeners of event
  • Repeat

AWT Event Loop
7
Making the GUI Interactive
  • import java.awt.event.
  • implements ActionListener
  • write methodpublic void actionPerformed(ActionEve
    nt e)
  • call addActionListener(this) to all JButtons

8
Examples
GameShell.java
AdderGUI.java
9
Examples
AdderGUI.java
import java.awt. import java.awt.event. import
javax.swing. public class AdderGUI
extends JApplet implements ActionListener
10
Examples
AdderGUI.java
public void actionPerformed(ActionEvent
ae) String
addend0Textaddend0.getText()
double addend0NumberDouble.parseDouble(addend0Tex
t) String addend1Textaddend1.get
Text() double addend1NumberDoubl
e.parseDouble(addend1Text)
double answeraddend0Numberaddend1Number
sum.setText(""answer)
11
Examples
AdderGUI.java
private void makeComponents()
framenew JFrame("Game Shell")
addend0new JTextField(8)
addend1new JTextField(8)
sumnew JTextField(8)
computenew JButton("")
compute.addActionListener(this)
plusnew JLabel("")
plus.setHorizontalAlignment(SwingConstants.CENTER)

12
Examples
GameShell.java
import java.awt. import java.awt.event. import
javax.swing. public class GameShell
extends JApplet implements ActionListener
13
Examples
public void actionPerformed(ActionEvent
ae) Object
causeae.getSource()
if(causepause)
if(pause.getText().equals("Pause"))

pause.setText("Resume")
shell.setText("Paused")
else

pause.setText("Pause")
shell.setText("Game Running")

if(causereset)
pause.setText("Start")
shell.setText("Splash")

GameShell.java
14
Examples
GameShell.java
pausenew JButton("Start")
pause.addActionListener(this)
resetnew JButton("Start New Game")
reset.addActionListener(this)

15
Practice
  • Make a 2x2 tic-tac-toe board out of initially
    blank Jbuttons.
  • Make the JButton text change to X when the user
    clicks on it.
  • Make the JButton text change to X and O
    alternatively as the user clicks on the
    buttons.Hint use a boolean instance variable.
  • Make the fonts larger, and maybe add images.
Write a Comment
User Comments (0)
About PowerShow.com