Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java中只有中心点和半径的包围盒算法_Java_Height_Width_Box_Bounding - Fatal编程技术网

Java中只有中心点和半径的包围盒算法

Java中只有中心点和半径的包围盒算法,java,height,width,box,bounding,Java,Height,Width,Box,Bounding,我正在尝试使用fillOval命令在Java中创建一个圆。用户输入圆的中心坐标和半径,但我不知道如何找到圆角命令的高度和宽度 import java.util.*; import java.awt.*; public class Circles { public static final Scanner Console = new Scanner(System.in); public static void main(String args[]) { DrawingPanel pan

我正在尝试使用fillOval命令在Java中创建一个圆。用户输入圆的中心坐标和半径,但我不知道如何找到圆角命令的高度和宽度

import java.util.*;
import java.awt.*;

public class Circles {
  public static final Scanner Console = new Scanner(System.in);
  public static void main(String args[]) {

DrawingPanel panel = new DrawingPanel(400,300);
Graphics g = panel.getGraphics();

//print the beginning statement of the main method
System.out.println("");

//ask the user for the center and radius for the first circle
System.out.println("Enter the x-value for the center of the first circle:    ");
double x1 = Console.nextDouble();
System.out.println("Enter the y-value for the center of the first circle: ");
double y1 = Console.nextDouble();
System.out.println("Enter the radius for the first circle: ");
double rad1 = Console.nextDouble();

//calculate the height and width of the first circle


//ask the user for the center and radius for the second circle
System.out.println("Enter the x-value for the center of the second circle: ");
double x2 = Console.nextDouble();
System.out.println("Enter the y-value for the center of the second circle: ");
double y2 = Console.nextDouble();
System.out.println("Enter the radius for the second circle: ");
double rad2 = Console.nextDouble();

//calculate the height and width of the second circle


//ask the user for the center and radius for the third circle
System.out.println("Enter the x-value for the center of the third circle: ");
double x3 = Console.nextDouble();
System.out.println("Enter the y-value for the center of the third circle: ");
double y3 = Console.nextDouble();
System.out.println("Enter the radius for the third circle: ");
double rad3 = Console.nextDouble();

//calculate the height and width of the third circle


//use the fillOval method to draw each circle
//fillOval(int x, int y, int width, int height)

到目前为止,我只有这些。我只是不知道如何获得高度和宽度。

也许这个简单的小方法会帮你一点忙

/**
 * Draws a circle on any Swing Component based off of the center of the circle rather than the top left corner of
 * the rectangle that the circle resides in.<pre>
 * 
 * <b>Example Usage:</b>
 * 
 *      DrawCircle(jLabel1, Color.RED, jLabel1.getWidth()/2, jLabel1.getHeight()/2, 40, 2);
 * </pre>
 *
 * @param ctrl (Any Swing Object) The variable name of the JComponent to draw on.<br> 
 * @param color (Color) (Optional - default is the current graphics color if null is passed) The color of the line
 * which makes the circle.<br>
 * @param x (Integer) The X location (in pixels) of where the center of circle is to be located.<br>
 * @param y (Integer) The Y location (in pixels) of where the center of circle is to be located.<br>
 * @param radius (Integer) The Radius of the circle in pixels (not the diameter). Radius is: (diameter/2).<br>
 * @param thickness (Integer) The thickness of the circle line (in pixels).
 */
public static void drawMyCircle(JComponent ctrl, Color color, int x, int y, int radius, int thickness) {
    Graphics2D g2 = (Graphics2D) ctrl.getGraphics();
    int diameter = radius * 2;
    g2.setColor(color);
    g2.setStroke(new BasicStroke(thickness));
    //shift x and y by the radius of the circle in order to correctly center it
    g2.drawOval(x - radius, y - radius, diameter, diameter); 
    g2.dispose();
    ctrl.revalidate();
}
/**
*基于圆的中心而不是旋转的左上角,在任何回转组件上绘制圆
*圆所在的矩形。
* 
*用法示例:
* 
*DrawCircle(jLabel1,Color.RED,jLabel1.getWidth()/2,jLabel1.getHeight()/2,40,2);
* 
*
*@param ctrl(任何Swing对象)要绘制的JComponent的变量名。
*@param color(color)(可选-如果传递null,则默认为当前图形颜色)线条的颜色 *这就是圆。
*@param x(整数)圆心所在的x位置(以像素为单位)。
*@param y(整数)圆心所在的y位置(以像素为单位)。
*@param radius(Integer)以像素为单位的圆半径(不是直径)。半径为:(直径/2)。
*@param thickness(Integer)圆线的厚度(以像素为单位)。 */ 公共静态空白drawMyCircle(JComponent ctrl、颜色、整数x、整数y、整数半径、整数厚度){ Graphics2D g2=(Graphics2D)ctrl.getGraphics(); 内径=半径*2; g2.设置颜色(颜色); g2.设定行程(新基本行程(厚度)); //将x和y移动圆的半径,以使其正确居中 g2.绘图椭圆(x-半径,y-半径,直径,直径); g2.dispose(); ctrl.revalidate(); }

将drawOval()替换为fillOval()。

好吧,圆的直径是半径的2倍,圆的宽度和高度等于直径,对吗?你的右边。我完全忽略了这一点。我显然是想让事情变得比实际更难。非常感谢。这很有帮助。现在它更有意义了。非常感谢。