java中怎样写等腰三角形_各位前辈,如何用java画个等腰三角形

展开全部

窗口的(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);// 右边的腰

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值