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.

Slides:



Advertisements
Similar presentations
Games in Python – the easy way
Advertisements

UID – Swing and Graphics Chris Coleman
COMP 110: Introduction to Programming Tyler Johnson Apr 20, 2009 MWF 11:00AM-12:15PM Sitterson 014.
Custom Painting Gestione della Grafica customizzata Vedi anche:
Chapter 16 Graphical User Interfaces
1 Applets Programming Enabling Application Delivery Via the Web.
Applets and Graphics.
Copyright 2010 by Pearson Education Building Java Programs More Graphics reading: Supplement 3G.
Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
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.
Fun with Swing Chapter 9. Overview Swing vs. AWT Creating windows and panels. Displaying formatted text in panels. Drawing graphics (lines, circles, etc.)
Made with love, by Zachary Langley Applets The Graphics Presentation.
Working with Tables for Page Design – Lesson 41 Working with Tables for Page Design Lesson 4.
Frame Windows A frame object is used to create a graphical frame window. This frame is used to show information in a graphical application. The JFrame.
Lecture 2 Internet Computing Using Java Theophano Mitsa UMASS-Dartmouth.
CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 21.
Polymorphism Method overriding Method overloading Dynamic binding 1.
Chapter 16 Graphical User Interfaces John Keyser’s Modifications of Slides by Bjarne Stroustrup
Java Applets:. How Applets differ from application?: They do not use main method but init(), start() and paint() methods of the applet class They can.
Laboratory Study II October, Java Programming Assignment  Write a program to calculate and output the distance traveled by a car on a tank of.
Graphics You draw on a Graphics object The Graphics object cannot directly be created by your code, instead one is generated when the method paintComponent.
Graphics Chapter 16.  If you want to draw shapes such as a bar chart, a clock, or a stop sign, how do you do it?
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.
LAB SESSION 7 Graphical user interface Applet fundamentals Methods in applets Execution of an applet Graphics class.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Applets Graphical Java programs Run inside web browser Platform-neutral Easy deployment--loads when needed Secure.
Gayle J Yaverbaum, PhD Professor of Information Systems Penn State Harrisburg.
Chapter 5 Programming Graphics. Chapter Goals To be able to write simple applications To display graphical shapes such as lines and ellipses To use colors.
Chapter 5 Programming Graphics. Chapter Goals To be able to write applications with simple graphical user interfaces To display graphical shapes such.
Object Oriented Programming with Java (150704).   Applet  AWT (Abstract Window Toolkit)  Japplet  Swing Types of Applet.
1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,
1 L45 Multimedia: Applets and Applications. 2 OBJECTIVES  How to get and display images.  To create animations from sequences of images.  To create.
Java Applets A lab course by Dr. Junaid Ahmed Zubairi SUNY Fredonia.
Review of Java Applets Vijayan Sugumaran Decision and Information Sciences Oakland University.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Topics  Applets  Classes used for graphics Graphics Point Dimension.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 16 Applets.
©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.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 14 Applets, Images,
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Java Graphics Applets.
Object Oriented Programming (OOP) LAB # 5 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L05 (Chapter 16) Applets.
Applets Java API.
©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 Chapter 17.  Java’s big splash onto the scene came in the mid 90’s. The people at Sun Microsystems had managed to work java programs into Web.
JAPPLET.
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.
CSE 219 Computer Science III Images. HW1 Has been posted on Blackboard Making a Game of Life with limited options.
Chapter 4: Applets and Graphics 1 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 4 Applets and Graphics.
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Review of Graphics in Java,
Java Programming Working with Sound and Images. Topics Learn about the paint() and repaint() methods Learn about paintComponent() method Use the drawString()
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
Field Trip #23 Hangman By Keith Lynn. JApplet A JApplet is a top-level container An applet is a small Java program that is executed by another program.
Painting (Chapter 12) Java Certification Study Group January 25, 1999 Mark Roth.
1 Applets are small applications that are accessed on an Internet server, transported over the internet, automatically installed and run as a part of web.
Chapter 12: Accessing the Web URL (Uniform Resource Locator) class Applet methods –for audio clips –for images –context interface.
Copyright © Curt Hill Applets A different type of program.
Creating a Java Application and Applet
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.
Introduction to Applets Chapter 21. Applets An applet is a Java application that is intended to be invoked and executed through a Web browser. Click Here.
CHAPTER Agenda Applets Servelets Browsers HelloWorld.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 10.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Jaeki Song ISQS6337 JAVA Lecture 10 Applets. Jaeki Song ISQS6337 JAVA Applet Applets run within the Web browser environment –Applets bring dynamic interaction.
Object Oriented Programming
Java Applets.
Distributed Computing, M. L. Liu
Distributed Computing, M. L. Liu
Java Applets.
APPLETS.
Presentation transcript:

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 Inserting Images l Inserting Sound l Preview: Classes & Objects

2 Changing Colors l By default, all drawing is done in black pen. However, we can change this by creating an object of class Color and passing it to the setColor method of the Graphics2D object. l Java uses the RGB color model, where colors are specified by giving the amount of the primary colors –Red, Green, and Blue- that make up the color. l The amounts are given in float values and range from 0.0F (no color present) to 1.0F (maximum color present). l The following applet draws three rectangles in 3 different colors, one of which are standard..

3 Changing Colors (Cont’d) import java.applet.Applet; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.Color; public class ColoredRectanglesApplet extends Applet { public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; Color myColor = new Color(0.25F, 0.5F, 0.75F); Rectangle rectangle1 = new Rectangle(10, 10, 70,30); Rectangle rectangle2 = new Rectangle(10, 50, 70,30); Rectangle rectangle3 = new Rectangle(10, 90, 70,30); g2.setColor(Color.blue); g2.fill(rectangle1); g2.setColor(myColor); g2.fill(rectangle2); g2.setColor(Color.red); g2.fill(rectangle3); }

4 Changing Fonts & Styles l By default, strings are drawn using the default font, plain style and default font size. l To change the font, style or size, we need to create an object of class Font and pass it to the setFont method of the Graphics2D object. l To create such object, we need to specify the following parameters: »The font name »The Style (Font.PLAIN, Font.BOLD, Font.ITALIC, or Font.BOLD + Fond.ITALIC) »The point Size

5 Changing Fonts & Styles (Cont’d) import java.applet.Applet; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Font; import java.awt.Color; public class BigFontApplet extends Applet { public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; final int size = 48; Color myColor = new Color(0.25F, 0.5F, 0.75F); Font myFont = new Font("SansSerif", Font.BOLD, size); g2.setColor(myColor); g2.setFont(myFont); g2.drawString("Applet",5,60); }

6 Applet Life Cycle l An applets must extend the Applet class defined in the java.applet package. This makes the applet to inherit the methods of the Applet class. l The Applet class provides a set of methods used by any web browser when it runs an applet:  init( ) -This is the first method called when an applet is loaded. Initialization of variables and objects is done in this method.  start( ) - This is called right after the init(), and every time the applet becomes active after it has been inactive. An applet becomes inactive when the browser window is minimized or the user leaves the applet page to another page.  paint(Graphics g) – is called when the applet's display area is covered by a menu, re-sized or when the repaint() method is called.  stop( ) - is called when the applet becomes inactive.  destroy( ) - is called when the browser closes. l By default, these methods do nothing, but we can make them do something by re-writing (overriding) them in our applets – we have been overriding paint method in previous examples.

7 Input using dialog windows l The applets we have written so far are nice, but they aren’t interactive. l Interactive applets allow the user to communicate with them through graphical interfaces such as buttons, text fields, etc. l We can dialog window to pass parameters to our applets. l To display a dialog window, we use showInputDialog() method of the JOptionPane class, which is contained in the javax.swing package. This waits for the user to enter a value and then press the OK button..

8 Input using dialog windows (Cont’d) l The problem with input dialog is that it can only read string input. Thus, if the user enters a numeric value such as 23, it treats it as the string “23”. Thus, we must use the parse method of the appropriate wrapper class to convert to a numeric value. import java.applet.Applet; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import javax.swing.JOptionPane; public class ColorSelect extends Applet { private float red; private float green; private float blue ;

9 Input using dialog windows (Cont’d) public void init() { String input; input = JOptionPane.showInputDialog("red:"); red = Float.parseFloat(input); input = JOptionPane.showInputDialog("green:"); green = Float.parseFloat(input); input = JOptionPane.showInputDialog("blue:"); blue = Float.parseFloat(input); } public void paint(Graphics g) { final int SQUARE_LENGTH = 100; Graphics2D g2 = (Graphics2D)g; Color fillColor = new Color(red, green, blue); g2.setColor(fillColor); Rectangle square = new Rectangle( (getWidth() - SQUARE_LENGTH) / 2, (getHeight() - SQUARE_LENGTH) / 2, SQUARE_LENGTH, SQUARE_LENGTH); g2.fill(square); }

10 Input using parameters l We can also supply input using the tag in the HTML file that loads the applet. l This has the form: where NAME is the parameter name and VALUE is its value. l We can have any number of PARAM tags between the tags. A Simple Program l Once parameters are passed using the PARAM tags, the getParameter() method can be used to get the values from within the applet. Again, the getParameter() method returns a string, so it needs to be converted

11 Input using parameters (cont’d) import java.applet.Applet; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; public class ColorSelect extends Applet { private Color fillColor; public void init() { float red = Float.parseFloat(getParameter("Red")); float green = Float.parseFloat(getParameter("Green")); float blue = Float.parseFloat(getParameter("Blue")); fillColor = new Color(red, green, blue); } public void paint(Graphics g) { final int SQUARE_LENGTH = 100; Graphics2D g2 = (Graphics2D)g; g2.setColor(fillColor); Rectangle square = new Rectangle( (getWidth() - SQUARE_LENGTH) / 2, (getHeight() - SQUARE_LENGTH) / 2, SQUARE_LENGTH, SQUARE_LENGTH); g2.fill(square); }

12 Inserting Images l Images of type.GIF and.JPEG can be loaded into a java applet by creating an object of class Image as follows: Image myImage = getImage(baseURL, relativeLocation); l baseURL: This is either the URL of the HTML file which loads the applet, which can be obtained using getDocumentBase() method or the URL of the applet’s.class file, which can be obtained using getCodeBase() l After the image is loaded, we display it by using the drawImage() method of the Graphics object as follows: g.drawImage(myImage, x, y, width, height, this); where x, y specifies the top left corner of the rectangular area to be used in displaying the image, width and height are the width and height to be used in displaying the image and this is the applet reference. If no width and height are specified, the actual width and height of the image are used..

13 Example Painted House l An image object has two methods getWidth(this) and getHeight(this) which can be used to get the with and height of the image. This is useful if the image need to be centered on the applet display area. this is a reference to the image object. import java.applet.Applet; import java.awt.Graphics; import java.awt.Color; import java.awt.Image; public class Images extends Applet { Image torch; public void init() { torch = getImage(getCodeBase(), "images/olympicTorch.gif"); } public void paint(Graphics g) { setBackground(Color.white); g.drawImage(torch, 5, 5, this); g.drawImage(torch, 200, 200, 100, 100, this); }

14 Playing Sound l Audio clips of type.au can be loaded and played in a java applet by creating an object of class AudioClip as follows: AudioClip myAudioClip = getAudioClip(baseURL, relativeLocation); l An audio clip object can be played by calling its play( ) or loop( ) method. l The play( ) method plays the clip only once, while the loop( ) plays the clip repeatedly until either the stop() method is called or the applet is closed. import java.awt.Graphics; import java.awt.Image; import java.applet.AudioClip; public class AudioSound extends java.applet.Applet { Image myImage; AudioClip music; public void init() { myImage = getImage(getDocumentBase(),"images/bashir.jpg"); music = getAudioClip(getDocumentBase(), "audio/spacemusic.au"); }

15 Playing Sound (Cont’d) public void paint(Graphics g) { int iWidth = myImage.getWidth(this); int iHeight = myImage.getHeight(this); int xPos = 10; // 50% g.drawImage(myImage, xPos, 10, iWidth/2, iHeight/2, this); // 100% xPos += (iWidth/2) + 10; g.drawImage(myImage, xPos, 10, this); xPos += iWidth + 10; g.drawImage(myImage, xPos, 10, iWidth + 15, iHeight + 15, this); music.loop(); }