Upload
ezekiel-hewitt
View
31
Download
4
Embed Size (px)
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
Citation preview
1
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
3
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
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
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
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
We will use paint method of Jframe We will redefine it with our text Graphics class is defined java.awt
import java.awt .*
8
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
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
11
P(x,y) = Point(50,50) start point
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
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.
public class Application{ public static void main(String[] args) { FirstWorld world = new FirstWorld(); world.show(); world.run(); }}
14
15
16
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
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
19
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
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
public class Application{ public static void main(String[] args) { FirstWorld world = new FirstWorld(); world.show(); world.run(); }}
22
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
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
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
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
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
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
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.