Complete a program to draw circles arranged in a grid for


Loops

Complete a program to draw circles, arranged in a grid. For example, 6 circles arranged in a 2 by 3 grid would look like this:

How does it work?

The main method prompts the user for the grid parameters, the number of rows and number of columns. The remaining code in main, creates a graphic window and calls the draw method to actually complete the graphic.

The draw method should contain code that uses a loop (or loops) to draw each circle. A circle is draw using the drawOval method applied to the graphic object g. The drawOval method is defined as
drawOval(int x, int y, int width, int height)

Draws the outline of an oval. The result is a circle or ellipse that fits within the rectangle specified by the x, y, width, and height arguments.

x and y are the coordinates of the circle measured in pixels from the upper lefthand corner of the window. A suitable width and height is 40 pixels.

import java.awt.Graphics; import java.util.Scanner; import javax.swing.JFrame; import javax.swing.JComponent;

public class Lab4C {

public static void draw(Graphics g, int a, int b) {
// Your code goes here
}

public static void main(String[] args) { int a, b;
// Your code to prompt the user for the grid size - a and b JFrame frame = new JFrame();
final int FRAME_WIDTH = 400; final int FRAME_HEIGHT = 400;

frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JComponent component = new JComponent() {

public void paintComponent(Graphics graph) { draw(graph, a, b);
}
};

frame.add(component); frame.setVisible(true);
}
}

Solution Preview :

Prepared by a verified Expert
JAVA Programming: Complete a program to draw circles arranged in a grid for
Reference No:- TGS02383162

Now Priced at $20 (50% Discount)

Recommended (90%)

Rated (4.3/5)