A Simple Applet (11/17/04) Outline of an applet that creates a drawing: An HTML file with an applet tag. A single class that extends Applet class. An HTML file can have multiple applets. File RectangleApplet.html <p>Here is my <i>first applet</i>:</p> <applet code="RectangleApplet.class" width="300" height="300"> </applet> An applet is like any other program in Java. The class is declared public, and it extends Applet. The RectangleApplet inherits the behavior of Applet class. import java.applet.Applet; public class RectangleApplet extends Applet { public void paint(Graphics g) { …… } } Applets do not have a main method Applets implement the method: paint Paint method receives an object of type Graphics Graphics class is the first version of Java, not object-oriented approach. Graphics2D class extends Graphics class. We always convert the Graphics object to an object of Graphics2D class. Recover Graphics2D reference by using a cast in order to use the more sophisticated 2D graphics. In the package of java.awt (Abstract Windowing Toolkit) import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; Three steps to draw a graph: 1. Recover Graphics2D 1 Graphics2D g2 = (Graphics2D)g; 2. Construct the object of Graphic2D Rectangle cerealBox = new Rectangle(5, 10, 20, 30); 3. Draw the object of Graphic2D g2.draw(cerealBox); import java.applet.Applet; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; /** An applet that draws two rectangles. */ public class RectangleApplet extends Applet { public void paint(Graphics g) { // recover Graphics2D Graphics2D g2 = (Graphics2D)g; // construct a rectangle and draw it Rectangle cerealBox = new Rectangle(5, 10, 20, 30); g2.draw(cerealBox); // move rectangle 15 units sideways and 25 units down cerealBox.translate(15, 25); // draw moved rectangle g2.draw(cerealBox); } } Graphical Shapes Ellipse Ellipse2D.Float, Ellipse2D.Double (more convenient) 2 Ellipse2D.Double easterEgg = new Ellipse2D.Double(x-Coordinate, y-Coordinate, width, heighth); Ellipse2D.Double is inner class, while Ellipse2D is outer class Use Ellipse2D.Double as class name to create object, while import java.awt.geom.Ellipse2D; Rectangle class use integer coordinate, while Rectangle.Double uses Ellipse2D.Double circle = new Ellipse2D.Double(x-coordinate, y-coordinate, diameter, diameter); Line2D.Double segment = new Line2D.Double (x1, y1, x2, y2); Point2D.Double from = new Point2D.Double(x1, y1); Point2D.Double to = new Point2D.Double(x2, y2); Line2D.Double segment = new Line2D.Double (from, to); For thicker lines G2.setStroke(new BasicStroke(4.0F)); // effective to all shapes after this call Colors Change the color of pen: Color magenta = new Color(1.0F, 0.0F, 1.0F); There primary colors ends with a suffix F, and value ranges from [0.0F, 1.0F]. Some predefined colors are available: Color.red, Color.pink… Usage: Color magenta = new Color(1.0F, 0.0F, 1.0F); g2.setColor(Color.red); g2.draw(cerealBox); Color inside the shape: g2.setColor(Color.red); g2.fill(cerealBox); (11/22/04) 1. Demo rectangles 2. Demo Ellipse 3. Demo Circle 4. Demo Color 5. Demo Font Fonts 3 Constructor: Font(String name, int style, int size) Usage: Font hugeFont = new Font(“Serif”, Font.Bold, HUGE_SIZE); g2.setFont(hugeFont); g2.drawString(“Hello”, 50, 100); 6. Explain CarApplet.java 11/24/2004 1. Explain the homework 2. Constructor and required packages for learned graphics import java.applet.Applet; // for applet import java.awt.Graphics; //for paint import java.awt.Graphics2D; // to recover object of Graphics2D import java.awt.Rectangle Rectangle(int x, int y, int width, int height) Constructs a new Rectangle whose top-left corner is specified as (x, y) and whose width and height are specified by the arguments of the same name. import java.awt.geom.Rectangle2D Rectangle2D.Double(double x, double y, double w, double h) Constructs and initializes a Rectangle2D from the specified double coordinates. Rectangle2D.Float(float x, float y, float w, float h) Constructs and initializes a Rectangle2D from the specified float coordinates. import java.awt.geom.Ellipse2D Ellipse2D.Double(double x, double y, double w, double h) Constructs and initializes an Ellipse2D from the specified coordinates. 4 Ellipse2D.Float(float x, float y, float w, float h) Constructs and initializes an Ellipse2D from the specified coordinates. import java.awt.geom.Line2D Line2D.Double(double X1, double Y1, double X2, double Y2) Constructs and initializes a Line2D from the specified coordinates. Line2D.Double(Point2D p1, Point2D p2) Constructs and initializes a Line2D from the specified Point2D objects. Line2D.Float(float X1, float Y1, float X2, float Y2) Constructs and initializes a Line from the specified coordinates. Line2D.Float(Point2D p1, Point2D p2) Constructs and initializes a Line2D from the specified Point2D objects. import java.awt.geom.Point2D Point2D.Double(double x, double y) Constructs and initializes a Point2D with the specified coordinates. Rectangle2D.Double() Constructs a new Rectangle2D, initialized to location (0, 0) and size Point2D.Float(float x, float y) Constructs and initializes a Point2D with the specified coordinates. (0, 0). import java.awt.Color Color(float r, float g, float b) Creates an opaque sRGB color with the specified red, green, and blue values in the range (0.0 - 1.0). import java.awt.Font Font(String name, int style, int size) Creates a new Font from the specified name, 3. style and point size. Color Exmple Color(float r, float g, float b) values in the range (0.0 - 1.0). import java.applet.Applet; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import javax.swing.JOptionPane; /** 5 An applet that lets a user choose a color by specifying the fractions of red, green, and blue. */ public class ColorApplet extends Applet { public ColorApplet() { String input; // ask the user for red, green, blue values input = JOptionPane.showInputDialog("red:"); float red = Float.parseFloat(input); input = JOptionPane.showInputDialog("green:"); float green = Float.parseFloat(input); input = JOptionPane.showInputDialog("blue:"); float blue = Float.parseFloat(input); fillColor = new Color(red, green, blue); } public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; // select color into graphics context //g2.setColor(Color.red); g2.setColor(fillColor); // construct and fill a square whose center is // the center of the window Rectangle square = new Rectangle( (getWidth() - SQUARE_LENGTH) / 2, (getHeight() - SQUARE_LENGTH) / 2, SQUARE_LENGTH, SQUARE_LENGTH); 6 g2.fill(square); } private static final int SQUARE_LENGTH = 100; private Color fillColor; } To make the applet interactive. Here we use JOptionPane.showInputDialog method. Place any calls to the showInputDialog method into the applet constructor, not the paint method. Why? In the constructor, when the applet is first displayed, the user is prompted for input once. But in the paint, the user is prompted for new input every time the applet is repainted. To get another chance to supply input select “reload” or “refresh” from the browser or applet viewer. Alternative Choice: supply inputs from the HTML file by the param tag <param name=”…” value = “…” > <applet code = “ColorApplet.class” width = “300” height = “300”> <param name = “Red” value= “1.0” /> <param name = “Green” value= “0.7” /> <param name = “Blue” value = “0.7” /> </applet> Read input either in the paint method or in the special init method public void init() { float r = Float.parseFloat(getParameter(“Red”)); float g = Float.parseFloat(getParameter(“Green”)); float b = Float.parseFloat(getParameter(“Blue”)); fillColor = new Color(r, g, b); } 4. Font Example Font(String name, int style, int size) name in {“Serif”, “SansSerif”, “Monospaced”, “Dialog”, “DialogInput”} 7 style in {Font.PLAIN, Font.BOLD, Font.ITALIC, Font.BOLD+Font.ITALIC} point size in {18, 20} or small, medium, large, huge import java.applet.Applet; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.font.FontRenderContext; import java.awt.geom.Line2D; import java.awt.geom.Rectangle2D; /** This applet draws a string that is centered in the applet window. getWidth and getHeight methods return the applet size in pixels. */ public class FontApplet extends Applet { public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; // select the font into the graphics context final int HUGE_SIZE = 48; Font hugeFont = new Font("Serif", Font.BOLD, HUGE_SIZE); g2.setFont(hugeFont); String message = "Applet"; g2.drawString(message, 50, 100); } } Example: Even.java /* Abstract: This program computes the sum of even integers from 1 to 10 */ import java.applet.Applet; import java.awt.*; public class Even extends Applet { public void paint( Graphics g ) 8 { int sum = 0; for ( int x = 2; x <= 10; x += 2 ) sum += x; g.drawString( "The sum is " + sum, 25, 25 ); } } 5. Comparing Visual and Numerical Information -- Intersection 9