金字塔java源代码 金字塔程序化编写( 二 )


if( jrowPoints - 1){
if(j + 1 == leftpoint || j + 1 == rightpoint) // 列序号从0开始,但点位是从1开始的,所以给列序号+1
allChar[index] = shape;
else
allChar[index] = ' ';
}else{
allChar[index] = '\n';
}
}
}
//特殊处理最后一行
int point = width / 2 + (width % 20 ? 1 : 0);
int startIndex = (height - 1) * rowPoints;
for(int i = 0; irowPoints; i++){
if( irowPoints - 1){
if( i + 1 == point)
allChar[startIndex + i] = shape;
else
allChar[startIndex + i]= ' ';
}else
allChar[allChar.length - 1] = '\n';
}
String result = new String(allChar);
System.out.print(result);
}
class PanelContainer extends JPanel{
private Point point1;
private Point point2;
private Point point3;
public PanelContainer(Point point1, Point point2, Point point3){
this.point1 = point1;
this.point2 = point2;
this.point3 = point3;
setBackground(Color.white);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if(point1 != nullpoint2 != nullpoint3 != null){
g.setColor(Color.red);
Graphics2D g2d = (Graphics2D) g;
g2d.drawLine((int)point1.getX(), (int)point1.getY(), (int)point2.getX(), (int)point2.getY());
g2d.drawLine((int)point2.getX(), (int)point2.getY(), (int)point3.getX(), (int)point3.getY());
g2d.drawLine((int)point1.getX(), (int)point1.getY(), (int)point3.getX(), (int)point3.getY());
}
}
}
//Java2D
public Question1(int height, int width) {
JFrame frame = new JFrame("Java2D 三角形");
frame.setBounds(50, 50, 400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Point point1 = new Point(50, 50);// 让图形举例边界50像素
Point point2 = new Point(width + 50, 50);
Point point3 = new Point(width/2 + 50, height + 50);
PanelContainer container = new PanelContainer(point1, point2, point3);
frame.getContentPane().add(container);
frame.setVisible(true);
}
public static void main(String args[]){
new Question1(200, 300, '*');
new Question1(200, 300);
}
}
【金字塔java源代码 金字塔程序化编写】关于金字塔java源代码和金字塔程序化编写的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

推荐阅读