A Simple Applet.

Slides:



Advertisements
Similar presentations
Lecture 2 Internet Computing Using Java Theophano Mitsa UMASS-Dartmouth.
Advertisements

Laboratory Study II October, Java Programming Assignment  Write a program to calculate and output the distance traveled by a car on a tank of.
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.
Java Graphics Section 1 - Multi-File Graphics Programs Section 2 - The Coordinate System and Graphics Context g Section 3 - The Java Drawing and Painting.
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.
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.
©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.
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.
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.
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.
Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
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.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
1 Graphical objects We will draw graphics in Java using 3 kinds of objects: DrawingPanel : A window on the screen. Not part of Java; provided by the authors.
Java Applets. Lecture Objectives  Learn about Java applets.  Know the differences between Java applets and applications.  Designing and using Java.
Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet.
©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 On The Web Session 15. Memory Upload JAVA Applets Colors Fonts Drawing Methods Posting your Applet.
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.
Lecture 15: Intro to Graphics Yoni Fridman 7/25/01 7/25/01.
Chapter 7 Graphics. © Daly and Wrigley Objectives Use Graphic Components: ▫ Strings ▫ Lines ▫ Rectangles ▫ Ovals ▫ Arcs Change the color and font of elements.
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.
Graphics Copyright © 2015 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. Java Methods Object-Oriented Programming and Data.
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.
Lec.10 + (Chapter 8 & 9) GUI Java Applet Jiang (Jen) ZHENG July 6 th, 2005.
Graphic Basics in C ATS 315. The Graphics Window Will look something like this.
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.
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.
Creating a Java Application and Applet
CS305j Introduction to Computing Simple Graphics 1 Topic 11 Simple Graphics "What makes the situation worse is that the highest level CS course I've ever.
Introduction to Graphics. Graphical objects To draw pictures, we will use three classes of objects: –DrawingPanel : A window on the screen. –Graphics.
Lec 15 Writing an Applet Class. Agenda Writing an Applet class Java Graphics class.
Copyright 2008 by Pearson Education Building Java Programs Graphics reading: Supplement 3G videos: Ch. 3G #1-2.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Will not cover Section 3.7 Thinking About Objects: Identifying.
Basic Graphics 03/03/16 & 03/07/16 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
CPCS 391 Computer Graphics Lab One. Computer Graphics Using Java What is Computer Graphics: Computer graphics are graphics created using computers and,
Graphics JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight.
Chapter 8 Graphics.
Lecture 09 Applets.
Object Oriented Programming
Building Java Programs
Building Java Programs
Building Java Programs
Chapter 10 Graphics.
Basic Graphics Drawing Shapes 1.
Building Java Programs
Java Applets.
Chapter 8 Graphics.
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Graphics Reading: Supplement 3G
Presentation transcript:

A Simple Applet

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 modern Java applets, old browsers need an up-to-date Java plugin appletviewer is a program that can run applets When you download the Java SDK, appletviewer comes with it appletviewer is always up-to-date with your Java system 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

AWT and Swing There are two ways to build Graphical User Interfaces (GUIs): AWT, the “Abstract Window Toolkit,” is the original way Swing is the new way (since Java 1.2) Swing is built “on top of” the AWT When you build an AWT GUI, you use only AWT methods When you build a Swing GUI, you typically use both AWT methods and Swing methods However, there are some incompatibilities you will need to learn about (later)

The Applet and JApplet classes To create an applet, you must import either the Applet class (to write an AWT applet) or the JApplet class (to write a Swing applet) The Applet class in in the java.awt package The JApplet class is in the javax.swing package Both Applet and JApplet contain code that works with a browser to create a display window We will use Swing (hence, JApplet) in this example Capitalization matters! Japplet and JApplet are different names

Importing the JApplet class Here is the directive that you need: import javax.swing.JApplet; import is a keyword javax.swing is the name of the package A dot ( . ) separates the package from the class JApplet is the name of the class There is a semicolon ( ; ) at the end This directive should be before your class definitions Only the package directive, if you have one, goes before the import directives

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 Swing builds on the AWT, it doesn’t replace it If it’s drawn on the screen, then java.awt is probably involved!

Importing the 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 ...unless you have a package directive, which must precede your import directives

C and C++ programmers only C and C++ have an #include directive that copies a library function into your program This makes your program bigger Java’s import gives you access to the library It does not make your program bigger It’s OK to use lots of import directives!

The applet so far import javax.swing.JApplet; import java.awt.*;

Comments A comment adds information for the reader Java ignores everything inside comments There are three kinds of comments: // starts a comment that goes to the end of the line /* starts a comment that can extend over many lines, and ends at */ /** is a “javadoc” comment that can be extracted from your program and used in documentation */

Classes In Java, all code occurs in classes Except for the package and import directives We will talk about package some day The code that you import is in classes Your code will also be in classes For now, a class is a bundle of code We will talk about what it really is very soon

Your first class public class Drawing extends JApplet { …the code for your class goes in here… } public says your class is not hidden This makes your class visible to “the whole world” We will talk later about why we hide code class says we are making a class (Duh!)

Your first class, part 2 public class Drawing extends JApplet { … } Drawing is the name of your class Class names should always be capitalized extends JApplet says that our Drawing is a kind of JApplet, but with added capabilities Java’s JApplet just makes an empty window We are going to draw in that window The only way to make an applet is to extend Applet or JApplet

Your first class, part 3 public class Drawing extends JApplet { …the code for your class goes in here… } The braces, { }, mark the beginning and ending of your code

The applet so far import javax.swing.JApplet; import java.awt.*; // CIT 591 example public class Drawing extends JApplet { …we still need to put some code in here... }

Methods A method is a group of commands that tell the computer to do something C programmers: methods are similar to functions A method takes information in, does something with it, and returns a result The input information is called the method’s parameters, or arguments The result is just called a result

The paint method Our applet is going to have a method to paint some colored rectangles on the screen This method must be named paint paint needs to be told where on the screen it can draw This will be the only parameter it needs paint doesn’t return any result

The paint method, part 2 public void paint(Graphics g) { … } public says that anyone can use this method void says that it does not return a result paint is the name of the method Method names should begin with a lowercase letter The argument, or parameter (there’s only one) is inside parentheses The method’s commands are inside braces

By the way…names ( ) are parentheses { } are braces [ ] are brackets

The paint method, part 3 public void paint(Graphics g) { … } A Graphics (short for “Graphics context”) is an object 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 (but it doesn’t remember what you have painted) In this program, The type of the parameter is Graphics The name of the parameter is g

The applet so far import javax.swing.JApplet; import java.awt.*; // CIT 591 example public class Drawing extends JApplet { public void paint(Graphics g) { …we still need to put some code in here… } }

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 For compatibility with older programs (before the naming conventions were established), Java also allows color names in lowercase: Color.black, Color.darkGray, etc.

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

The paint method so far public void paint(Graphics g) { g.setColor(Color.BLUE); …draw a rectangle… g.setColor(Color.RED); …draw another rectangle… } }

Pixels A pixel is a picture (pix) element one pixel is one dot on your screen there are typically 72 to 90 pixels per inch java.awt measures everything in pixels

Java’s 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 );

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!\" " To draw a string, you need to specify not only what you want to say, but where to say it g.drawString( string, left, top ); For example, g.drawString("Example JApplet", 20, 80);

The complete applet import javax.swing.JApplet; import java.awt.*; // CIT 591 example public class Drawing extends JApplet { 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); g.setColor(Color.BLACK); g.drawString("Example JApplet", 20, 80); } }

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 ); g.drawPolygon( xPoints, yPoints, n ); g.fillPolygon( xPoints, yPoints, n ); xPoints and yPoints are int arrays of size n

The HTML page You can only run an applet from an HTML page The HTML looks something like this: <html> <body> <h1>Drawing Applet</h1> <applet code="Drawing.class" width="100" height="150"> </applet> </body> </html> BlueJ will create this HTML for you

The End