A Simple AppletApplets and applicationsPackages and classesImporting some things we needYour first class, part 2Your first class, part 3The applet so farMethodsThe paint methodThe paint method, part 2The paint method, part 3Slide 12ColorsNew colorsSetting a colorThe paint method so farPixelsJava’s coordinate systemDrawing rectanglesDrawing stringsThe complete appletMore java.awt.Graphics methodsStill more Graphics methodsThe HTML pageDoing it in an applicationThe End1A Simple Applet2Applets and applicationsAn application is an “ordinary” programExamples: 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 browserTo run Java applets, browsers need an up-to-date Java pluginappletviewer is a program that can run appletsWhen you download the Java SDK, appletviewer comes with itappletviewer is always up-to-date with your Java systemEclipse has an built-in applet viewer3Packages and classesJava supplies a huge library of pre-written “code,” ready for you to use in your programsCode is organized into classesClasses are grouped into packagesOne way to use this code is to import itYou can import a single class, or all the classes in a packageFor this applet, you will need to import two drawing packages, awt and swing4Importing some things we needTo create an applet, you will import the JApplet classThe JApplet class is in the javax.swing packageThis is the only class we will need from the swing packageimport javax.swing.JApplet;Capitalization matters! Japplet and JApplet are different names; be careful!Since you will want to use many classes from the java.awt package, we will just import them all: import java.awt.*;5Your first class, part 2 public class Drawing extends JApplet { …}Drawing is the name of your classClass names should always be capitalizedextends JApplet says that our Drawing is a kind of JApplet, but with added capabilitiesJava’s JApplet just makes an empty “window”We are going to draw in that window6Your 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 code7The applet so farimport javax.swing.JApplet;import java.awt.*;// CIT 591 examplepublic class Drawing extends JApplet { …we still need to put some code in here...}8MethodsA method is a group of commands that tell the computer to do somethingC programmers: methods are similar to functionsA method takes information in, does something with it, and returns a resultThe input information is called the method’s parameters, or argumentsThe result is just called a result9The paint methodOur applet is going to have a method to paint some colored rectangles on the screenThis method must be named paintIn applets, this is a special method namepaint needs to be told where on the screen it can drawThis will be the only parameter it needspaint doesn’t return any result10The paint method, part 2public void paint(Graphics g) { … }public says that anyone can use this methodvoid says that it does not return a resultpaint is the name of the methodMethod names should begin with a lowercase letterThe argument, or parameter (there’s only one) is inside parenthesesThe method’s commands are inside braces11The paint method, part 3public void paint(Graphics g) { … }A Graphics (short for “Graphics context”) is an object that holds information about a paintingIt remembers what color you are usingIt remembers what font you are usingYou can “paint” on it (but it doesn’t “remember” what you have painted)In this program,The type of the parameter is GraphicsThe name of the parameter is g12The 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… }}13ColorsThe java.awt package defines a class named ColorThere 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.MAGENTA14New colorsEvery color is a mix of red, green, and blueYou can make your own colors: new Color( red , green , blue )Amounts range from 0 to 255Black is (0, 0, 0), white is (255, 255, 255)We are mixing lights, not pigmentsYellow is red + green, or (255, 255, 0)15Setting a colorTo 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 color16The paint method so far public void paint(Graphics g) { g.setColor(Color.BLUE); …draw a rectangle… g.setColor(Color.RED); …draw another rectangle… }}17PixelsA pixel is a picture (pix) elementone pixel is one dot on your screenthere are typically 72 to 90 pixels per inchjava.awt measures everything in pixels18Java’s coordinate systemJava 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)19Drawing rectanglesThere are two ways to draw rectangles:g.drawRect( left , top , width , height );g.fillRect(left , top , width , height );20Drawing stringsA 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 itg.drawString( string, left, top );For example,g.drawString("Example JApplet", 20, 80);21The complete appletimport javax.swing.JApplet;import java.awt.*;// CIT 591 examplepublic class Drawing extends JApplet { public void paint(Graphics g) { g.setColor(Color.BLUE);
View Full Document