目标是使用等边三角形、正方形或六边形对平面进行镶嵌,由从文件中读取的整数确定。边长也是通过从文件中读取一个整数来给出的。我已经为正方形做过,但三角形让我难过。我一直在尝试使用drawPolygon
. 我假设边长将与从点 a 到点 b 到点 c 的距离相关,但我真的不知道如何找到坐标。
import java.awt.*;
import javax.swing.JPanel;
public class DrawShapes extends JPanel {
private int shape; //user choice of which shape to draw
private int sideLength; //user choice of what length to draw the sides
public DrawShapes(int shapeChoice, int sideChoice) {
shape = shapeChoice;
sideLength = sideChoice;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
switch(shape) {
case 3: //Draws triangles
//int[] x =
//int[] y =
//int[] n =
g.setColor(Color.green);
//g.drawPolygon(x,y,n);
break;
case 4: //Draws squares
g.setColor(Color.blue);
g.drawRect(sideLength*i, sideLength*j, sideLength, sideLength);
break;
case 6: //Draws hexagons
break;
}
}
}
}
}