Download presentation
Presentation is loading. Please wait.
Published byGriffin Nelson Modified over 8 years ago
1
1 Block1 – unit 2 (The Case study in Budd 5-6)
2
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
3
4
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
5 Partial AWT and Swing Class Hierarchy java.lang.Object ComponentMenuComponentCheckboxGroup ButtonCheckboxCanvasChoiceContainerLabelListScrollbarTextComponent JComponentWindow Frame JFrame Dialog JDialog PanelScrollpane Applet JApplet java.awt.* javax.swing.* JLabelJListAbstractButton JButton JPanelJScrollpane
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
7
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
8
We will use paint method of Jframe We will redefine it with our text Graphics class is defined java.awt import java.awt.* 8
9
import javax.swing.*; // Gives access to the class JFrame and its methods import java.awt.*; // Give access to Graphics class public 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
10
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
11 P(x,y) = Point(50,50) start point
12
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
13
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.
14
public class Application { public static void main(String[] args) { FirstWorld world = new FirstWorld(); world.show(); world.run(); } 14
15
15
16
16
17
import java.awt.*; public class Disk { protected Point location; // position of disk in window protected int radius; // radius of disk protected 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
18
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
19 Class: DiskData members Location (declared as a point) RadiuscolorMethods Disk (constructor) getLocationsetLocationpaint
20
import javax.swing.*; // for JFrame import java.awt.*; // for Graphics and Point 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 = 25; // set the initial values of the coordinates private int yCoord = 100; // near the top left-hand corner of the window frame // create a new point object at this specific location private Point p = new Point (xCoord, yCoord); // create a (red) disk object, of radius 15, initially at the location p private Disk d = new Disk (p, 15, Color.red); 20
21
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 point d.setLocation (p); // set the position of the disc to new coordinates given by p repaint (); } // A paint method for drawing a disk in the FirstWorld window public void paint (Graphics g) { super.paint(g); // we call the paint method of the super class this clears the screen d.paint (g); // the one we defined in the disk class } 21
22
public class Application { public static void main(String[] args) { FirstWorld world = new FirstWorld(); world.show(); world.run(); } 22
23
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
24
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
25
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
26
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
27
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
28
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
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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.