2013-04-25 89 views
0

所以这是我并排制作了一堆表侧的代码。(我还是个初学者)使用while循环利用g.drawPolygon代码

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循环代码,并教我如何做到这一点,我一直在这一点上卡住了很长时间时间。

+0

使用二维数组和嵌套循环。 – 2013-04-25 21:44:16

回答

0

而不必为每个坐标的单阵列,为什么不能有这样的事情:

int[][] x = { 
    {123, 534, 643}, 
    {123, 543, 152}, 
    ... 
    {543, 125, 163} 
}; 

int[][] y = { 
    {123, 534, 643}, 
    {123, 543, 152}, 
    ... 
    {543, 125, 163} 
}; 

现在你只需要遍历这些使用循环。请记住,这里的隐含假设似乎是x和y都有相同的坐标数,所以您应该能够同时迭代它们两个,以获得相应的xy's。

问这么写你的代码你可能不会工作,因为我们不真的在这里给代码,除非你显示你的重要工作。

0

我不会给你答案,但这可能有帮助。创建一个二维数组int [] [],其大小为[4] [4],x和[4] [4]为y。

然后你可以创建一个while循环遍历数组。即使是for循环也足够好。

像这样

for(int i = 0 ; i < 4; i++) { 
    g.drawPolygon(x[i], y[i]); 
} 

1

也许你可以使用一个内部类来存储COORDS。我不确定你是否想专注于多维数组。

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; 
     } 
    } 

} 
0

下面是使用简单的数学计算坐标的替代解决方案。

public class Test extends Applet { 
    public void init() { 
     setSize(500, 225); 
    } 

    public void paint(Graphics g) { 
     int deskCount = 5; 
     int[] y = { 200, 185, 185, 200 }; 
     for (int i = 0; i < deskCount; i++) { 
      int[] x = { 430 - i * 79, 430 - i * 79, 351 - i * 79, 351 - i * 79 }; 
      g.drawPolygon(x, y, 4); 
     } 
    } 
} 
0

感谢大家的工作,它确实帮助,所以,谢谢你,最后的家伙,for循环是真棒方式也不过在那个时候,我设法找到了如何通过自己与while循环,你这样做见下文! :)

//Desk #1-4 #Using While Loop 
    int c=0; 
    int [ ] x1 = {65, 65, 153, 153}; 
    int [ ] y1 = {20, 35, 35, 20}; 
    g.drawPolygon(x1, y1, 4); 


    while (c <= 3) { 
     g.drawPolygon(x1, y1, 4); 

     x1[0] += 88; 
     x1[1] += 88; 
     x1[2] += 88; 
     x1[3] += 88; 

     c += 1; // c = c + 1; - Same thing. 
    }