Java 关于使用中心坐标创建圆的输入
您好,我被分配了一个任务,我必须使用用户输入的圆心坐标和半径绘制3个圆。分配明确规定我必须输入中心和半径,我不能单独输入x和y坐标。它们必须作为坐标对(x,y)输入。 这是我的密码Java 关于使用中心坐标创建圆的输入,java,arrays,input,graphics,drawing,Java,Arrays,Input,Graphics,Drawing,您好,我被分配了一个任务,我必须使用用户输入的圆心坐标和半径绘制3个圆。分配明确规定我必须输入中心和半径,我不能单独输入x和y坐标。它们必须作为坐标对(x,y)输入。 这是我的密码 import java.util.*; import java.awt.*; public class Circles { static Scanner CONSOLE = new Scanner(System.in); public static final int PANEL_HEIGHT = 300; p
import java.util.*;
import java.awt.*;
public class Circles {
static Scanner CONSOLE = new Scanner(System.in);
public static final int PANEL_HEIGHT = 300;
public static final int PANEL_WIDTH = 400;
public static void main (String [] args) {
DrawingPanel panel = new DrawingPanel (PANEL_HEIGHT,PANEL_WIDTH);
Graphics g = panel.getGraphics();
System.out.println();
System.out.println("Red Circle data");
System.out.println("Input center of Red circle: ");
int center1 = CONSOLE.nextInt();
System.out.println("Input radius of Red circle: ");
int radius1 = CONSOLE.nextInt();
System.out.println();
System.out.println("Blue Circle data");
System.out.println("Input center of Blue circle: ");
int center2 = CONSOLE.nextInt();
System.out.println("Input radius of Blue circle: ");
int radius2 = CONSOLE.nextInt();
System.out.println();
System.out.println("Green Circle data");
System.out.println("Input center of Green circle: ");
int center3 = CONSOLE.nextInt();
System.out.println("Input radius of Green circle: ");
int radius3 = CONSOLE.nextInt();
g.setColor(Color.RED);
g.fillOval(center1 -radius1 ,center1-radius1 ,radius1 ,radius1);
g.setColor(Color.BLUE);
g.fillOval(center2 -radius2 ,center2-radius2 ,radius2 ,radius2);
g.setColor(Color.GREEN);
g.fillOval(center3 -radius3 ,center3-radius3 ,radius3 ,radius3);
}
}
我的问题是,我不知道如何正确地输入x和y坐标,并将其存储为一个int。因为它目前只需要一个数据点,将其存储,并将其用作x和y
System.out.println("Red Circle data");
System.out.println("Input center of Red circle: ");
int center1 = CONSOLE.nextInt();
在这里,我知道java必须请求并存储这些值
g.setColor(Color.RED);
g.fillOval(center1(THIS SHOULD BE THE X VALUE) -radius1 ,center1(THIS SHOULD BE THE Y VALUE)-radius1 ,radius1 ,radius1);
然后在这里,我必须以某种方式获得存储在上述代码中的值
请帮助我,我是个新手,希望能得到一些反馈!希望我所要求的有意义S/< P> <不是java编码器,所以我坚持C++,<强>你确定你想要X,Y对在单int?< /强> < /p> 1.如果是,则只需对其进行编码
- 但你必须知道坐标范围
- 例如,如果您有32位int,并且坐标适合16位,则:
int xy,x,y; xy = (x&0x0000FFFF) | ((y&0x0000FFFF)<<16); // this makes xy = (x,y)
- 末尾的if恢复负数的缺失位
- 数组:
int xy[2],x,y; xy[0]=x; xy[1]=y;
- 或结构/类
int x,y; struct point { int x,y; } xy; xy.x=x; xy.y=y;
int x,y; struct point { int x,y; } xy; xy.x=x; xy.y=y;