gpt4 book ai didi

java - 用 Java 绘制头部和颈部。拉线问题

转载 作者:行者123 更新时间:2023-12-01 05:14:34 25 4
gpt4 key购买 nike

在 Person 类中:

  public Point[] endPointArray;
private int scalar;
private Head head;
private Neck neck;

public Person(){
scalar = 1;
endPointArray = new Point[100];
for ( int i = 0 ; i < 100 ; i++ ){
endPointArray[i] = new Point();
}
positionArray = new int[4];
point = new Point(0,0);
endPointArray[0].x = 5;
endPointArray[0].y = 5;

head = new Head(neck, endPointArray, scalar);
neck = new Neck(body, endPointArray, scalar);
}

public void draw(Graphics g){
head.draw(g);
neck.draw(g);
}

头类内部:

// ---------- CONSTANTS. ----------
private static int Base_Radius = 20;

// ---------- VARIABLE DECLARATIONS. ----------
private int radius;
private int scale; // Scale determines how large the Head will be.
private Point startingPoint; //Starting Point for the Person and the Head.
private Point endingPoint; //Ending Point, where the Neck will start from.
private Point[] endPointArray; // Contains startingPoint in position [0] by this point from Person.
private Point centre;
private Neck neck;

// ----------------------------------------
// HEAD CREATION.
// ----------------------------------------
public Head(Neck neck, Point[] endPointArray, int scale){

this.neck = neck;
this.endPointArray = endPointArray;
this.scale = scale;

radius = Base_Radius * scale;
startingPoint = new Point();
endingPoint = new Point();
centre = new Point();

// Copies the value of endPointArray[0] into startingPoint.
startingPoint.x = endPointArray[0].x;
startingPoint.y = endPointArray[0].y;

// Picks the point on the Head where the Neck will start from.
endingPoint.x = startingPoint.x + radius;
endingPoint.y = startingPoint.y + ( 2*radius );

// Assigns the value of the circles centre to the centre point.
centre.x = startingPoint.x + radius;
centre.y = startingPoint.y + radius;
// Puts the value of the endingPoint into the endPointArray[1].
endPointArray[1].x = endingPoint.x;
endPointArray[1].y = endingPoint.y;
}

// ----------------------------------------
// DRAW THE HEAD.
// ----------------------------------------
public void draw(Graphics g){
g.drawOval(startingPoint.x, startingPoint.y, radius, radius);
System.out.println("Radius : " + radius);
System.out.println("Head Starting Point : ("+startingPoint.x+","+startingPoint.y+").");
System.out.println("Head Ending Point : ("+endingPoint.x+","+endingPoint.y+").\n");
}

颈部类内部:

// ---------- CONSTANTS. ----------
private static int Base_Length = 10;

// ---------- VARIABLE DECLARATIONS. ----------
private int length;
private int scale; // Scale determines how large the Head will be.
private Point anglePoint;
private Point startingPoint; //Starting Point for the Person and the Head.
private Point endingPoint; //Ending Point, where the Neck will start from.
private Point[] endPointArray; // Contains startingPoint in position [0] by this point from Person.
private Body body;

// ----------------------------------------
// NECK CREATION.
// ----------------------------------------
public Neck(Body body, Point[] endPointArray, int scale){

this.endPointArray = endPointArray;
this.body = body;
this.scale = scale;

length = Base_Length * scale;
startingPoint = new Point();
endingPoint = new Point();
anglePoint = new Point();

// Making the startingPoint equal to the endPointArray[1].
startingPoint.x = endPointArray[1].x;
startingPoint.y = endPointArray[1].y;

// Seting the values of the point where the neck finishes.
endingPoint.x = startingPoint.x;
endingPoint.y = startingPoint.y + length;

}

// ----------------------------------------
// DRAW THE HEAD.
// ----------------------------------------
public void draw(Graphics g){
g.drawLine(startingPoint.x, startingPoint.y, endingPoint.x, endingPoint.y);
System.out.println("Neck Starting Point : ("+startingPoint.x+","+startingPoint.y+").");
System.out.println("Neck Ending Point : ("+endingPoint.x+","+endingPoint.y+").");
}

输出:

半径:20

头部起点:(5,5)。

头部终点:(25,45)。

颈部起点:(25,45)。

颈部终点:(25,55)。

头部在面板中以 5,5 绘制,并且似乎具有正确的半径。脖子与头部漂浮着一段距离。输出看起来似乎是正确的,但视觉上它是不正确的。脖子应该是从头的底部向下的一条水平线,我不知道为什么它的行为不符合我的预期。抱歉,如果这是一个新手问题,但我现在很困惑。

最佳答案

问题在于,drawOval 将宽度和高度作为参数,但您传递的是半径。您需要使用:

g.drawOval(startingPoint.x, startingPoint.y, 2 * radius, 2 * radius);

From the API :

public abstract void 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.
The oval covers an area that is width + 1 pixels wide and height + 1 pixels tall.

Parameters:
x - the x coordinate of the upper left corner of the oval to be drawn.
y - the y coordinate of the upper left corner of the oval to be drawn.
width - the width of the oval to be drawn.
height - the height of the oval to be drawn.

关于java - 用 Java 绘制头部和颈部。拉线问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11462480/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com