java-谢尔宾斯基地毯

java-谢尔宾斯基地毯java 谢尔宾斯基地毯本程序将使用递归画出谢尔宾斯基地毯 首先我们将主类继承 JFrame 类 因为要使用 JFrame 类生成一个窗体 然后在其中定义一个初始化方法 publicvoidin this setSize 800 600 this setDefaultCl MyJFrame EXIT ON CLOSE this setLo

java–谢尔宾斯基地毯

本程序将使用递归画出谢尔宾斯基地毯,首先我们将主类继承JFrame类,因为要使用JFrame类生成一个窗体,然后在其中定义一个初始化方法:

public void initUI(){ this.setSize(800,600); this.setDefaultCloseOperation(MyJFrame.EXIT_ON_CLOSE); this.setLocationRelativeTo(null); this.setLayout(new FlowLayout()); this.setVisible(true); //设置可见 } 
public void paint(Graphics g) { // TODO Auto-generated method stub super.paint(g); g.drawRect(0, 0, this.getWidth(), this.getHeight()); this.test(g, 0, 0, this.getWidth(), this.getWidth(),4); } private int test(Graphics g, int x, int y, int l, int w, int num) { if (num == 0) { return 1; } int temp = x; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (i == 1 && j == 1) { g.fillRect(x, y, l, w); }else g.drawRect(x, y, l, w); test(g, x, y, l / 3, w / 3, num - 1); x += l; } x = temp; y += w; } return 1; } 
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/231447.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号