import javax.swing.*; import java.awt.*; // Abstract Windowing Toolkit: graphics stuff public class Ovals extends JApplet { public void paint (Graphics g) { drawOvals(g, 10,10); // Draw a 10x10 grid of ovals } // Draw a grid of ovals with the given number of rows and columns. // The size of the ovals changes with x and y coordinates: // * Across the x-axis, the width grows from cellWidth/cols to cellWidth // * Down the y-axiss, the width grows from cellHeight/rows to cellHeight // The color of the ovals changes with x and y coordinate: // * Across the x-axis, the blue component grows from 255/cols to 255 // * Down the y-axis, the red component grows from 255/rows to 255 // * The green component is always 0 public void drawOvals (Graphics g, int rows, int cols) { // Find the width and height of this applet int appletWidth = getWidth(); int appletHeight = getHeight(); int cellWidth = (appletWidth - (cols + 1))/cols; // subtract off grid lines int cellHeight = (appletHeight - (rows + 1))/rows; // subtract off grid lines // Draw grid lines g.setColor(Color.black); for (int x = 0; x <= appletWidth-1 ; x = x + cellWidth + 1) { g.drawLine(x, 0, x, appletWidth-1); // Vertical grid lines } for (int y = 0; y <= appletHeight-1; y = y + cellHeight + 1) { g.drawLine(0, y, appletHeight-1, y); // Horizontal grid lines } for (int row = 1; row <= rows; row++) { for (int col = 1; col <= cols; col++) { g.setColor(new Color(row * 255/rows, 0, col * 255/cols)); g.fillOval((col - 1)*(cellWidth + 1) + 1, (row - 1)*(cellHeight + 1) + 1, (col * cellWidth)/cols, (row * cellHeight)/rows); } } } }