introduction to java classes, events, gui’s. understand: how to use textpad how to define a class...

31
Introduction to Java Classes, events, GUI’s

Upload: janae-bursell

Post on 14-Dec-2015

224 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven

Introduction to Java

Classes, events, GUI’s

Page 2: Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven

Understand:

How to use TextPad

How to define a class or object

How to create a GUI interface

How event-driven programming works

How classes inherit methods

Overall program structure

Page 3: Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven

Major Example

Page 4: Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven

Red

colorMe

Green Blue

darker reSize

Page 5: Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven

Red Green Blue

Each color is mixture of amounts of Red, Green, and Blue

( 200, 50 125 )

values in range: 0 … 255

Page 6: Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven

Illustrate with Rectangle object

Class definition of rectangle

including constructor, properties, methods

Test program that creates instance of rectangle

Classes or Objects in Java

Page 7: Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven

Class Rectangle

int height , width ;

{

}

public Rectangle (int h, int w ) {

}

height = h; width = w;

int findArea ( ) {

}

return height * width ;

instance variablesor properties

constructor

Class blueprint

int getHght ( ) { return height }

methods

Page 8: Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven

public class TestRectangle {

}

public static void main ( String [ ] args ){

}

Rectangle r ;

r = new Rectangle (2, 4) ;

int area = r.findArea ( );

System.out.println ( "Area is: " + area )

Test program

instance rof rectangle

saved in file TestRectangle

say:r’s findArea

constructor

Page 9: Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven

public class Rectangle {

}

public static void main ( String [ ] args ){

}

Rectangle r ;

r = new Rectangle (2, 4) ;

int area = r.findArea ( );

System.out.println ( "Area is: " + area ) ;anotherinstance

System.out.println ( "Area is: " + s.findArea ( ) );

Rectangle s = new Rectangle (5, 10) ;

Page 10: Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven

Major Example

Page 11: Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven

Red

colorMe

Green Blue

darker reSize

objectProgram Design

Page 12: Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven

Design object

GUI componentsRecognize & handle events

Make Test program to create instances

Page 13: Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven

GUI design

Build object on top of frame class

Add panel to frame

Put GUI elements in frame:

Add labels, textfields, & buttons

Make object listen for clicks on buttons

inherit propertiesof frames

Page 14: Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven

Red

colorMe

Green Blue

darker reSize

object

labels

buttons

text fields

panel

Page 15: Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven

Event-handler design

Recognize source of event

Take appropriate action

- grab data from textfield- convert data to integer

- use value to make RGB color

- RGB: ( r, g, b )

- change color of panel

Asynchronouslycalled

Page 16: Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven

Program Structure for Demo

Page 17: Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven

import java.awt.*;

public class TestColorizer{

}

public static void main (String[ ] args) { … }

class Colorizer

{

}

public void actionPerformed(ActionEvent e) { … }

public Colorizer( ) { … }

private JTextField Num1 … ;

implements ActionListenerextends JFrame

Program Structure

Page 18: Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven

class Colorizer{

}

implements ActionListenerextends JFrame

private JTextField Num1 … ;

Instancevariables

public Colorizer( ) { … }

constructor

public void actionPerformed(ActionEvent e) { … }

Event-handler

Class

Page 19: Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven

Content Pane

X

titlebar

JFrame

Page 20: Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven

X

Red

colorMe

Green Blue

darker reSize

Colorizer objectextends Jframe

Page 21: Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven

JFrame

Title Bar Content Pane

Panel

JTextFieldJLabel JButton

Page 22: Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven

public Colorizer ( ) {

}

p1 = new JPanel ( );this.getContentPane().add(p1);

p1.add(new JLabel(“Red"));p1.add(Num1 = new JTextField(3 ));

p1.add(colorMe = new JButton("ColorMe"));

colorMe.addActionListener (this );

objects

Constructor

Page 23: Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven

public Colorizer ( ) {

}

p1 = new JPanel ( );this.getContentPane().add(p1);

p1.add(new JLabel(“Red"));p1.add(Num1 = new JTextField(3 ));

p1.add(colorMe = new JButton("ColorMe"));

colorMe.addActionListener (this );

named so can refer to in event-handler

Constructor

un-named since not referred to in event-handler

system listensfor button click

Page 24: Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven

class Colorizer

{

}

public Colorizer( ) { … }

private JTextField Num1 ;

implements ActionListenerextends JFrame

private JButton colorMe ;

private JPanel p1 ;

public void actionPerformed(ActionEvent e) { … }

instancevariables

Class…again

Page 25: Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven

if (e.getSource() == colorMe ){

}

public void actionPerformed (ActionEvent e) {

}

p1.setBackground ( c );

Integer.parseInt ( );int R = Num1.getText( )

recognize eventsource

Get & convertnumber

c = new Color (R, 100, 150);

Create color

Event-handlermethodsignature

Page 26: Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven

import java.awt.*;

public class TestColorizer{

}

public static void main (String[ ] args) {

}

Colorizer f = new Colorizer( );

f.setSize (400,400);

f.setVisible(true);

import javax.swing.*;import java.awt.event.*;

create instanceof object

use inheritedmethods

Test program

Page 27: Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven

public class TestColorizer{

}

public static void main (String[ ] args) {

}

Colorizer f = new Colorizer( );f.setSize (400,400);f.setVisible(true);

import java.awt.*;

import javax.swing.*;import java.awt.event.*;

{

}

class Colorizer implements ActionListenerextends JFrame

if (e.getSource() == colorMe ){} c = new Color (R, 100, 150);

p1.setBackground ( c );

Integer.parseInt ( );int R = Num1.getText( )

public void actionPerformed (ActionEvent e) {

}

private JTextField Num1 ;

private JButton colorMe;private JPanel p1 ;

private Color c ;

p1 = new JPanel ( );this.getContentPane().add(p1);

p1.add(new JLabel(“Red"));p1.add(Num1 = new JTextField(3 ));

p1.add(colorMe = new JButton("ColorMe"));

colorMe.addActionListener (this );

public Colorizer ( ) {

}

prototypeprogram

Page 28: Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven

Outline

Classes in Java

Rectangle exampleproperties or instance variablesinstances of classconstructor for class

Demo Colorizer Example

compilation executionRGB representation panel colorbuttons darken-button resize-buttonColorizer classmain program

TextPad

Page 29: Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven

Colorizer Class Design

program structure

constructor

event-handler

GUI setup

constructor

recognize event-source

grab text

make Color

reset background color

reset window size

event-handler

instance variables

declarationslabels

textFields

buttons

panel

actionListeners

Outline

Page 30: Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven

TextPad

Multiple instances

Terms and Concepts

GUI componentsJTextFields

JButtons

JLabels

Event-driven programming

AsynchronousInheriting properties & methods

Instance variables

Constructors

Method signature

ClassesObjectsMethodsInstances of objects

JFrames

import statements

extend class

JFrame’s setSize methodJFrame’s setVisible method

JPanelsJPanel’s add methodthis notationActionEvent

ActionEvent’e getSource() method

Color objectsRGB representation

Test programProgram structure

JPanel’s setBackground ( c) method

Page 31: Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven