1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| public class Scarpet extends JFrame { public Scarpet(){ setTitle("谢尔宾斯基地毯"); setSize(800,800); setResizable(true); setDefaultCloseOperation(EXIT_ON_CLOSE); setLocationRelativeTo(null); setVisible(true); } @Override public void paint(Graphics g) { super.paint(g); draw(150,150,500,500,g); } public void draw(int x,int y,int w,int h,Graphics g){ if(w<3) return ; g.fillRect(x+w/3,y+h/3,w/3,h/3); draw(x,y,w/3,h/3,g); draw(x+w/3,y,w/3,h/3,g); draw(x+2*w/3,y,w/3,h/3,g); draw(x,y+h/3,w/3,h/3,g); draw(x+2*w/3,y+h/3,w/3,h/3,g); draw(x,y+2*h/3,w/3,h/3,g); draw(x+w/3,y+2*h/3,w/3,h/3,g); draw(x+2*w/3,y+2*h/3,w/3,h/3,g); } public static void main(String[] args) { new Scarpet(); } }
|