2/5/00SEM107 © Kamin & Reddy Review -1 Class 19 - Review r This lecture contains a selection of slides from previous lectures, giving the “high points”

Slides:



Advertisements
Similar presentations
2D Graphics Drawing Things. Graphics In your GUI, you might want to draw graphics E.g. draw lines, circles, shapes, draw strings etc The Graphics class.
Advertisements

Made with love, by Zachary Langley Applets The Graphics Presentation.
Lecture 2 Internet Computing Using Java Theophano Mitsa UMASS-Dartmouth.
18/2/00SEM107 - © Kamin & Reddy Class 7 - LineList - 1 Class 7 - Line Drawings  The LineList data type r Recursive methods to construct line drawings.
LAB SESSION 7 Graphical user interface Applet fundamentals Methods in applets Execution of an applet Graphics class.
Chapter 5 Programming Graphics. Chapter Goals To be able to write applications with simple graphical user interfaces To display graphical shapes such.
GUI and Swing, part 2 The illustrated edition. Scroll bars As we have previously seen, a JTextArea has a fixed size, but the amount of text that can be.
Programming and Problem Solving With Java Copyright 1999, James M. Slack Applets What is an Applet? Applet Parameters Graphics in Applets Other Applet.
CS 178: Programming with Multimedia Objects Aditya P. Mathur Professor of Computer Sciences Purdue University, West Lafayette Sept 9, 2004 Last update:
©2004 Brooks/Cole Applets Graphics & GUIs. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Graphical Programs Most applications these days are.
Chapter Day 5. © 2007 Pearson Addison-Wesley. All rights reserved2-2 Agenda Day 5 Questions from last Class?? Problem set 1 Posted  Introduction on developing.
Lecture 24 Applets. Introduction to Applets Applets should NOT have main method but rather init, stop, paint etc They should be run through javac compiler.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Outline 3.1 Introduction 3.2 Sample Applets from the Java 2.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Topics  Applets  Classes used for graphics Graphics Point Dimension.
18-Jun-15 Applets. 2 An applet is a program that is typically embedded in a Web page and can be run from a browser You need special HTML in the Web page.
1 Applets Chapter 1 To understand:  why applets are used to extend the capabilities of Web pages  how an applet is executed and know about the restrictions.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
ObjectDraw and Objects Early Chris Nevison Barbara Wells.
Java Applets. Road Map Introduction to Java Applets Review applets that ship with JDK Make our own simple applets –Introduce inheritance –Introduce the.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Java Graphics Applets.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Applets and Graphics.
26-Jun-15 Applets. 2 An applet is a Panel that allows interaction with a Java program A applet is typically embedded in a Web page and can be run from.
A Simple Applet. Applets and applications An applet is a Java program that runs on a web page –Applets can be run from: Internet Explorer Netscape Navigator.
Chapter 3 - Introduction to Java Applets Outline 3.1Introduction 3.2Thinking About Objects 3.4A Simple Java Applet: Drawing a String 3.5Two More Simple.
28-Jun-15 Applets. 2 An applet is a program that is typically embedded in a Web page and can be run from a browser You need special HTML in the Web page.
A Simple Applet.
Object Oriented Programming (OOP) LAB # 5 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet.
Java Programming, 2E Introductory Concepts and Techniques Chapter 3 Manipulating Data Using Methods.
Computer Science [3] Java Programming II - Laboratory Course Lab 6: Introduction to Java Applets Sample Applets from the Java Simple Java Applet: Drawing.
20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 16: Java Applets & AWT Fundamentals of Web Programming.
Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
Applets CS 3331 Sections 3.3 & 4.7 of [Jia03].
Chapter 17: Applets, Images, and Sound. Objectives Learn about applets Write an HTML document to host an applet Use the init() method Work with JApplet.
11/9: Recursion, Method Overloading About Scoping.java Recursion Method overloading.
Lab 6: Shapes & Picture Extended Ellipse & Rectangle Classes Stand-Alone GUI Applications.
CompSci Today’s topics Java Review Just a bunch of headings meant to trigger questions A contraction of previous notes Upcoming Midterm Exam Reading.
7/3/00SEM107- © Kamin & ReddyClass 11 - Events - 1 Class 11 - Events r A couple of odds & ends m Component sizes  switch statement r Event types r Catching.
Chapter 2: Color and Applets Coming up: Introduction to Graphics.
Program that runs in appletviewer (test utility for applets) Web browser (IE, Communicator) Executes when HTML (Hypertext Markup Language) document containing.
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 1 Lecture 4 – Event Handling r Painting r Event types r Catching different event types.
12/5/00SEM107, Kamin & ReddyReview - 34 Events Event types Catching different event types Getting information from components and events Distinguishing.
Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.
Applets Yong Choi School of Business CSU, Bakersfield.
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
Java Applet Basics (2). The Body Mass Index Calculator.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
25/2/00SEM107 - © Kamin & ReddyClass 9 - HTML - 1 Class 9 - HTML r Creating a personal web page r Structure of HTML documents r HTML tags r The APPLET.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
17/3/00SEM107 © Kamin & Reddy Class 13 - Animation 1 Class 13 - Animation r Summary of loops m while-do m for m do-while r while loop examples r for loop.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 5 Objectives  Learn about basic GUI components.  Explore how the GUI.
22/2/00 SEM107 © Kamin & Reddy Class 8 - Hilbert’s curve - 1 Class 8 - Recursive Pictures r Recursively-defined curves r The Hilbert curve.
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.
Creating a Java Application and Applet
1 Introduction to Graphics b The last one or two sections of each chapter of the textbook focus on graphical issues b Most computer programs have graphical.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
CHAPTER Agenda Applets Servelets Browsers HelloWorld.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 10.
JavaScript 101 Lesson 6: Introduction to Functions.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Will not cover Section 3.7 Thinking About Objects: Identifying.
8/2/00SEM107- © Kamin and ReddyClass 5 - Lists - 1 Class 5 - Lists r The list data type r Recursive methods on lists.
CSC 205 Programming II Lecture 5 AWT - I.
MSc/ICY Software Workshop , Semester 2
Lecture 09 Applets.
“Form Ever Follows Function” Louis Henri Sullivan
JAVA APPLET PREPARED BY Mr. Jahanzaib Ahmed
Presentation transcript:

2/5/00SEM107 © Kamin & Reddy Review -1 Class 19 - Review r This lecture contains a selection of slides from previous lectures, giving the “high points” of the material we have covered. r This selection is intended merely as an “outline” of the topics. All material covered in lecture may be included in the exam. (Exception: you will not be asked to draw a Hilbert curve.)

2/5/00SEM107 © Kamin & Reddy Review -2 Applications vs. applets r Applications: stand-alone programs, usually invoked from command line.

2/5/00SEM107 © Kamin & Reddy Review -3 Strings Strings have many instance methods provided in the Java API, e.g.  int length ()  int indexOf (String)  String substring(int, int)  String toUpperCase() r... lots more...

2/5/00SEM107 © Kamin & Reddy Review -4 Review  Class methods: call using class name and method name, e.g. Math.sin(...) In documentation, begins with word static.  Instance methods: call using value and method name, e.g. String s ; … s.substring(0,4) In documentation, does not begin with word static.

2/5/00SEM107 © Kamin & Reddy Review -5 Summary r Classes introduce types. Values with these types are called objects.  Create objects using new (exception: String ’s) r Classes have  Class methods: classname. methodname (arg’s)  Class variables: classname. variablename

2/5/00SEM107 © Kamin & Reddy Review -6 Summary (cont.) r Objects have: m Instance methods: expression. methodname (arg’s)  Instance variables: expression. variablename  Variables declared inside main (or other methods) are called local variables. These are different from class variables and instance variables, and are referred to simply by their names.

2/5/00SEM107 © Kamin & Reddy Review -7 Methods r Method = named collection of statements  Can be class method ( static ) or instance method m Has return type  Method call is statement if return type is void  Method call is expression if return type is not void

2/5/00SEM107 © Kamin & Reddy Review -8 Conditional statements A conditional statement has the form if ( condition ) statement1 else statement2 It executes statement1 if the condition is true, statement2 if the condition is false.

2/5/00SEM107 © Kamin & Reddy Review -9 Conditional expressions r A conditional expression has the form: condition ? expression1 : expression2 r It evaluates the condition and then either evaluates expression1 or expression2, depending whether condition is true or false r Note: the type of expression1 and expression2 must be the same; that type is the type of the conditional expression

2/5/00SEM107 © Kamin & Reddy Review -10 Recursive methods As of now, we lack the ability to do repetitive actions. Can solve them using recursive methods: A recursive method is one that calls itself. A recursive method is one that calls itself.

2/5/00SEM107 © Kamin & Reddy Review -11 Recursive methods in Java Recursive method f with argument x has the form static typename f ( typename x) { if (x is simple enough ) solve directly else use f( value “smaller than” x) to calculate f(x) }

2/5/00SEM107 © Kamin & Reddy Review -12 Thinking recursively Key ideas: m Know exactly what the method is supposed to do. m Assume method works for smaller values. Then just figure out how to use it to compute method for larger values. m Don’t forget base cases - small values that can be calculated directly.

2/5/00SEM107 © Kamin & Reddy Review -13 Lists Lists are a simple data structure in which data are stored in a row. We will talk first about lists of integers: x 0, x 1,..., x n-1 (n >= 0) Elements can be added and removed only at the beginning (x 0 ).

2/5/00SEM107 © Kamin & Reddy Review -14 Recursion on lists Writing recursive methods on lists follows same principle as for integers: m To compute f(L), assume f(L’) can be calculated for lists L’ smaller than L, and use f(L’) to calculate f(L). m Some lists are small enough for f to be calculated directly

2/5/00SEM107 © Kamin & Reddy Review -15 Lines We will provide a class called Line, with several operations:  Construct a line from (x0,y0) to (x1,y1) by: new Line(x0,y0,x1,y1)  Give line L, find coordinates of starting and ending points using instance methods x0(), y0(), x1(), y1().  Lines can be printed by System.out.println.

2/5/00SEM107 © Kamin & Reddy Review -16 Line lists The class LL has the same operations as IL, but contains operations on lists of Line objects:  LineList cons (Line ln, LineList L) - construct list containing i at front m nil

2/5/00SEM107 © Kamin & Reddy Review -17 Drawing lines at an angle To draw a line of length m at an angle theta from the x-axis:  m y = m sin  x = m cos  (But remember, this is upside-down...)

2/5/00SEM107 © Kamin & Reddy Review -18 Drawing “stars” (cont.) static LineList star1 (int order, int radius, double theta, double angleIncr) { if (order == 0) return LL.nil; else return LL.cons( new Line(0, 0, (int)Math.round(radius*Math.cos(theta)), (int)Math.round(radius*Math.sin(theta))), star1(order-1, radius, theta+angleIncr, angleIncr)); } (Detail: Math.cos and Math.sin take arguments in radians, instead of degrees.)

2/5/00SEM107 © Kamin & Reddy Review -19 Rotation Rotation is more complicated. Consider rotating one point around the origin by angle  : 1. Calculate m and  m =  x 2 +y 2  = tan -1 (y/x) 2.  =  +  3. (x’,y’) = point of length m, at angle  (x,y) (x’,y’)   

2/5/00SEM107 © Kamin & Reddy Review -20 Rotation (cont.) We’ll rotate shapes (i.e. LineList’s) about the origin by rotating each line: static LineList rotateShapeAboutOrigin (LineList L, double theta) { if (LL.empty(L)) return LL.nil; else return LL.cons(rotateLine(LL.hd(L), theta), rotateShapeAboutOrigin(LL.tl(L), theta)); }

2/5/00SEM107 © Kamin & Reddy Review -21 HTML r Web pages are text documents with special formatting commands, called tags. r The formatting language is called HTML. r Can view raw HTML by selecting “view source” in browser.

2/5/00SEM107 © Kamin & Reddy Review -22 The APPLET tag  Place all class files in a subdirectory, say appletcode. <APPLET CODE=“ appletname.class” CODEBASE=“appletcode” HEIGHT = 400 WIDTH = 300> mandatory stuff in here will be displayed if browser is not Java-equipped

2/5/00SEM107 © Kamin & Reddy Review -23 Writing applets The general form of an applet is: import java.applet.*; import java.awt.*; public class appletname extends Applet { public void init () {... } public void paint (Graphics g) {... }... other methods... } Usually present

2/5/00SEM107 © Kamin & Reddy Review -24 init and paint methods Most applets have these two methods (at least):  init initializes instance variables (see below).  paint performs graphics operations to draw shapes in the applet’s window r Called when applet is first displayed, and whenever it is re-displayed.  The Graphics objects is used to do the drawing

2/5/00SEM107 © Kamin & Reddy Review -25 Drawing operations The Graphics class defines numerous instance methods:  drawLine(int x0, int y0, int x1, int y1) - draw a line from (x0,y0) to (x1,y1)  drawRect(int x0, int y0, int width, int height) - draw a width x height box, with upper left corner at (x0,y0)  fillRect(int x0, int y0, int width, int height) - same as drawRect, but filled

2/5/00SEM107 © Kamin & Reddy Review -26 Example: drawing a LineList r When the paint method is called with a Graphics object, it can call other methods. The Graphics object can be used to draw in the applet window. r Draw a LineList in a Graphics object: static void drawLineList (LineList L, Graphics g) { if (!LL.empty(L)) { Line ln = LL.hd(L); g.drawLine(ln.x0(), ln.y0(), ln.x1(), ln.y1()); drawLineList(LL.tl(L), g); }

2/5/00SEM107 © Kamin & Reddy Review -27 Color  The Color class (in the java.awt package) has values that denote different colors.  Symbolic constants Color.black, Color.red, etc., are predefined.  Arbitrary colors can be created by giving RGB values in the range when constructing a Color value: m Color col1 = new Color(100,220,155); m Color darkred = new Color(100,50,50);

2/5/00SEM107 © Kamin & Reddy Review -28 Instance variables r Variables declared inside a method (the kind we’ve used so far) are either parameters or local variables.  Variables declared inside a class but outside of any method are either class variables or instance variables. (They are class variables if they are declared with the static keyword; we will consider only instance variables for now.)

2/5/00SEM107 © Kamin & Reddy Review -29 Components Some sample components:  Label: Place text in applet (alternative to drawString ): new Label(“A label”); m Button: Place button, possibly with label: new Button(); new Button(“Press me”); m TextField: Create field of given width to input text: new TextField(10);

2/5/00SEM107 © Kamin & Reddy Review -30 Placing GUI components in an applet  Normally, declare an instance variable of the component type ( Button, Checkbox, TextField, etc.)  In init m Initialize variable.  Use add method to place it in applet.  paint does not have to draw components - they are automatically drawn

2/5/00SEM107 © Kamin & Reddy Review -31 Example: A component-filled applet public class BusyApplet extends Applet { Label L = new Label(“A label”); Button b = new Button(); Button bl = new Button(“Press me”); TextField t = new TextField(10); CheckBox cb = new Checkbox(); public void init () { add(L); add(b); add(bl); add(t); add(cb); }

2/5/00SEM107 © Kamin & Reddy Review -32 Input in applets r Users enter text in TextField components r Reading the value in a TextField involves four changes to what we’ve seen: import java.awt.*; import java.applet.*; import java.awt.event.*; public class appletname extends Applet implements ActionListener { 1 2

2/5/00SEM107 © Kamin & Reddy Review -33 Input in applets (cont.) TextField t = new TextField(4); public void init () {... add(t); t.addActionListener(this); } public void actionPerformed (ActionEvent e) {... t.getText()... repaint(); } 3 4