graphics and event-driven programs

30
Graphics and event-driven programs Learning objectives By the end of this lecture you should be able to: identify and use some of the common components of the Java Swing package; program graphics components to handle mouse-click events; describe the role of layout managers; use the FlowLayout and BorderLayout managers; make use of compound containers.

Upload: charleigh-edwards

Post on 30-Dec-2015

42 views

Category:

Documents


3 download

DESCRIPTION

Graphics and event-driven programs. Learning objectives. By the end of this lecture you should be able to:. identify and use some of the common components of the Java Swing package; program graphics components to handle mouse-click events; describe the role of layout managers ; - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Graphics and event-driven programs

Graphics and event-driven programs

Learning objectives

By the end of this lecture you should be able to:

• identify and use some of the common components of the Java Swing package;

• program graphics components to handle mouse-click events;

• describe the role of layout managers;

• use the FlowLayout and BorderLayout managers;

• make use of compound containers.

Page 2: Graphics and event-driven programs

Which printer [a] LPT1 or [b] LPT2?:

How many copies do you wish to print?:

[P]rint now or [C]ancel?:

Print all pages (y/n)?:

1

2

y

C

Page 3: Graphics and event-driven programs
Page 4: Graphics and event-driven programs

Swing and AWT

AWTSwing

Page 5: Graphics and event-driven programs

Swing Components

Page 6: Graphics and event-driven programs

The SmileyFace class

public class RunSmileyFace{ public static void main(String[] args) { new SmileyFace(); }}

Page 7: Graphics and event-driven programs

public class SmileyFace

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

{ public SmileyFace() { // initialise the screen }

public void paint(Graphics g) { // draw onto the screen }}

extends JFrame

Page 8: Graphics and event-driven programs

public SmileyFace()

{

setTitle("Smiley Face");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setSize(250,220);

setLocation(300,300);

getContentPane().setBackground(Color.yellow);

setVisible(true);

}

Page 9: Graphics and event-driven programs

public void paint(Graphics g)

{

super.paint(g);

}

g.drawOval(85,75,75,75); g.setColor(Color.blue); g.drawOval(100,95,10,10); g.drawOval(135,95,10,10);

g.drawArc(102,115,40,25,0,-180); g.drawString("Smiley Face", 90,175);

g.setColor(Color.red);

Page 10: Graphics and event-driven programs

Event-handling in Java :The ChangingFace class

Page 11: Graphics and event-driven programs

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

public class ChangingFace extends JFrame {

public ChangingFace() { // code goes here }

public void paint(Graphics g) { // code goes here }

}

private JButton happyButton = new JButton("Smile");private JButton sadButton = new JButton("Frown");

private boolean isHappy = true;

Page 12: Graphics and event-driven programs

public ChangingFace()

{

setTitle("Changing Face");

getContentPane().setBackground(Color.yellow);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setSize(250, 200);

setLocation(300,300);

setVisible(true);

}

setLayout(new FlowLayout());add(happyButton);

add(sadButton);

Page 13: Graphics and event-driven programs

public void paint(Graphics g){ super.paint(g); g.setColor(Color.red); g.drawOval(85,45,75,75); g.setColor(Color.blue); g.drawOval(100,65,10,10); g.drawOval(135,65,10,10); g.drawString("Changing Face", 80,155); if(isHappy == true) { g.drawArc(102,85,40,25,0,-180); } else { g.drawArc(102,85,40,25,0,180); } }

Page 14: Graphics and event-driven programs
Page 15: Graphics and event-driven programs

public ChangingFace()

{

setTitle("Changing Face");

setLayout(new FlowLayout());

add(happyButton);

add(sadButton);

getContentPane().setBackground(Color.yellow);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setSize(250, 200);

setLocation(300,300);

setVisible(true);

}

happyButtonsadButton

.addActionListener( ); this.addActionListener( ); this

Page 16: Graphics and event-driven programs

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

public class ChangingFace extends JFrame {

public ChangingFace() { // code goes here } public void paint(Graphics g) { // code goes here }

}

private JButton happyButton = new JButton("Smile");private JButton sadButton = new JButton("Frown");

private boolean isHappy = true;

public void actionPerformed(ActionEvent e){ // code goes here}

implements ActionListener

import java.awt.event.*;

Page 17: Graphics and event-driven programs

public void actionPerformed(ActionEvent e)

{

if(e.getSource() == happyButton)

{

isHappy = true;

repaint();

}

if(e.getSource() == sadButton)

{

isHappy = false;

repaint();

}

}

Page 18: Graphics and event-driven programs

public class RunChangingFace

{

public static void main(String[] args)

{

new ChangingFace();

}

}

Running the application

Page 19: Graphics and event-driven programs

An interactive graphics class

Page 20: Graphics and event-driven programs

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

class PushMe extends JFrame implements ActionListener{ private JTextField myTextField = new JTextField(15); private JButton myButton = new JButton("please push me"); private JLabel myLabel = new JLabel("Enter some text and push the button", JLabel.CENTER); public PushMe() { // code goes here }

public void actionPerformed(ActionEvent e) { // code goes here }}

Page 21: Graphics and event-driven programs

public PushMe()

{

setTitle("Push Me");

setLayout(new FlowLayout());

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setSize(220,120);

setLocation(400, 300);

add(myTextField);

add(myButton);

add(myLabel);

myButton.addActionListener(this);

setVisible(true);

}

Page 22: Graphics and event-driven programs

public void actionPerformed(ActionEvent e){

}

String myText;myText = myTextField.getText();

myLabel.setText("You entered: " + myText);

myTextField

myLabel

Page 23: Graphics and event-driven programs

A GUI for the Oblong class

JTextArea

JButton

JLabel

JTextField JLabel JTextField

Page 24: Graphics and event-driven programs

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class OblongGUI extends JFrame implements ActionListener

{

private Oblong myOblong = new Oblong(0,0);

private JLabel lengthLabel = new JLabel("Length");

private JTextField lengthField = new JTextField(5);

private JLabel heightLabel = new JLabel("Height");

private JTextField heightField = new JTextField(5);

private JButton calcButton = new JButton("Calculate");

private JTextArea displayArea = new JTextArea(2,20);

// methods go here

}

Page 25: Graphics and event-driven programs

public OblongGUI(){ setTitle("Oblong GUI"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new FlowLayout()); add(lengthLabel); add(lengthField); add(heightLabel); add(heightField); add(calcButton); add(displayArea); setSize(240, 135); setLocation(300,300); calcButton.addActionListener(this); setVisible(true);}

Page 26: Graphics and event-driven programs

public void actionPerformed(ActionEvent e){ String lengthEntered = lengthField.getText(); String heightEntered = heightField.getText(); if(lengthEntered.length() == 0 || heightEntered.length() == 0) { displayArea.setText("Length and height must be entered"); } else { myOblong.setLength(Double.parseDouble(lengthEntered)); myOblong.setHeight(Double.parseDouble(heightEntered)); displayArea.setText("The area of the oblong is " + myOblong.calculateArea() + "\n" + "The perimeter of the oblong is " + myOblong.calculatePerimeter()); } }}

Page 27: Graphics and event-driven programs

A metric converter

JPanel

JPanel

JPanel

inchCmPanel

mileKmPanel

poundKgPanel

JFrame

Page 28: Graphics and event-driven programs

Layout policies - FlowLayout

Page 29: Graphics and event-driven programs

Layout policies - BorderLayout

Page 30: Graphics and event-driven programs

Compound containers

JPanel

JPanel

inchCmPanel

inchCmButtons

inchCmButtons.setLayout(new BorderLayout()); inchCmButtons.add("North", cmToInchButton);inchCmButtons.add("South", inchToCmButton);inchCmPanel.add(cmText);inchCmPanel.add(cmLabel);inchCmPanel.add(inchCmButtons);inchCmPanel.add(inchText);inchCmPanel.add(inchLabel);