Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.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在不使用shape类的情况下创建正方形的轮廓_Java_Eclipse_Shapes - Fatal编程技术网

使用Java在不使用shape类的情况下创建正方形的轮廓

使用Java在不使用shape类的情况下创建正方形的轮廓,java,eclipse,shapes,Java,Eclipse,Shapes,我正在尝试创建一个方形轮廓,而不使用shape类。基本上是用老式数学计算的。我有一个在这里创建一个圆: public class CircleDemo { public static void main(String[] args) { Display panel = new Display(10, 2); drawCircle(panel); } public static void drawCircle(Display panel) { int centerX = panel.getW

我正在尝试创建一个方形轮廓,而不使用shape类。基本上是用老式数学计算的。我有一个在这里创建一个圆:

public class CircleDemo {


public static void main(String[] args) {
Display panel = new Display(10, 2);
drawCircle(panel);
}

public static void drawCircle(Display panel) {
int centerX = panel.getWidth() / 2;
int centerY = panel.getHeight() / 2;

// Draw a circle starting at the top and going clock wise
double degAng = 270;
double radius = 150;
double x, y, radAng;
while ( true ) {

    radAng = ( degAng * Math.PI ) / 180;
    x = centerX + radius * Math.cos ( radAng );
    y = centerY + radius * Math.sin ( radAng );
    panel.drawNextPixel ( (int) x, (int) y );
    degAng += 0.15;
    // System.out.println ( "x = " + x + ", y = " + y );
}

}

}

你不能简单地画四条连接线,其中两条从正方形位置的左上角开始,分别延伸到长度和宽度吗?如果你要画每个像素:有4个循环,每个循环一个像素一个像素地画正方形的一侧。这种循环的一般结构是:“绘制像素,移动像素,重复”。您可以根据正在绘制的正方形的侧面移动到不同的位置。
while(true)
很可能会阻塞事件队列,阻止您的编程显示任何更新……您还可以查看一下,当它谈论曲线时,基本思想是模拟的,甚至是模拟的。基本上,该算法实际上是基于多次迭代从一点到另一点绘制直线。。。