1 / 31

INFSY 547: WEB-Based Technologies

Gayle J Yaverbaum, PhD Professor of Information Systems Penn State Harrisburg. INFSY 547: WEB-Based Technologies. OO Concepts Reviewed. Programs consist of communicating objects. Objects consist of: fields/data (values or attributes) = properties. methods= behaviors.

Download Presentation

INFSY 547: WEB-Based Technologies

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. Gayle J Yaverbaum, PhD Professor of Information Systems Penn State Harrisburg INFSY 547: WEB-Based Technologies

  2. OO Concepts Reviewed • Programs consist of communicating objects. • Objects consist of: • fields/data (values or attributes) = properties. • methods= behaviors. • Instantiation = process of creating an object.

  3. More OOP Concepts • Abstract data type (ADT) • Encapsulation • Polymorphism

  4. Java features • Portable • Multithreaded • Secure

  5. Java Graphics/speed • JAVA has very good graphics • Applets provide access to Graphical User Interface (GUI) • JAVA is fast • Better Support in recent years

  6. JAVA Development Environment Eclipse 3.2 • Review tutorial-March 1 • Use for applet development

  7. JavaApplets • Each JAVA Applet extends or inheritsthe JApplet class • Methods in JApplet class do nothing unless overriden: • init : called automatically when • applet is run • start: automatically called after init • paint: called once after init, when a panel changes, • and also after repaint () method is called • stop: called when execution is • finished • destroy:cleans up after applet • removed from memory

  8. Inheritance

  9. Business RetailBusiness ServiceBusiness KMart Macys Kinkos The child inherits characteristics of the parent: (both methods and data) is-a Note: Single inheritance in JAVA

  10. An Applet Sample Structure import javax.swing.*; //programs are derived // from javax.swing import java.awt.*; //abstract windows toolkit public class <name of your applet> extends JApplet { public void init () { } public void paint (Graphics g) { } }

  11. Lab: Applet I • CREATE A PROJECT in Eclipse (Tutorial (if review is necessary) • File/New/Project/Java/JavaProject/Next/Finish • Project Name: FirstApplet, in the appropriate space.

  12. Lab: Applet I • CREATE A Package in the project • File/New/Package • Enter a Package Name: firstapplet, in the appropriate space • Finish

  13. Lab: Applet I • Create a class within the package • Caution: Do not check any buttons on bottom of the screen • Right Click on the package name • Create a class • Enter the Name: of the class – call it FirstApplet • Finish

  14. Lab: Applet I Your screen! • package firstapplet; • public class FirstApplet { • } • Extend the class so that it will inherit methods from JApplet public class FirstApplet extends JApplet • Add two import statements after the package statement: import javax.swing.JApplet; • import java.awt.*;

  15. You will see something similar to this! package firstapplet; import java.swing.JApplet; Import java.awt.*; public class FirstApplet extends JApplet { }

  16. Add the following within the class! package firstapplet; import java.awt.*; Import javax.swing.JApplet; public class FirstApplet extends JApplet { public void init () { } public void paint (Graphics g) { } }

  17. Save your program • Right Click on FirstApplet Project • Build project • Run • Run As • Java Applet

  18. Applet Demo Explanation

  19. Add some code! (use your own string values and color) package firstapplet; import java.awt.*; Import javax.swing.JApplet; public class FirstApplet extends JApplet { public void init () { } public void paint (Graphics g) { super.paint (g); Graphics2D g2 = (Graphics2D)g g2.setColor (Color.blue); g2.drawString (“Gayle J Yaverbaum”, 20, 10); g2.drawString (“Penn State Harrisburg”, 20,25); } } Caution: DO NOT cut and paste from Powerpoint

  20. Applet Demo Explanation • init : is called automatically when applet is run • paint: is called once after init and when a repaint method is called. • super.paint (g) calls the super class inherited from JApplet. Omitting it can cause some subtle drawing errors. • paint receives the Graphics class as a parameter.

  21. Casting: to convert the object "g" to "g2" The form is (type)<object or variable> public void draw (Graphics g) { Graphics2D g2=(Graphics2D)g; }

  22. A Graphics object is passed as a parameter to paint () • setColoris a method in the Graphics class • black (default), blue, cyan, darkGray, gray, green, lightGray, magenta, orange, pink, red, white, and yellow • drawStringis a method in Graphics • Parameters are: string, x coordinate, y coordinate

  23. Reminders! • FirstAppletis name of class and must be same as the class name • By convention - class names begin with a capital letter • By convention - package names are the same name as the project name and all lower case • By convention method names begin with a lower-case letter

  24. xml file for FirstApplet <?xml version="1.0"?> <!DOCTYPE jclass SYSTEM "jclass.dtd"> <jclass> <app class="firstapplet.FirstApplet" width="300" height="250"> </app> </jclass> Of note!

  25. XSLT • <xsl:template match=“jclass”> • <xsl:for-each select="app"> • <applet> • <xsl:attribute name="code"> • <xsl:value-of select="@class"/> • </xsl:attribute> • <xsl:attribute name="width"> • <xsl:value-of select="@width"/> • </xsl:attribute> • <xsl:attribute name="height"> • <xsl:value-of select="@height"/> • </xsl:attribute> • </applet> • <br/><br/><br/> • </xsl:for-each> • </xsl template>

  26. DTD: Element with attributes <!ELEMENT jclass (app+)> <!ATTLIST app class CDATA "none" width CDATA "none" height CDATA "none" > <!ATTLIST elementnameattributenametype default CDATA means character string

  27. Reviews • Text: Chapter 7 • Reviews • A Team Home Page • A member Home Page • One Applet Page • 2..4 sub pages

  28. Extending the Application DrawClass Create another class, DrawClass, in your project package Use the same package name, firstapplet, as before Create a method, draw, in the class Draw receives the Graphics class from the calling method

  29. public class DrawClass { public void draw (Graphics g) { } }

  30. public class FirstApplet extends JApplet { DrawClass d; public void init () { d = new DrawClass (); } public void paint (Graphics g) { super.paint(g); Graphics2D g2=(Graphics2D)g; g2.setColor (Color.blue); g2.drawString ("Gayle J Yaverbaum", 50, 20); g2.drawString ("Penn State Harrisburg", 50,30); d.draw(g); } }

  31. Draw Method Ideas public void draw (Graphics g) { super.paint(g); Graphics2D g2=(Graphics2D)g; g2.setPaint (Color.red); g2.setStroke( (new BasicStroke (10.0f))); g2.draw (new Rectangle2D.Double(100, 50, 100,50)); } Sun Java Tutorial on 2D Graphics

More Related