Presentation is loading. Please wait.

Presentation is loading. Please wait.

Gillian Miller Java Teacher Workshop :  Macquarie University 1Contents Advanced OO – Inheritance The Java API and documentation Strings Swing The Java.

Similar presentations


Presentation on theme: "Gillian Miller Java Teacher Workshop :  Macquarie University 1Contents Advanced OO – Inheritance The Java API and documentation Strings Swing The Java."— Presentation transcript:

1 Gillian Miller Java Teacher Workshop :  Macquarie University 1Contents Advanced OO – Inheritance The Java API and documentation Strings Swing The Java Virtual Machine Applets Graphics Buttons, Frames and Panels

2 Gillian Miller Java Teacher Workshop :  Macquarie University 2 Object Think Design a system for BlackJack Think of 5 classes and some related attributes and methods How are classes associated ?

3 Gillian Miller Java Teacher Workshop :  Macquarie University 3 Class Association - BlackJack Class Player AttributesMethods hand name kitty showHand getAction receiveCash dealHand

4 Gillian Miller Java Teacher Workshop :  Macquarie University 4Inheritance When Object Classes are similar common shared characteristics of blueprint New sub class “specializes” or inherits from previous one Base class called super class Objects of subclass contain the signature of the superclass as well as their own signature eg base Person subclasses Student, Staff, Lecturer We say Class Student extend Person { …

5 Gillian Miller Java Teacher Workshop :  Macquarie University 5

6 Gillian Miller Java Teacher Workshop :  Macquarie University 6 Methods may also overridden

7 Gillian Miller Java Teacher Workshop :  Macquarie University 7Polymorphism void doStuff(Shape s) { s.erase(); //... s.draw(); } Circle c = new Circle(); Triangle t = new Triangle(); Square s = new Square (); doStuff(c); doStuff(t); doStuff(s); Determines which method to use depending on the specific class that object is – done by system at runtime Saves lots of if statements

8 Gillian Miller Java Teacher Workshop :  Macquarie University 8API Application Programming Interface Framework for specific functionality for the application programmer Set of classes (and interfaces) Implementation details hidden In Java the application programmer creates or extends objects from the API classes uses public methods and properties to achieve goals Examples - awt, applet, swing, jdbc, …

9 Gillian Miller Java Teacher Workshop :  Macquarie University 9

10 Gillian Miller Java Teacher Workshop :  Macquarie University 10 Java API Document http://java.sun.com/j2se/1.4.1/docs/api/index.html A daunting but valuable resource Your task : Go to the above link Can you find String – Does String support a substring operation ? Find the Maths documentation (java.lang.maths) Can you find class Object. How does Object related to the other classes ?

11 Gillian Miller Java Teacher Workshop :  Macquarie University 11Strings Java provided class Strings are objects of class String String name; String lecName = “Gillian Miller”; String name1 = “Joan”; String name2 = “JOAN”; String name3 = “ xyz “; if (name1.equals (name2)) ….. if (name1.equalsIgnoreCase (name2)).. int j = name3.trim().length(); String methods equals (String val) length() charAt( int pos) trim() toLowerCase() toUpperCase() substring(int from, int len);

12 Gillian Miller Java Teacher Workshop :  Macquarie University 12 Java is Portable One of the main reasons for Java’s success ? Java is portable - ubiquitous Java sits on top of Java Virtual Machine – A JVM needs only to be written once for each major platform – So for Program code

13 Gillian Miller Java Teacher Workshop :  Macquarie University 13 Java Compilation System Library classes Compile Java Source filename.java Java Byte Codes filename.class Java Virtual Machine Interprets and executes byte code Can transfer bytecodes to another computer

14 Gillian Miller Java Teacher Workshop :  Macquarie University 14SWING Build StandAlone Applications in Java Windows, Dialog Boxes Components – text area, …. Different layouts Responding to events such as clicking boxes

15 Gillian Miller Java Teacher Workshop :  Macquarie University 15 Dialog Frame Top Level Containers - the top of the Swing Hierarchy Containers may contain other components and containers Top Level Containers add (Component comp) setSize( …) setLayout ( …)

16 Gillian Miller Java Teacher Workshop :  Macquarie University 16 Layout Managers

17 Gillian Miller Java Teacher Workshop :  Macquarie University 17Components

18 Gillian Miller Java Teacher Workshop :  Macquarie University 18COMPONENT GUI Components + Applets Examples - classes Label, TextField, TextArea, Button, Panel METHODS setBackground (Color col) setForeground (Color col) setText(String) String getText()

19 Gillian Miller Java Teacher Workshop :  Macquarie University 19 import java.awt.*; public class buttonDir extends JPanelApplet { public void init() { setLayout(new BorderLayout()); add(new Button("North"), BorderLayout.NORTH); add(new Button("South"), BorderLayout.SOUTH); add(new Button("East"), BorderLayout.EAST); add(new Button("West"), BorderLayout.WEST); add(new Button("Center"), BorderLayout.CENTER) ; } } Border Layout

20 Gillian Miller Java Teacher Workshop :  Macquarie University 20 register implements actionPerformed getSource ()

21 Gillian Miller Java Teacher Workshop :  Macquarie University 21Applets Java Byte code that can work inside a browser One of the reasons that Java took off with such Hype Applets are still fun Java also has a comprehensive set of tools for GUI stand alone applications Most graphical, dynamic effects on the Web these days are being done with JavaScript (which is not Java !) and dynamic HTML

22 Gillian Miller Java Teacher Workshop :  Macquarie University 22 c:\jdk1.3.1_01\demo\jfc\Java2D.html

23 Gillian Miller Java Teacher Workshop :  Macquarie University 23Applets Use of the Java API – applets, awt Applies inheritance and interfaces Object-driven programming at work Applets invoke from within HTML Issues with browser wars Plug-ins available from Sun, or you can run an Applet Viewer

24 Gillian Miller Java Teacher Workshop :  Macquarie University 24 My First Applet Build an applet to say “Hello World”

25 Gillian Miller Java Teacher Workshop :  Macquarie University 25

26 Gillian Miller Java Teacher Workshop :  Macquarie University 26Applet public static void init () called when applet created public static void start () called whenever applet refreshed useful for animations and threads public static void stop () whenever applet stopped PLUS methods of Panel, Component, Container etc YOU DO NOT NEED A MAIN CLASS FOR APPLET

27 Gillian Miller Java Teacher Workshop :  Macquarie University 27 Java Applet Class Hierarchy Object Component Container Panel Applet YourAppletClass

28 Gillian Miller Java Teacher Workshop :  Macquarie University 28

29 Gillian Miller Java Teacher Workshop :  Macquarie University 29

30 Gillian Miller Java Teacher Workshop :  Macquarie University 30 Graph Methods drawLine (int x1, int y1, int x2, int y2) drawRect (int x, int y, int width, int height) drawOval (int x, int y, int width, int height) fillOval (int x, int y, int width, int height) fillRect (int x, int y, int width, int height) drawString (String s, int x, int y) drawArc (int x, int y, int width, int height, int startAngle, int arcAngle) clearRect (int x, int y, int width, int height) setFont (Font fnt) setColor (Color color) many others - RoundRect, 3DRect, Polygon, Image etc

31 Gillian Miller Java Teacher Workshop :  Macquarie University 31

32 Gillian Miller Java Teacher Workshop :  Macquarie University 32

33 Gillian Miller Java Teacher Workshop :  Macquarie University 33

34 Gillian Miller Java Teacher Workshop :  Macquarie University 34

35 Gillian Miller Java Teacher Workshop :  Macquarie University 35 Your Turn 1.Practical 3 2.Create a new folder called AppletPlay 3.Copy the applets n the sample code 4.Open in BlueJ. Compile, right mouse click to run Applet. 5.All are best viewed with a size of 300 X 300 6.Play with saving as HTML then running in IE 7.Create a new applet in BlueJ 8.Play with all applets adding your own flair and touches.


Download ppt "Gillian Miller Java Teacher Workshop :  Macquarie University 1Contents Advanced OO – Inheritance The Java API and documentation Strings Swing The Java."

Similar presentations


Ads by Google