gpt4 book ai didi

java - 将 While 循环与 g.drawPolygon 代码结合使用

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

所以这是我并排制作一堆表格的代码。(我仍然是初学者)

import java.applet.Applet;
import java.awt.*;

public class Test extends Applet{

public void init() {
setSize(500, 225);
}

public void paint (Graphics g){

//Desk #1
int [ ] x8 = {430, 430, 351, 351};
int [ ] y8 = {200, 185, 185,200};
g.drawPolygon(x8, y8, 4);

//Desk #2
int [ ] x9 = {351, 351, 272, 272};
int [ ] y9 = {200, 185, 185, 200};
g.drawPolygon(x9, y9, 4);

//Desk #3
int [ ] x10 = {272, 272, 193, 193};
int [ ] y10 = {185, 200, 200, 185};
g.drawPolygon(x10, y10, 4);

//Desk #4
int [ ] x11 = {193, 193, 114, 114};
int [ ] y11 = {185, 200, 200, 185};
g.drawPolygon(x11, y11, 4);

//Desk #5
int [ ] x12 = {114, 114, 35, 35};
int [ ] y12 = {185, 200, 200, 185};
g.drawPolygon(x12, y12, 4);

}
}

我想要做的只是制作一个 while 循环,这样我就不需要做所有这些序列垃圾,有人可以为我制作一个有效的 while 循环代码并教我他们是如何做到的吗,我已经被这个问题困扰了很长时间了。

最佳答案

您也许可以使用内部类来存储坐标。我不确定您是否想专注于多维数组。

public class Test extends Applet {

Poly desk1 = new Poly(new int[] {430, 430, 351, 351}, new int[] {200, 185, 185,200});
Poly desk2 = new Poly(new int[] {351, 351, 272, 272}, new int[] {200, 185, 185, 200});
Poly desk3 = new Poly(new int[] {272, 272, 193, 193}, new int[] {185, 200, 200, 185});
Poly desk4 = new Poly(new int[] {193, 193, 114, 114}, new int[] {185, 200, 200, 185});
Poly desk5 = new Poly(new int[] {114, 114, 35, 35}, new int[] {185, 200, 200, 185});

Poly[] desks = new Poly[] {desk1, desk2, desk3, desk4, desk5};

public void init() {
setSize(500, 225);
}

public void paint (Graphics g) {
for (int i = 0; i < desks.length; i++) {
g.drawPolygon(desks[i].xs, desks[i].ys, 4);
}
}

private static class Poly {
// public fields are sometimes frowned upon,
// but for a private class and a simple example
public int[] xs;
public int[] ys;

public Poly(int[] xs, int[] ys) {
this.xs = xs;
this.ys = ys;
}
}

}

关于java - 将 While 循环与 g.drawPolygon 代码结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16225223/

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