DOC PREVIEW
Penn CIT 591 - A Simple Applet

This preview shows page 1-2-20-21 out of 21 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

A Simple AppletApplets and applicationsImporting some things we needThe applet structureThe paint methodThe paint method, part 2The paint method, part 3The applet so farColorsNew colorsSetting a colorThe paint method so farPixelsJava’s coordinate systemDrawing rectanglesDrawing stringsThe complete appletMore java.awt.Graphics methodsStill more Graphics methodsThe HTML pageThe End1A Simple Applet2Applets and applicationsAn application is an “ordinary” programExamples: Notepad, MS Word, Firefox, Halo, etc.An applet is a Java program that runs “within” another program (usually a browser)Applets can be run within any browserTo run Java applets, 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Eclipse has an built-in applet viewer3Importing some things we needTo create an applet, you will import the JApplet classThe JApplet class is in the javax.swing packageThis is the only class we will need from the swing packageimport javax.swing.JApplet;Since you will want to use many classes from the java.awt package, we will just import them all: import java.awt.*;4The applet structureimport javax.swing.JApplet;import java.awt.*;// CIT 591 examplepublic class Drawing extends JApplet { …we still need to put some code in here...}5The paint methodOur applet is going to have a method to paint some colored rectangles on the screenThis method must be named paintIn applets, this is a special method name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 result6The 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 braces7The 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 g8The applet so farimport javax.swing.JApplet;import java.awt.*;// CIT 591 examplepublic class Drawing extends JApplet { public void paint(Graphics g) { …we still need to put some code in here… }}9ColorsThe java.awt package defines a class named ColorThere are 13 predefined colors—here are their fully-qualified names:For compatibility with older programs (before the naming conventions were established), Java also allows color names in lowercase: Color.black, Color.darkGray, etc.Color.BLACK Color.PINK Color.GREENColor.DARK_GRAY Color.RED Color.CYANColor.GRAY Color.ORANGEColor.BLUEColor.LIGHT_GRAY Color.YELLOWColor.WHITE Color.MAGENTA10New 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)11Setting 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 color12The paint method so far public void paint(Graphics g) { g.setColor(Color.BLUE); …draw a rectangle… g.setColor(Color.RED); …draw another rectangle… }}13Pixels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 pixels14Java’s 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(0, 0)(0, 20)(50, 0)(50, 20)(w-1, h-1)(50, 0)(0, 0)(0, 20)(50, 20)15Drawing rectanglesThere are two ways to draw rectangles:g.drawRect( left , top , width , height );g.fillRect(left , top , width , height );16Drawing 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);17The complete appletimport javax.swing.JApplet;import java.awt.*;// CIT 591 examplepublic 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); }}18More java.awt.Graphics 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, arcWidth, arcHeight);arcWidth, arcHeight define the “roundedness” of cornersg.fillRoundRect(left, top, width, height, arcWidth, arcHeight);g.drawArc( left, top, width, height, startAngle, arcAngle);Angles are in degrees0 degrees is the 3 o’clock positionPositive angles are to the rightg.FillArc( left, top, width, height, startAngle, arcAngle);19Still more Graphics methodsg.drawPolygon(xPoints, yPoints, n);g.fillPolygon(xPoints, yPoints, n);xPoints and yPoints are int arrays of size nOne way to write an int array is: new int[] { value1, value2, ..., valueN}Example: g.drawPolygon(new int[] { 250, 290, 210 },IIIIIIIIIIIIII III new int[] { 210, 290, 290 }, 3);draws a triangle using the 3 points (250, 210), (290, 290), and (210, 290).g.drawPolyline(xPoints, yPoints, n);A “polyline” is like a polygon, except the first and last points are not automatically connectedHence, there is no “fillPolyline” method20The HTML pageYou can only


View Full Document

Penn CIT 591 - A Simple Applet

Documents in this Course
Stacks

Stacks

11 pages

Arrays

Arrays

30 pages

Arrays

Arrays

29 pages

Applets

Applets

24 pages

Style

Style

33 pages

JUnit

JUnit

23 pages

Java

Java

32 pages

Access

Access

18 pages

Methods

Methods

29 pages

Arrays

Arrays

32 pages

Methods

Methods

9 pages

Methods

Methods

29 pages

Vectors

Vectors

14 pages

Eclipse

Eclipse

23 pages

Vectors

Vectors

14 pages

Recursion

Recursion

24 pages

Animation

Animation

18 pages

Animation

Animation

18 pages

Static

Static

12 pages

Eclipse

Eclipse

23 pages

JAVA

JAVA

24 pages

Arrays

Arrays

29 pages

Animation

Animation

18 pages

Numbers

Numbers

21 pages

JUnit

JUnit

23 pages

Access

Access

18 pages

Applets

Applets

24 pages

Methods

Methods

30 pages

Buttons

Buttons

20 pages

Java

Java

31 pages

Style

Style

28 pages

Style

Style

28 pages

Load more
Download A Simple Applet
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view A Simple Applet and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view A Simple Applet 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?