Object Oriented Programming

Slides:



Advertisements
Similar presentations
1 More on Applets Overview l Changing Colors l Changing Fonts & Styles l Applet Life-Cycle l Input using Dialog Window l Input using HTML parameters l.
Advertisements

PHY-102 SAPIntroductory GraphicsSlide 1 Introductory Graphics In this section we will learn how about how to draw graphics on the screen in Java:  Drawing.
Java Applets. 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 browser.
1 A Simple Applet. 2 Applets and applications An application is an “ordinary” program Examples: Notepad, MS Word, Firefox, Halo, etc. An applet is a Java.
©2004 Brooks/Cole Applets Graphics & GUIs. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Graphical Programs Most applications these days are.
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.
1 Introduction to Applets Overview l What is an Applet? l Steps for creating an applet l What is HTML? l Basic HTML tags l Drawing Simple Graphical shapes.
2D Graphics in Java COMP53 Nov 14, Applets and Applications Java applications are stand-alone programs – start execution with main() – runs in JVM.
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.
A Simple Applet --- Digital Clock import java.awt.*; import java.util.Calendar; public class DigitalColok extends java.applet.Applet implements Runnable.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Java Applets What is an Applet? How do you create.
Java Applets. Road Map Introduction to Java Applets Review applets that ship with JDK Make our own simple applets –Introduce inheritance –Introduce the.
Applets. 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 browser You.
APPLETS CSC 171 FALL 2004 LECTURE 6. APPLETS Graphical Java programs Run inside web browser Platform-neutral Easy deployment--loads when needed Secure.
©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.
Program Design With Methods And Graphics / Chapter 4 1 Abstract Window Toolkit.
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 Applets. Lecture Objectives  Learn about Java applets.  Know the differences between Java applets and applications.  Designing and using Java.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Java Applets What is an Applet? How do you create.
Applets CS 3331 Sections 3.3 & 4.7 of [Jia03].
Java On The Web Session 15. Memory Upload JAVA Applets Colors Fonts Drawing Methods Posting your Applet.
Applets.
JAPPLET.
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.
Abstract Window Toolkit (AWT) The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include:  A rich set.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Introduction to Applets Course Lecture Slides 29 th July 2010.
Applets & Graphics. Applets programs that run inside a browser Java platform-independence makes applets possible security restrictions: –cannot read or.
Chapter 7 Graphics. © Daly and Wrigley Objectives Use Graphic Components: ▫ Strings ▫ Lines ▫ Rectangles ▫ Ovals ▫ Arcs Change the color and font of elements.
Java Applets. 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 browser.
Applets Applet is java program that can be embedded into HTML pages. Java applets runs on the java enabled web browsers such as mozilla and internet explorer.
1 A Simple Applet. 2 Applets and applications An application is an “ordinary” program Examples: Notepad, MS Word, Firefox, Halo, etc. An applet is a Java.
Java Applets 1. What is an applet? An applet is a small Java program that is typically embedded in a Web page and can be run using the applet viewer or.
Program that runs in appletviewer (test utility for applets) Web browser (IE, Communicator) Executes when HTML (Hypertext Markup Language) document containing.
APPLET. 2 Introduction to Java Applet Programs Applications are stand alone programs ◦ executed with Java interpreter Applet is a small program ◦ can.
Applets Yong Choi School of Business CSU, Bakersfield.
Drawing rectangles and ovals in the Applet window Displaying Text in the Java Console Window Demo of the HelloAgain program Arithmetic expressions Examples.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
1 Contents Introduction Applet Vs Application Security Restrictions on Applet A simple example “Hello World!” applet Compiling & Running Applet HTML document.
1 A Simple Applet. 2 Applets and applications An applet is a Java program that runs on a web page Applets can be run within any modern browser To run.
1 Applets. 2 What is an applet? Applet: a Java program that can be inserted into a web page and run by loading that page in a browser brings web pages.
CSI 3125, Preliminaries, page 1 Applet. CSI 3125, Preliminaries, page 2 Applet An applet is a Java program that runs in a Web browser. An applet can be.
CHAPTER Agenda Applets Servelets Browsers HelloWorld.
Java Applets Getting Started. Import Files In order to run the applet properly, we have to import some files into our class. These files are: –import.
Sachin Malhotra Saurabh Choudhary
Chapter 8 Graphics.
Lecture 09 Applets.
Applets In Java Visit for more Learning Resources 1.
Java Applets.
Building Java Programs
Chapter 10 Graphics.
Java Applets.
Building Java Programs
UNIT-5.
Java Applets.
Java – Applet Class.
APPLETS.
Chapter 8 Graphics.
Applet in Java.
Java Programming COMP-417 Applet
Handout-14 Applets and Graphics
JAVA APPLET PREPARED BY Mr. Jahanzaib Ahmed
APPLET PROGRAMMING.
Presentation transcript:

Object Oriented Programming

Topic to be covered today Applets

Applets and Applications An applet is a Java program that runs on a web page Applets can be run from: Internet Explorer Netscape Navigator (sometimes) appletviewer An application is a Java program that runs all by itself

Packages and Classes Java supplies a huge library of pre-written “code,” ready for you to use in your programs Code is organized into classes Classes are grouped into packages One way to use this code is to import it You can import a single class, or all the classes in a package

The Applet Class To create an applet, you must import the Applet class This class is in the java.applet package The Applet class contains code that works with a browser to create a display window Capitalization matters! applet and Applet are different names Applet class derives from the Abstract Window Toolkit (AWT) hierarchy.

Importing the Applet Class Here is the directive that you need: import java.applet.Applet; import is a keyword java.applet is the name of the package A dot ( . ) separates the package from the class Applet is the name of the class There is a semicolon ( ; ) at the end

The java.awt Package “awt” stands for “Abstract Window Toolkit” The java.awt package includes classes for: Drawing lines and shapes Drawing letters Setting colors Choosing fonts If it’s drawn on the screen, then java.awt is probably involved!

AWT Components(Will be Covered in Next Lecture) AWT supplies the following UI components: Buttons (java.awt.Button) Checkboxes (java.awt.Checkbox) Single-line text fields (java.awt.TextField) Menus (java.awt.MenuItem) Containers (java.awt.Panel) Lists (java.awt.List)

Importing java.awt Package Since you may want to use many classes from the java.awt package, simply import them all: import java.awt.*; The asterisk, or star (*), means “all classes” The import directives can go in any order, but must be the first lines in your program

Your First Java Applet To try it Compile: javac Hello.java Test: appletviewer hello.html

Life Cycle of an Applet

OUTPUT Now Press minimize button, stop( ) method will be called init( ) method called start( ) method called after init( ) Maximizing Screen will again call start( ) method Closing the window will call destroy( ) method

Life Cycle of an Applet init(): browser calls it when applet first loaded start(): Called immediately after init( ) and again revisited after browser left page containing applet stop(): stop execution (eg. after switching to different page), Called when the browser leaves the page destroy(): Called when applet is killed (rarely used) paint(): browser tells it it’s time to redraw Called by the browser after init and start, and again whenever the browser redraws the screen This method is where user-level drawing is placed Inherited from java.awt.Container

Exploring Graphics class public void paint(Graphics g) { … } A Graphics is something that holds information about a painting It remembers what color you are using It remembers what font you are using You can “paint” on it, and it remembers what you have painted Graphics g says g is an object of type Graphics We can paint on g

Colors The java.awt package defines a class named Color There are 13 predefined colors--here are their fully- qualified names: Color.BLACK Color.PINK Color.GREEN Color.DARK_GRAY Color.RED Color.CYAN Color.GRAY Color.ORANGE Color.BLUE Color.LIGHT_GRAY Color.YELLOW Color.WHITE Color.MAGENTA

New Colors Every color is a mix of red, green, and blue You can make your own colors: new Color( red , green , blue ) Amounts range from 0 to 255 Black is (0, 0, 0), white is (255, 255, 255) We are mixing lights, not pigments Yellow is red + green, or (255, 255, 0)

Setting a Color g.setColor(Color.RED); To use a color, we tell our Graphics g what color we want: g.setColor(Color.RED); g will remember this color and use it for everything until we tell it some different color

Java Coordinate System (0, 0) (0, 20) (50, 0) (50, 20) (w-1, h-1) Java uses an (x, y) coordinate system (0, 0) is the top left corner (50, 0) is 50 pixels to the right of (0, 0) (0, 20) is 20 pixels down from (0, 0) (w - 1, h - 1) is just inside the bottom right corner, where w is the width of the window and h is its height

Drawing Rectangles There are two ways to draw rectangles: g.drawRect( left , top , width , height ); g.fillRect(left , top , width , height );

Example import java.applet.Applet; import java.awt.*; public class Drawing extends Applet { public void paint(Graphics g) { g.setColor(Color.BLUE); g.fillRect(20, 20, 50, 30); g.setColor(Color.RED); g.fillRect(50, 30, 50, 30); } }

Some More java.awt Methods g.drawLine( x1 , y1 , x2 , y2 ); g.drawOval( left , top , width , height ); g.fillOval( left , top , width , height ); g.drawRoundRect( left , top , width , height ); g.fillRoundRect( left , top , width , height ); g.drawArc( left , top , width , height , startAngle , arcAngle );

Graphics Font setFont, getFont Specifies the font to be used for drawing text Setting the font for the Graphics object does not persist to subsequent invocations of paint Set the font of the window (I.e., call the applet’s setFont method) for permanent changes to the Graphics object In JDK 1.1, only 5 fonts are available: Serif (aka TimesRoman), SansSerif (aka Helvetica), Monospaced (aka Courier), Dialog, and DialogInput

Drawing Strings A String is a sequence of characters enclosed in double quote marks "Hello, World!" A double quote mark in a String must be preceded by a backslash ( \ ) "He said, \"Please don't go.\"" g.drawString( string , x , y );

Graphics Behavior Browser calls repaint method to request redrawing of applet Called when applet first drawn or applet is hidden by another window and then re-exposed

Assignment Search awt Method for loading an Image. Also find Graphics Method for drawing that image in Applet window and write instructions for loading and drawing an Image using Applet viewer Also explore how to play an audio file