import java.applet.Applet; import java.awt.Graphics; import java.awt.Color; import java.awt.Font; public class GraphicsDrawShapesWithApplet extends Applet { // With no 'main' method, this 'paint' method is called automatically public void paint(Graphics g) { // Set the size of the applet window (canvas) this.setSize(400, 300); // Set the background color of the canvas to white this.setBackground(Color.WHITE); // Draw a rectangle in black g.setColor(Color.BLACK); g.drawRect(10, 10, 100, 150); // Fill in the rectangle with red; note that the values have // been adjusted slightly to avoid covering the border g.setColor(Color.RED); g.fillRect(11, 11, 99, 149); // Draw a triangle in blue g.setColor(Color.BLUE); g.drawLine(160, 10, 250, 120); g.drawLine(250, 120, 380, 10); g.drawLine(160, 10, 380, 10); // Fill in the triangle with yellow by creating two integer // arrays to form a three-point polygon to match the lines // of the triangle g.setColor(Color.YELLOW); int[] xPoints = {162, 379, 250}; int[] yPoints = {11, 10, 119}; g.fillPolygon(xPoints, yPoints, 3); // Create a custom color (brown); the format for setting the // color is (R, G, B), with each value being an integer in // the range from 0 to 255 (R = Red, G = Green, B = Blue) Color brown = new Color(124, 0, 0); // Draw an oval in brown g.setColor(brown); g.drawOval(180, 150, 150, 75); // Display some text in gray and blue g.setColor(Color.GRAY); g.setFont(new Font("Arial", Font.BOLD, 20)); g.drawString("Graphics are fun...", 30, 260); g.setColor(Color.BLUE); g.setFont(new Font("Arial", Font.BOLD, 12)); g.drawString("Yeah!", 320, 300); } }