1 / 29

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

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.

Download Presentation

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

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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;

  3. 1. First Program : A program that display a frame in the screen

  4. To build our program • 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)

  5. Partial AWT and SwingClass Hierarchy java.lang.Object CheckboxGroup Component MenuComponent Button Canvas Checkbox Choice Container Label List Scrollbar TextComponent JComponent Scrollpane Panel Window AbstractButton JLabel JList JPanel JScrollpane Applet Dialog Frame JButton JApplet JDialog JFrame java.awt.* javax.swing.*

  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 }

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

  8. 2. Second program :To add text inside the frame • We will use paint method of Jframe • We will redefine it with our text • Graphics class is defined java.awt import java.awt.*

  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); } }

  10. 3. Third Program : Modification of program : places two copies of the same object on different parts of the window at different times • display the message at some part of the window; • wait for a few seconds; • display the message at some other part of the window.

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

  12. FirstWorld class 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); }

  13. FirstWorld class .. Cont. 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 } }

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

  15. 4. Program: Animated ball moves in window

  16. A disk in a window frame

  17. Disk class 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; }

  18. Disk class .. cont 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); } }

  19. Modification for FirstWorld class 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. Modification for FirstWorld class…cont. for (inti = 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. Application class (main program) public class Application { public static void main(String[] args) { FirstWorld world = new FirstWorld(); world.show(); world.run(); } }

  22. 5. Having two different frames 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. Java event • 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. listener interfaces 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 FireButtonListenerimplements ActionListener{ public void actionPerformed(ActionEventevt){ 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. 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.

More Related