使用随机中心点的同心圆 [英] Concentric circles using a random center point

查看:184
本文介绍了使用随机中心点的同心圆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做的一个问题,我在网上找到了实践,我有麻烦找出了一步。我的目标是在使用数组作为直径打印6同心圆随机颜色。

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.

任何想法?

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]);
  }
}

这里是我的输出是什么样子:

推荐答案

您动点是最初创建的而不是圆的中间左上角。这是因为您指定的矩形中与内绘制一个椭圆fillOval(左偏移,topOffset,宽,高),并在你的程序不一样。

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.

要纠正这个:


  1. 计算已知点( X0 | Y0 ),你的情况( X中心值 | yCenter ),所以这一步已经完成

  2. 使用 fillOval(X0 - D / 2,Y0 - D / 2,D,D),其中 D 为直径

  1. calculate a fixpoint (x0|y0) in your case (xCenter|yCenter), so this step is already done
  2. use fillOval(x0 - d/2, y0 - d/2, d, d) where d is the diameter

这篇关于使用随机中心点的同心圆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆