+ All Categories
Home > Documents > Block1 – unit 2 (The Case study in Budd 5-6)

Block1 – unit 2 (The Case study in Budd 5-6)

Date post: 03-Jan-2016
Category:
Upload: ezekiel-hewitt
View: 29 times
Download: 4 times
Share this document with a friend
Description:
Block1 – unit 2 (The Case study in Budd 5-6). Objectives. create a small application that uses the Abstract Windowing Toolkit (AWT) Swing packages to simulate movement in a window based on the Java graphics model;. 1. First Program : A program that display a frame in the screen. - PowerPoint PPT Presentation
29
1 Block1 –unit 2 (The Case study in Budd 5-6)
Transcript
Page 1: Block1  – unit 2 (The Case study in  Budd 5-6)

1

Block1 –unit 2

(The Case study in

Budd 5-6)

Page 2: Block1  – unit 2 (The Case study in  Budd 5-6)

create a small application that uses the Abstract Windowing Toolkit (AWT)

Swing packages to simulate movement in a window based on the Java graphics model;

2

Page 3: Block1  – unit 2 (The Case study in  Budd 5-6)

3

Page 4: Block1  – unit 2 (The Case study in  Budd 5-6)

We will define a new class : Firstworld Firstworld will extend an existing class

Jframe◦ It inherits its features : methods and data fields

To be able to access Jframe class we have to import the package javax.swing. import javax.swing.

constructor: a method that has the name of class will be executed automatically when object is created (used for intialization)

4

Page 5: Block1  – unit 2 (The Case study in  Budd 5-6)

5

java.lang.Object

Component MenuComponentCheckboxGroup

Button CheckboxCanvas Choice Container Label List Scrollbar TextComponent

JComponent Window

Frame

JFrame

Dialog

JDialog

PanelScrollpane

Applet

JApplet

java.awt*. javax.swing*.

JLabel JListAbstractButton

JButton

JPanel JScrollpane

Page 6: Block1  – unit 2 (The Case study in  Budd 5-6)

import javax .swing.*; // Gives access to the class JFrame and its methods

public class FirstWorld extends JFrame { public FirstWorld() // constructor: method has the name of

class //will be executed automatically when object is created { setSize ( FrameWidth , FrameHeight); //these are found in jFrame

setTitle ("MY FIRST WORLD"); } public static final int FrameWidth = 600; public static final int FrameHeight = 400;//final means constant}

6

Page 7: Block1  – unit 2 (The Case study in  Budd 5-6)

public class Application{ public static void main(String[] args) { FirstWorld world = new FirstWorld(); world.show() // method defined in jframe to

show the defined frame; }}

7

Page 8: Block1  – unit 2 (The Case study in  Budd 5-6)

We will use paint method of Jframe We will redefine it with our text Graphics class is defined java.awt

import java.awt .*

8

Page 9: Block1  – unit 2 (The Case study in  Budd 5-6)

import javax .swing.*; // Gives access to the class JFrame and its methods

import java.awt .*; // Give access to Graphics classpublic class FirstWorld extends JFrame // extends to inherit

Jframe class{ public FirstWorld() { setSize ( FrameWidth , FrameHeight); setTitle ("First World"); } public static final int FrameWidth = 600; public static final int FrameHeight = 400;

public void paint(Graphics g)// method in Jframe { g.drawString ("A Simple message", FrameWidth/2 ,

FrameHeight/2); }} 9

Page 10: Block1  – unit 2 (The Case study in  Budd 5-6)

display the message at some part of the window;

wait for a few seconds; display the message at some other part of the

window.10

Page 11: Block1  – unit 2 (The Case study in  Budd 5-6)

11

P(x,y) = Point(50,50) start point

Page 12: Block1  – unit 2 (The Case study in  Budd 5-6)

import javax.swing.*;import java.awt.*;public class FirstWorld extends JFrame {

public FirstWorld () {setSize (FrameWidth, FrameHeight);setTitle ("FirstWorld");}

public static final int FrameWidth = 600;public static final int FrameHeight = 400;

private int xCoord = FrameWidth/2;private int yCoord = FrameHeight/2;

public void paint (Graphics g) {g.drawString ("A simple message", xCoord, yCoord);

}

12

FirstWorld class

Page 13: Block1  – unit 2 (The Case study in  Budd 5-6)

public void run () {try { Thread.sleep (3000); // stop the execution for 3

seconds} catch (Exception e) {System.exit (0);}//you

can //press escape for example to stop execution

xCoord = 25; // set the coordinates to new values

yCoord = 100; repaint (); // repaint the window} }

13

FirstWorld class .. Cont.

Page 14: Block1  – unit 2 (The Case study in  Budd 5-6)

public class Application{ public static void main(String[] args) { FirstWorld world = new FirstWorld(); world.show(); world.run(); }}

14

Page 15: Block1  – unit 2 (The Case study in  Budd 5-6)

15

Page 16: Block1  – unit 2 (The Case study in  Budd 5-6)

16

Page 17: Block1  – unit 2 (The Case study in  Budd 5-6)

import java.awt.*;public class Disk {

protected Point location; // position of disk in windowprotected int radius; // radius of diskprotected Color color; // colour of disk

public Disk (Point p, int r, Color c) { //constructor initialise disk

Location = p;radius = r;color = c;}

public Point getLocation () {return location;}

17

Page 18: Block1  – unit 2 (The Case study in  Budd 5-6)

public void setLocation (Point p) {location = p;}public void paint (Graphics g) {g.setColor (color);g.fillOval (location.x – radius, location.y – radius, 2*radius, 2*radius);}

}

18

Page 19: Block1  – unit 2 (The Case study in  Budd 5-6)

19

Page 20: Block1  – unit 2 (The Case study in  Budd 5-6)

import javax.swing.*; // for JFrameimport java.awt.*; // for Graphics and Pointpublic class FirstWorld extends JFrame {public FirstWorld () {setSize (FrameWidth, FrameHeight);setTitle ("FirstWorld");}public static final int FrameWidth = 600;public static final int FrameHeight = 400;private int xCoord = 25; // set the initial values of the

coordinatesprivate int yCoord = 100; // near the top left-hand corner of

the window frame// create a new point object at this specific locationprivate Point p = new Point (xCoord, yCoord);// create a (red) disk object, of radius 15, initially at the

location pprivate Disk d = new Disk (p, 15, Color.red);

20

Page 21: Block1  – unit 2 (The Case study in  Budd 5-6)

for (int i = 0; i < 10; i++) {try { Thread.sleep (500);} catch (Exception e) { System.exit(0); }

xCoord = xCoord + 25; // change the value of xCoord by a small amount

yCoord = yCoord + 25; // change the value of yCoord by a small amount

p.setLocation (xCoord, yCoord); // set new coordinates for pointd.setLocation (p); // set the position of the disc to new coordinates

given by prepaint ();}}// A paint method for drawing a disk in the FirstWorld windowpublic void paint (Graphics g) {super.paint(g); // we call the paint method of the super class this

clears the screend.paint (g); // the one we defined in the disk class}}

21

Page 22: Block1  – unit 2 (The Case study in  Budd 5-6)

public class Application{ public static void main(String[] args) { FirstWorld world = new FirstWorld(); world.show(); world.run(); }}

22

Page 23: Block1  – unit 2 (The Case study in  Budd 5-6)

public class BallWorld extends JFrame{public static void main (String [] args){

BallWorld world = new BallWorld(Color.red);

world.show(); BallWorld world2 = new

BallWorld(Color.yellow);world2.show();

for(int i = 0; i < 1000; i++){ world.run(); world2.run(); }Study P.79 multi balls in the same frame.Private Ball [ ] balls = new Ball [10];//Array of objects

23

Page 24: Block1  – unit 2 (The Case study in  Budd 5-6)

The Java event is based on the concept of listeners.

A listener is an object whose purpose is to sit and wait for an event to occur. When it is occurred the listener goes into action and perform the behavior.

Java.awt.event.* provides interfaces for listeners◦ActionListener interface (event:pressing button)

◦AdjustmentListener interface (event: moving slider)

24

Page 25: Block1  – unit 2 (The Case study in  Budd 5-6)

interface ActionListener //interface name: ActionListener { public void actionPerformed(ActionEvent); // method represents behavior which must be taken when the event

happened. } interface AdjustmentListener //interface name: AdjustmentListener { public void adustmentValueChanged(ActionEvent); // method represents behavior which must be taken

when the event happened. }

25

Page 26: Block1  – unit 2 (The Case study in  Budd 5-6)

Interface: is a description of behavior, it is a keyword such as class. It provides the method names only without implementation code.

You have to define a class that implement the interface

modifier class classname implements interfacename A class implements ActionListener interface must

contain implementation for the method public void actionPerformed(ActionEvent)

A class implements AdjustmentListener interface must contain implementation for the method

public void adustmentValueChanged(ActionEvent);

26

Page 27: Block1  – unit 2 (The Case study in  Budd 5-6)

Inner classes can be used to implement interface

Inner class: is to place a class definition within another class definition.

public Class first // Outer class { ……. Private class second //Inner class { …….} }

27

Page 28: Block1  – unit 2 (The Case study in  Budd 5-6)

Button example: (you hve to import java.awt.Toolkit for the beep)

private class FireButtonListener implements ActionListener{

public void actionPerformed(ActionEvent evt){

Toolkit.getDefaultToolkit().beep();}}

To attach the Listener to a newly created button: Button fire = new Button(“fire”) //”fire” is the button label .. Fire.addActionListener(new FireButtonListener()); // the action fire 28

Page 29: Block1  – unit 2 (The Case study in  Budd 5-6)

29

The Graphics class: java.lang.Object -> java.awt.Graphics

abstract void drawLine(int x1, int y1, int x2, int y2)          

// Draws a line, using the current color, between the points (x1, y1) and (x2, y2) in this graphics context's coordinate

abstract void drawString(String str, int x, int y)  

//Draws the text given by the specified string, using this graphics context's current font and color

abstract void fillOval(int x, int y, int width, int height)  

// Fills an oval bounded by the specified rectangle with the current color.

abstract void fillRect(int x, int y, int width, int height)   

// Fills the specified rectangle.

abstract void setColor(Color c)    

// Sets this graphics context's current color to the specified color.


Recommended