1

我使用了一个引用同一数组中先前元素的数组来确定下一个值是什么。这样我就可以在绘制多边形时获得位置的相对值。没有报告语法错误,但我在示例中使用的三角形要么不可见,要么不存在。

对于这个例子,我想在窗口的上半部分随机散布小的黑色三角形,而不考虑窗口的边缘和其他三角形。以下是包含我想要实现的示例的代码,但没有使用自引用数组(我在 BlueJ 中编写了此代码,并且从未在 BlueJ 之外编写过任何内容。我不知道如何编写连续的代码所以请耐心等待。我写了两个类中的每一个在每个 // 之后开始的位置:

//第一类

import javax.swing.*;

public class patterns {
    public static void main(String[] args) {
        JFrame f = new JFrame("Example");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        example p = new example();
        f.add(p);
        f.setSize(400,400);
        f.setVisible(true);
    }
}

//下一节课

     import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.Color;
public class example extends JPanel{
    public void paintComponent (Graphics g){
        super.paintComponent(g);
        this.setBackground(Color.WHITE);
        //The following is the part of the code that doesn't work.
        g.setColor(Color.BLACK);
        int x=0;//this x is used for the following while loop
        int[] xC = new int[3];//these are the x coordinates of the random triangles
        int[] yC = new int[3];//these are the y coordinates of the random triangles
        while(x<14){//this while loop is used to make many triangles
            xC[0]= (int)Math.random()*400;
            xC[1]= xC[0]+(int)Math.random()*20;
            xC[2]= xC[1]+(int)Math.random()*4;
            yC[0]= (int)Math.random()*200;
            yC[1]= yC[0]-(int)Math.random()*10;
            yC[2]= yC[0]-(int)Math.random()*3;
            g.fillPolygon(xC,yC,3);//I changed this to fillPolygon
            x++;//this is so the loop will eventually end
        }
        //the following is the manual part that does work.
        int[] xCe = new int[3];//this is the array of x coordinates for the triangle
        xCe[0]= 200;
        xCe[1]= 210;
        xCe[2]= 213;
        int[] yCe = new int[3];//this is the array of y coordinates for the triangle
        yCe[0]= 300;
        yCe[1]= 295;
        yCe[2]= 299;
        g.fillPolygon(xCe,yCe,3);//this polygon appears properly
        //everything after this is just to help explain after compiling
        g.drawString("There should be a whole bunch of little triangles that look",20,320);
        g.drawString("sort of like this one, but on the top half of this window.", 40,340);
        g.drawLine(170,270,200,290);
        g.drawLine(194,289,200,290);
        g.drawLine(197,285,200,290);
        g.drawString("this one",140,260);
    }
}

是什么导致它们看不见?我希望它们是可见的。我不知道我做错了什么。它是自引用数组吗?fillPolygon() 不允许这样做吗?

4

1 回答 1

0

您可以像这样初始化数组中的值:

xC[0]= (int)Math.random()*400;

但是强制转换发生在乘法之前,因此Math.random()(小于 1)返回的值被强制转换为 int,因此它变为0*400,因此 0 存储到数组的所有索引中。

要解决此问题,只需在乘法周围加上括号,如下所示:

xC[0]= (int)(Math.random()*400);

我还应该注意,您的循环仅绘制了 14 次相同的三角形,因此您仍然不会看到一堆不同的三角形。要解决这个问题,只需将用于生成三角形的代码放入循环中,如下所示:

for (int i = 0; i < 14; i++) {
    int[] xC = new int[3];
    xC[0] = (int) (Math.random() * 400);
    xC[1] = xC[0] + (int) (Math.random() * 200);
    xC[2] = xC[1] + (int) (Math.random() * 4);
    int[] yC = new int[3];
    yC[0] = (int) (Math.random() * 200);
    yC[1] = yC[0] - (int) (Math.random() * 10);
    yC[2] = yC[0] - (int) (Math.random() * 3);
    g.setColor(Color.BLACK);
    g.fillPolygon(xC,yC,3);
}

我还建议给你的三角形一个更统一的大小(< 3 和 4 的边有点小),也许会生成一个介于低数和高数之间的随机值(例如 10 到 20)。也许java.util.Random在你上课的时候看看课堂。

于 2013-09-12T03:57:39.787 回答