// FirstApplet.java -- Practice making some geometrical shapes, based on // p.140-150 in the textbook -- rectangles, circles, lines and text. import java.awt.*; import java.applet.*; import java.awt.geom.*; // for ellipses/circles import javax.swing.*; // for input dialog box public class FirstApplet extends Applet { private String input; // constructor gets called first public FirstApplet() { input = JOptionPane.showInputDialog("enter a string, and I'll put it in" + " a circle: "); } // The paint() function tells what to draw every time the applet window // is refreshed. public void paint(Graphics g) { // Initialize graphics object Graphics2D g2 = (Graphics2D)g; // Create a rectangle, and make a copy 15x25 units away (p. 143-144) Rectangle box = new Rectangle(5,10,20,30); g2.draw(box); box.translate(15,25); g2.draw(box); // Make an ellipse towards lower right corner (p. 145) Ellipse2D.Double circle = new Ellipse2D.Double(200,200,75,75); g2.draw(circle); // Draw a line (also possible to draw line connecting 2 point objects // (but you can't draw a single point) p. 146 // Let's make the line blue (list of colors in book p. 147) Line2D.Double line = new Line2D.Double(250,10,10,250); g2.setColor(Color.blue); g2.draw(line); g2.fill(box); // While we're at it, color in 2nd rectangle! // To put text inside Applet, need to specify a font (p. 149-150) Font myFont = new Font("SansSarif", Font.PLAIN, 24); g2.drawString(input,205,240); } }