I am doing a problem I found online for practice and I am having trouble figuring out a step. My goal is to print 6 concentric circles with random colors while using an array as the diameter.
I have managed to get everything working except my circles are not concentric and seem to just draw away from each other.
Any ideas?
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;
import java.awt.*;
import java.util.Random;
public class E3 {
public static int [] diameters = new int[6];
public static void main(String[] args) throws FileNotFoundException {
Scanner console = new Scanner(new File("Practice47.txt"));
int panelX = 400, panelY = 400;
DrawingPanel panel = new DrawingPanel(panelX, panelY);
panel.setBackground(Color.WHITE);
Graphics g = panel.getGraphics();
Random r = new Random();
int xCenter = r.nextInt(400);
int yCenter = r.nextInt(400);
for (int i = 0; i < diameters.length; i++) {
diameters[i]=console.nextInt();
g.setColor(new Color(r.nextInt(256),r.nextInt(256), r.nextInt(256)));
g.fillOval(xCenter, yCenter, diameters[i], diameters[i]);
}
for (int i=0;i<diameters.length;i++)
System.out.println("diameters["+i+"] = "+ diameters[i]);
}
}
Here's what my output looks like:
Your fixpoint is the top left corner initially created instead of the middle of the circles. This happens because you specify the rectangle in which to draw an oval inside with
fillOval(leftOffset, topOffset, width, height)
and not like in your program.To correct this:
x0
|y0
) in your case (xCenter
|yCenter
), so this step is already donefillOval(x0 - d/2, y0 - d/2, d, d)
whered
is the diameter