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.

Slides:



Advertisements
Similar presentations
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Advertisements

Multithreading : animation. slide 5.2 Animation Animation shows different objects moving or changing as time progresses. Thread programming is useful.
Chapter 5 Programming Graphics. Chapter Goals To be able to write applications with simple graphical user interfaces To display graphical shapes such.
Session 9 MultiballWorld, Refactoring Ball using Inheritence, and Intro. to CannonWorld.
2D Graphics in Java COMP53 Nov 14, Applets and Applications Java applications are stand-alone programs – start execution with main() – runs in JVM.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 20.1 Test-Driving the Screen Saver Application.
Learn about the types of Graphics that are available Develop a basic Graphics applet Develop a basic Graphics application Review the Java API and use.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
GUI programming Graphical user interface-based programming.
Java exercise review Lesson 25: Exercise 1, 2 and 3.
SD2071 Games Programming Abstraction, inheritance and interfaces Exceptions Two dimensional arrays Java collections framework Files Aaron Kans.
J McQuillan SE204: 2004/2005: Lecture 4slide 1 The Graphics Class Used when we need to draw to the screen Two graphics classes –Graphics –Graphics2D.
Canvas and Graphics CS 21a. 9/26/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L17: Canvas.
Objectives of This Session
CSTP FS99CS423 (cotter)1 Java Graphics java.awt.*;
© A+ Computer Science - Chicken yeller = new Chicken();
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Even-Driven Programming and basic Graphical User Interface.
Swing GUI Components You can create graphics components to place on your applet using classes available in the Swing package ( javax.swing ) Class names.
Chapter 5: Ball Worlds Features 2 classes, constant data fields, constructors, extension through inheritance, graphics.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
Creating a GUI with JFC/Swing. What are the JFC and Swing? JFC –Java Foundation Classes –a group of features to help people build graphical user interfaces.
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
BallWorld.java A structured walkthrough. Key Features: 2 classes are created Execution is done through the procedure called “main” which are decleared.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
Ch13 Creating windows and applets. Short overview AWT (Abstract Windowing Toolkit) Early Java development used graphic classesEarly Java development used.
BallWorld.java Ball.java A functional walkthrough Part 3: the interaction of objects.
1 Applications & Applets Standalone applications & Java applets Peter Mozelius DSV/UCSC.
Review_6 AWT, Swing, ActionListener, and Graphics.
CSI 3125, Preliminaries, page 1 AWT. CSI 3125, Preliminaries, page 2 AWT Java AWT (Abstract Windowing Toolkit) is an API to develop GUI or window-based.
Java Swing One of the most important features of Java is its ability to draw graphics.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 27.1 Test-Driving the Drawing Shapes Application.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
A cannon game ?. Simple version angle from command line, one shot only Coordinate system is “upside-down”: Use dy(int) method to transform y coordinate:
Graphical User Interface (GUI) Two-Dimensional Graphical Shapes.
Java exercise review Lesson 26: Exercise 4. Exercise #4 – a sample solution 1.Write the psudocode and the deployment diagram for what you are planning.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Lesson 28: More on the GUI button, frame and actions.
Session 7 More Implications of Inheritance & Chapter 5: Ball World Example.
Basic Graphics 03/03/16 & 03/07/16 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Block 1 Unit 2 Basic Constructs in Java. Objectives create a simple object using a constructor; create and display a window frame on your computer screen;
CPCS 391 Computer Graphics Lab One. Computer Graphics Using Java What is Computer Graphics: Computer graphics are graphics created using computers and,
Session 8 Lab 4 comments, MultiballWorld, Refactoring Ball using Inheritence, and Intro. to CannonWorld.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
A structured walkthrough
CSC 205 Programming II Lecture 5 AWT - I.
Java Applet What is a Java Applet? How is applet compiled?
Introduction to Graphics
Java Applets.
User Interface Programming In Java
A First Look at GUI Applications
Java Applets.
Graphical user interface-based programming
Java Applets.
Java Applets.
Advanced Programming in Java
Constructors, GUI’s(Using Swing) and ActionListner
Session 29 Introducing CannonWorld (Event-driven Programming)
Advanced GUIs and Graphics
Presentation transcript:

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 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

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 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

 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 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

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 Class: DiskData members Location (declared as a point) RadiuscolorMethods Disk (constructor) getLocationsetLocationpaint

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

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

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.