目标是使用等边三角形,正方形或六边形对平面进行镶嵌,由从文件读取的整数确定。边长也通过从文件中读取整数来给出。我已经把它做成了正方形,但这个三角形让我很难过。我一直在尝试使用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;
}
}
}
}
}
答案 0 :(得分:1)
要将三角形的左下角设为0,0,需要将3个坐标设为:
0,0 (bottom left)
sidelength,0 (bottom right)
sidelength / 2,sin(60)* sidelength (tip)
要进行详细说明,该行中的下一个三角形将具有
sidelength / 2,sin(60)* sidelength (top left)
3 * sidelength / 2,sin(60)* sidelength (top right)
sidelength,0 (tip, which is now on the bottom)
然后,它只是在每个点上添加x,y来移动它。
答案 1 :(得分:1)
这听起来像是一个数学问题。假设您使用的是等边三角形,等边三角形的高度等于sqrt(3) * base
。另外,为了使等边三角形具有特征,它们可以上下移动等等。
因此,如果边长是sidelength
,第一个坐标是(0,0)
,要绘制第一个三角形,请执行以下操作:
g.drawLine(0, 0, sidelength, 0); // the top
g.drawLine(0, 0, sidelength/2, sidelength/2 * sqrt(3)); // left side
g.drawLine(sidelength, 0, sidelength/2, sidelength/2 * sqrt(3)); // right side
要绘制颠倒的三角形,您只需绘制两个边:
g.drawLine(sidelength, sidelength/2 * sqrt(3), 3 * sidelength/2, sidelength/2 * sqrt(3));
g.drawLine(sidelength, 0, 3 * sidelength/2, sidelength/2 * sqrt(3));
但是,这不是最好的方法(手工绘制每个三角形)。最好只绘制大线,因为线条都是相互连接的。
private static final double srqtThree = sqrt(3)/2d;
private Dimension d = new Dimension();
private int rootThree;
public DrawShapes(int sideLength) {
rootThree = (int) sideLength * sqrt(3)/2d;
}
public void paintComponent(Graphics g){
getSize(d);
int y = 0;
while(y < d.height) {
// Draw the horizontal line
g.drawLine(0, y, d.width, y);
y = y + rootThree;
}
// Figure this out mostly yourself, but now you draw the angled sides.
// Use this to get started. This is the down-to-right line
g.drawLine(0, 0, d.width / sqrtThree, d.height);
g.drawLine(sideLength, 0, sideLength + (d.width / sqrtThree), d.height);-
// Now draw the down-to-left line
}
注意:您可能需要执行大量的大小边界检查才能使其正常工作。但它比手工绘制每个三角形要快。
答案 2 :(得分:0)
创建一个直立的等边三角形,边长为10
,原点居中于(50, 50)
。
g.fillPolygon(createTriange(50, 50, 10, false));
private Polygon createPolygon(float x, float y, float side, boolean invert) {
float xOff = side / 2f;
float yOff = (float) (xOff * Math.sqrt(3));
float r1 = 1f / 3f;
float r2 = 2f / 3f;
if (invert) { yOff *= -1; }
Point2D.Float top = new Point2D.Float(x, y - (yOff * r2));
Point2D.Float left = new Point2D.Float(x - xOff, y + (yOff * r1));
Point2D.Float right = new Point2D.Float(x + xOff, y + (yOff * r1));
int xCoords[] = { (int) top.x, (int) left.x, (int) right.x, (int) top.x };
int yCoords[] = { (int) top.y, (int) left.y, (int) right.y, (int) top.y };
return new Polygon(xCoords, yCoords, xCoords.length);
}