门格海绵的实现

news/2024/11/24 11:28:02/

实现效果:
这里写图片描述
源代码:

package sponge;import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Polygon;import javax.swing.JFrame;public class Sponge_Main extends JFrame implements Runnable{public static void main(String[]  args) {Sponge_Main mf = new Sponge_Main();mf.initUI();}Point[] pn = new Point[20];public void paint(Graphics g) {super.paint(g);//正方形的顶点坐标int x = 100,y = 500;int d = 120,dx = 81,dy = 60;Point p0 = new Point(x,y);drawBottomLevel(g,p0,d,dx,dy);Point p1 = new Point(x,y-d);drawMidLevel(g,p1,d,dx,dy);Point p2 = new Point(x,y-2*d);drawTopLevel(g,p2,d,dx,dy);//----------for(int i = 0;i<pn.length;i++) {Point ptem = pn[i];if(ptem!=null) {Point ptem0 = new Point(pn[i].x,pn[i].y+2*d/3); drawBottomLevel(g,ptem0,d/3,dx/3,dy/3);Point ptem1 = new Point(pn[i].x,pn[i].y+d/3);drawMidLevel(g,ptem1,d/3,dx/3,dy/3);Point ptem2 = new Point(pn[i].x,pn[i].y);drawTopLevel(g,ptem2,d/3,dx/3,dy/3);}}}//根据起点p0得到另外6个点public Point[] getPointByP0(Point p0,int d,int dx,int dy) {Point p1 = new Point(p0.x+dx,p0.y-dy);Point p2 = new Point(p0.x+dx+d,p0.y-dy);Point p3 = new Point(p0.x+d,p0.y);Point p4 = new Point(p0.x+d,p0.y+d);Point p5 = new Point(p0.x,p0.y+d);Point p6 = new Point(p0.x+dx+d,p0.y+d-dy);Point[] ps = new Point[7];ps[0] = p0;ps[1] = p1;ps[2] = p2;ps[3] = p3;ps[4] = p4;ps[5] = p5;ps[6] = p6;return ps;}int count = 0;//递归次数public void draw(Graphics g,Point p0,Point p1,Point p2,Point p3,Point p4,Point p5,Point p6) {if(count<20) {pn[count] = p0;}count++;//三原色
//      int c1 = new java.util.Random().nextInt(255);
//      int c2 = new java.util.Random().nextInt(255);
//      int c3 = new java.util.Random().nextInt(255);Graphics2D gD = (Graphics2D) g;//填充面的颜色Polygon pon1 = new Polygon();//填充第一个可见面pon1.addPoint(p0.x, p0.y);pon1.addPoint(p1.x, p1.y);pon1.addPoint(p2.x, p2.y);pon1.addPoint(p3.x, p3.y);gD.setColor(new Color(230,0,0));gD.fillPolygon(pon1);//填充第二个可见面Polygon pon2 = new Polygon();pon2.addPoint(p0.x, p0.y);pon2.addPoint(p3.x, p3.y);pon2.addPoint(p4.x, p4.y);pon2.addPoint(p5.x, p5.y);gD.setColor(new Color(255,0,0));gD.fillPolygon(pon2);//填充第三个可见面Polygon pon3 = new Polygon();pon3.addPoint(p2.x, p2.y);pon3.addPoint(p3.x, p3.y);pon3.addPoint(p4.x, p4.y);pon3.addPoint(p6.x, p6.y);gD.setColor(new Color(200,0,0));gD.fillPolygon(pon3);}public void drawBottomLevel(Graphics g, Point p0, int d, int dx, int dy) {Point[] ps1 = getPointByP0(p0,d,dx,dy);Point[] ps2 = getPointByP0(ps1[3],d,dx,dy);Point[] ps3 = getPointByP0(ps2[3],d,dx,dy);Point[] ps4 = getPointByP0(ps3[1],d,dx,dy);Point[] ps5 = getPointByP0(ps4[1],d,dx,dy);Point[] ps6 = getPointByP0(ps1[1],d,dx,dy);Point[] ps7 = getPointByP0(ps6[1],d,dx,dy);Point[] ps8 = getPointByP0(ps7[3],d,dx,dy);draw(g,ps7[0],ps7[1],ps7[2],ps7[3],ps7[4],ps7[5],ps7[6]);try {Thread.sleep(100);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}draw(g,ps8[0],ps8[1],ps8[2],ps8[3],ps8[4],ps8[5],ps8[6]);try {Thread.sleep(100);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}draw(g,ps5[0],ps5[1],ps5[2],ps5[3],ps5[4],ps5[5],ps5[6]);try {Thread.sleep(100);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}draw(g,ps4[0],ps4[1],ps4[2],ps4[3],ps4[4],ps4[5],ps4[6]);try {Thread.sleep(100);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}draw(g,ps6[0],ps6[1],ps6[2],ps6[3],ps6[4],ps6[5],ps6[6]);try {Thread.sleep(100);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}draw(g,ps1[0],ps1[1],ps1[2],ps1[3],ps1[4],ps1[5],ps1[6]);try {Thread.sleep(100);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}draw(g,ps2[0],ps2[1],ps2[2],ps2[3],ps2[4],ps2[5],ps2[6]);try {Thread.sleep(100);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}draw(g,ps3[0],ps3[1],ps3[2],ps3[3],ps3[4],ps3[5],ps3[6]);try {Thread.sleep(100);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public void drawMidLevel(Graphics g, Point p0, int d, int dx, int dy) {Point[] ps1 = getPointByP0(p0,d,dx,dy);Point[] ps2 = getPointByP0(ps1[3],d,dx,dy);Point[] ps3 = getPointByP0(ps2[3],d,dx,dy);Point[] ps4 = getPointByP0(ps3[1],d,dx,dy);Point[] ps5 = getPointByP0(ps4[1],d,dx,dy);Point[] ps6 = getPointByP0(ps1[1],d,dx,dy);Point[] ps7 = getPointByP0(ps6[1],d,dx,dy);Point[] ps8 = getPointByP0(ps7[3],d,dx,dy);draw(g,ps7[0],ps7[1],ps7[2],ps7[3],ps7[4],ps7[5],ps7[6]);draw(g,ps5[0],ps5[1],ps5[2],ps5[3],ps5[4],ps5[5],ps5[6]);draw(g,ps1[0],ps1[1],ps1[2],ps1[3],ps1[4],ps1[5],ps1[6]);draw(g,ps3[0],ps3[1],ps3[2],ps3[3],ps3[4],ps3[5],ps3[6]);}public void drawTopLevel(Graphics g, Point p0, int d, int dx, int dy) {Point[] ps1 = getPointByP0(p0,d,dx,dy);Point[] ps2 = getPointByP0(ps1[3],d,dx,dy);Point[] ps3 = getPointByP0(ps2[3],d,dx,dy);Point[] ps4 = getPointByP0(ps3[1],d,dx,dy);Point[] ps5 = getPointByP0(ps4[1],d,dx,dy);Point[] ps6 = getPointByP0(ps1[1],d,dx,dy);Point[] ps7 = getPointByP0(ps6[1],d,dx,dy);Point[] ps8 = getPointByP0(ps7[3],d,dx,dy);draw(g,ps7[0],ps7[1],ps7[2],ps7[3],ps7[4],ps7[5],ps7[6]);draw(g,ps8[0],ps8[1],ps8[2],ps8[3],ps8[4],ps8[5],ps8[6]);draw(g,ps5[0],ps5[1],ps5[2],ps5[3],ps5[4],ps5[5],ps5[6]);draw(g,ps4[0],ps4[1],ps4[2],ps4[3],ps4[4],ps4[5],ps4[6]);draw(g,ps6[0],ps6[1],ps6[2],ps6[3],ps6[4],ps6[5],ps6[6]);draw(g,ps1[0],ps1[1],ps1[2],ps1[3],ps1[4],ps1[5],ps1[6]);draw(g,ps2[0],ps2[1],ps2[2],ps2[3],ps2[4],ps2[5],ps2[6]);draw(g,ps3[0],ps3[1],ps3[2],ps3[3],ps3[4],ps3[5],ps3[6]);}public void initUI() {this.setTitle("门格海绵");this.setSize(1000, 800);this.setDefaultCloseOperation(3);this.setResizable(false);this.setLocationRelativeTo(null);this.setVisible(true);}@Overridepublic void run() {// TODO Auto-generated method stub}
}

http://www.ppmy.cn/news/420208.html

相关文章

[海绵城市]透水铺装、调蓄设施与控制指标关系整理

道路建设中的海绵城市措施&#xff0c;常见的有人行道透水砖、机动车道透水沥青等透水铺装和雨水花园、下凹绿地等调蓄设施。在设计时一般采用容积法&#xff0c;复核相关措施是否能达到上位规划提出的控制指标。 厦门市《海绵城市建设工程施工图设计导则及审查要点》&#xff…

SHA-3:KECCAK(基于海绵构造的哈希函数)

SHA-3 SHA-3的历史海绵结构吸收阶段与输出阶段双工海绵 KECCAK-p θ \theta θ ρ ρ ρ π π π χ \chi χ ι \iota ι Multi-rate padding SHA-3的历史 2015年通过公募展SHA-3选出了一个名为KECCAK的哈希函数&#xff0c;作为SHA-3的代表。因此这两个名字代表着同一个哈希…

中国海绵城市建设发展模式及投资方式建议报告2022-2028年

中国海绵城市建设发展模式及投资方式建议报告2022-2028年 【报告目录】: 第1章&#xff1a;海绵城市建设综述及数据来源说明1.1 海绵城市的界定 1.2 海绵城市建设行业所归属国民经济行业分类 1.3 本报告研究范围界定说明 1.4 本报告数据来源及统计标准说明 第2章&#xff1…

四川省某市智慧海绵城市监测系统案例分享

四川省某市自2018年起至今陆续进行海绵城市建设&#xff0c;应用平升智慧海绵城市监测系统对小区排水管网出口、河流断面重要位置进行监测&#xff0c;数据统一上报至管理中心软件平台&#xff0c;为城市水资源、水环境、水安全的综合管理和海绵城市建设成效的评定提供数据支撑…

智慧海绵城市解决方案

海绵城市是指能够像海绵一样吸收水分并再利用的城市建设方式&#xff0c;能够最大程度的避免暴雨导致的城市内涝等积水情况&#xff0c;并且吸收的水分能够再次利用于其他例如洗车&#xff0c;浇花等情境。能够有效提升水资源的再利用以及自然灾害造成的影响。   一、系统原…

海绵中的水--时间

时间都去哪了 这几天感觉自己越发的忙&#xff0c;也许大家会认为这不挺好的吗&#xff0c;过得多充实&#xff0c;但我想说&#xff0c;这不是充实&#xff0c;而是忙&#xff0c;一天到晚都在忙活。忙中出乱总是会发生&#xff0c;上次的国学达人比赛也许是最好的例子了&…

分形之门格海绵

门格海绵解决思路&#xff1a; 1. 迭代如何实现 2. 立方体该怎么画 3. 实现门格海绵代码 4. 画图板门格海绵代码 1. 迭代如何实现 为了实现从1图—>2图—>3图效果,我们就要想到用迭代去实现. 因为像3图这样的图形,是由若干个2图这样的基本图形组成的 那么,怎样实现…

中国引流海绵行业市场供需与战略研究报告

引流海绵用于吸收伤口和其他需要清除不需要的液化物质的区域的液体。 引流海绵市场的企业竞争态势 该报告涉及的主要国际市场参与者有Medline、Dukal Corporation、Dynarex Corporation、B Braun、Amd-Ritmed、McKesson、Boston Medical、Medtronic、Smiths Medical、Teleflex…