1 / 25

Object Oriented Programming

Learn about Java applets and applications, how to import packages and classes, and the lifecycle of an applet. Explore graphics and drawing shapes in Java.

dgovan
Download Presentation

Object Oriented Programming

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. Object Oriented Programming

  2. Topic to be covered today • Applets

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

  4. 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 toimport it • You can import a single class, or all the classes in a package

  5. The Applet Class • To create an applet, you must import the Applet class • This class is in thejava.applet package • TheAppletclass 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.

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

  7. 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.awtis probably involved!

  8. 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)

  9. 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” • Theimportdirectives can go in any order, but must be the first lines in your program

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

  11. Life Cycle of an Applet

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

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

  14. 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 • Graphicsg says gis an object of typeGraphics • We can paint ong

  15. Colors • The java.awtpackage defines a class named Color • There are 13 predefined colors--here are their fully-qualified names: Color.BLACK Color.PINK Color.GREENColor.DARK_GRAY Color.RED Color.CYANColor.GRAY Color.ORANGE Color.BLUEColor.LIGHT_GRAY Color.YELLOWColor.WHITE Color.MAGENTA

  16. 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)

  17. Setting a Color • To use a color, we tell our Graphicsg 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

  18. (0, 0) (50, 0) (50, 20) (0, 20) (w-1, h-1) Java Coordinate System • 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

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

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

  21. 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 );

  22. 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’ssetFont 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

  23. 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);

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

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

More Related