//====================================================================== // Project: CS 116, Simple 2-D graphics demo // Author: Tom Kelliher // File: Graphics2d.java // Purpose: To demonstrate some simple 2-D graphics and some // object manipulation //====================================================================== import java.applet.*; import java.awt.*; //====================================================================== // Class: Graphics2d // Purpose: Display some simple graphical elements. //====================================================================== public class Graphics2d extends Applet { //=================================================================== // Method: init // Purpose: Set background color. //=================================================================== public void init() { setBackground(Color.black); } //=================================================================== // Method: paint // Purpose: Display the graphical elements. // Input: A graphics object. //=================================================================== public void paint(Graphics g) { // Create two custom colors. Color myColor1 = new Color(200, 0, 100); Color myColor2 = new Color(0, 150, 200); // Create a rectangular box for sizing graphical elements later. Rectangle myRect = new Rectangle(50, 100, 50, 100); // Draw a filled rectangle. g.setColor(myColor1); g.fillRect(myRect.x, myRect.y, myRect.width, myRect.height); // Draw a second filled rectangle, shifted from the first. myRect.translate(100, 100); g.setColor(myColor2); g.fillRect(myRect.x, myRect.y, myRect.width, myRect.height); // Draw a filled oval, shifted yet again. myRect.translate(0, -150); g.setColor(myColor1); g.fillOval(myRect.x, myRect.y, myRect.width, myRect.height); // Shrink the rectangular box by one-half and draw an oval // outline. myRect.grow(-12, -25); g.setColor(Color.black); g.drawOval(myRect.x, myRect.y, myRect.width, myRect.height); // Draw a line. g.setColor(myColor2); g.drawLine(50, 50, 350, 350); // Draw text. g.setColor(Color.red); g.setFont(new Font("Serif", Font.ITALIC, 20)); g.drawString("The 20 Pt. italic Serif font", 20, 40); // Draw more text. g.setColor(Color.green); g.setFont(new Font("SansSerif", Font.BOLD, 30)); g.drawString("The 30 Pt. bold SansSerif font", 20, 375); } }