展开全部
窗口的(Swing)就相对容易一些,只要你找到三个点的坐标就可以32313133353236313431303231363533e78988e69d8331333337396261通过API画出来
而控制台版就通过打点,打空格来打到画出等腰三角形;public class DrawTriangleTest extends JFrame {
private Point top;
private Point bottom1;
private Point bottom2;
public static void main(String[] args) {
new DrawTriangleTest();//swing版的三角形
//下面是控制台版的三角形
//-----------------------
int n = 10;
String c = "\t"; //分隔符
String x = "\t*"; //打点
for (int i = 0; i
for (int j = 0; j
if (j == (n - i) || j == (n + i)) {
System.out.print(x);
} else {
System.out.print(c);
}
}
System.out.println();
}
for(int j=0;j<2*(n+1)-1;j++){
System.out.print(x);
}
//-----------------------
}
public DrawTriangleTest() {
init();
}
public void init() {
this.setSize(new Dimension(300, 300));
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(3);
this.setVisible(true);
drawTrinagle(100, 150);
}
public void drawTrinagle(int h, int w) {
int bx = (int) ((this.getWidth() - w) / 2.0);// 低端左边点的x
int ty = (int) ((this.getHeight() - h) / 2.0);//
top = new Point(this.getWidth() / 2, ty);// 定点
bottom1 = new Point(bx, ty + h);// 左下角的点
bottom2 = new Point(bx + w, ty + h);// 右下角的点
this.repaint();
}
@Override
public void paint(Graphics g) {
super.paint(g);
if (top != null && bottom1 != null && bottom2 != null) {
g.drawLine(bottom1.x, bottom1.y, bottom2.x, bottom2.y);// 底下那条线
g.drawLine(top.x, top.y, bottom2.x, bottom2.y);// 左边的腰
g.drawLine(bottom1.x, bottom1.y, top.x, top.y);// 右边的腰
}
}
}