Java - Munger sponge

Solution of Munger sponge: 1. How to implement iteration 2. How to draw a cube 3. Implementation of Munger sponge code 4...

Solution of Munger sponge:

1. How to implement iteration 2. How to draw a cube 3. Implementation of Munger sponge code 4. Drawing board + door sponge code

1. How to implement iteration




In order to realize the effect from Figure 1 - > Figure 2 - > Figure 3, we should think of using iteration to realize it

Because a graph like Figure 3 is composed of several basic graphs like Figure 2

So, how to achieve the effect of hollowing?

View 2

2 the cube of the figure can be divided into three layers:

There are 8 small cubes in the lower layer, 4 in the middle layer and 8 in the upper layer

So what we're thinking about now is how to draw these three layers of basic graphics

But before painting, let's think about: which layer should be painted first:?

If it is in the order of above, we will get:


Obviously, this is not consistent with the graphics we need

So the answer is: down in up

So, the question comes again. What is the order of the small cubes in each layer?

Through the test of the previous problem, we can know that the cube of each layer, the next drawing near us, and the first drawing far away from us can achieve the effect of correct cube stacking

2. How to draw a cube

OK. After confirming the above, let's determine the most basic problem. How to draw a cube?

(x,y) point at the upper left corner of the front face of cube

dx,dy: because we are visually all parallelogram except for the square on the front, so we add an offset value

d: Side length of cube

With these values, we can determine the coordinates of each vertex

Color, free play, as long as the color depth: top > right side > front, and

//above int[] xs1=; int[] ys1=; g.setColor(new Color(0,50,50)); g.fillPolygon(xs1, ys1, 4);//fillPolygon is to draw a filling polygon. The parameter passed in is the array of x coordinate, the array of y coordinate and the number of vertices //Right side int[] xs2=; int[] ys2=; g.setColor(new Color(0,100,100)); g.fillPolygon(xs2, ys2, 4); //positive g.setColor(new Color(0,160,160)); g.fillRect(x, y, d, d);
3. Implementation of Munger sponge code

Because the process of drawing cube is to draw the innermost layer first, and then the smallest one can have the effect of superposition, so as long as the iteration is not carried out to the end, we need to continue the iteration, and then draw cube

public void drawSponge(Graphics g,int n,int x,int y,int dx,int dy,int d){ //n is the number of iterations //Draw from the inside out, from the top down n--; if(n>0){ //Lowest layer drawSponge(g,n,x+2*dx/3,y-2*dy/3+2*d/3,dx/3,dy/3,d/3); drawSponge(g,n,x+2*dx/3+d/3,y-2*dy/3+2*d/3,dx/3,dy/3,d/3); drawSponge(g,n,x+2*dx/3+2*d/3,y-2*dy/3+2*d/3,dx/3,dy/3,d/3); drawSponge(g,n,x+dx/3,y-dy/3+2*d/3,dx/3,dy/3,d/3); drawSponge(g,n,x+dx/3+2*d/3,y-dy/3+2*d/3,dx/3,dy/3,d/3); drawSponge(g,n,x,y+2*d/3,dx/3,dy/3,d/3); drawSponge(g,n,x+d/3,y+2*d/3,dx/3,dy/3,d/3); drawSponge(g,n,x+2*d/3,y+2*d/3,dx/3,dy/3,d/3); //Mesosphere drawSponge(g,n,x+2*dx/3,y-2*dy/3+d/3,dx/3,dy/3,d/3); drawSponge(g,n,x+2*dx/3+2*d/3,y-2*dy/3+d/3,dx/3,dy/3,d/3); drawSponge(g,n,x,y+d/3,dx/3,dy/3,d/3); drawSponge(g,n,x+2*d/3,y+d/3,dx/3,dy/3,d/3); //Topmost drawSponge(g,n,x+2*dx/3,y-2*dy/3,dx/3,dy/3,d/3); drawSponge(g,n,x+2*dx/3+d/3,y-2*dy/3,dx/3,dy/3,d/3); drawSponge(g,n,x+2*dx/3+2*d/3,y-2*dy/3,dx/3,dy/3,d/3); drawSponge(g,n,x+dx/3,y-dy/3,dx/3,dy/3,d/3); drawSponge(g,n,x+dx/3+2*d/3,y-dy/3,dx/3,dy/3,d/3); drawSponge(g,n,x,y,dx/3,dy/3,d/3); drawSponge(g,n,x+d/3,y,dx/3,dy/3,d/3); drawSponge(g,n,x+2*d/3,y,dx/3,dy/3,d/3); }else{ int[] xs1=; int[] ys1=; g.setColor(new Color(0,50,50)); g.fillPolygon(xs1, ys1, 4); int[] xs2=; int[] ys2=; g.setColor(new Color(0,100,100)); g.fillPolygon(xs2, ys2, 4); g.setColor(new Color(0,160,160)); g.fillRect(x, y, d, d); } }
4. Drawing board + door sponge code

We put the Munger sponge on the drawing board

And add the button to realize the effect of drawing according to the coordinates of the points pressed after clicking the button

Sketchpad class:

import javax.swing.JFrame; //forms import javax.swing.JButton; //Button import java.awt.FlowLayout; //Flow layout import java.awt.Graphics; //paint brush public class DrawPad { public static void main(String args[]){ DrawPad dp = new DrawPad(); dp.showUI(); } public void showUI(){ JFrame jf = new JFrame(); DrawPadListener dl = new DrawPadListener(); JButton btn = new JButton("Menger sponge "); btn.setName("Menger sponge "); btn.addActionListener(dl); jf.add(btn); } FlowLayout fl = new FlowLayout(); jf.setTitle("visualization"); //name jf.setSize(800,600); //size jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Shut down process jf.setLayout(fl); //Set up flow layout jf.setResizable(false); //Cannot change form size jf.setLocationRelativeTo(null);//Center display jf.addMouseListener(dl); jf.setVisible(true); //Set form visibility Graphics g = jf.getGraphics(); dl.g = g; } }

Sketchpad listener class:

import java.awt.Color; //colour import java.awt.Graphics; //paint brush import java.awt.event.ActionEvent; import java.awt.event.ActionListener;//Motion monitor import java.awt.event.MouseEvent; import java.awt.event.MouseListener; //Mouse monitor public class DrawPadListener implements MouseListener, ActionListener{ String btnstr;//String of record button Graphics g; int x1,x2,y1,y2; int n = 1;//Iteration level, change by yourself public void actionPerformed(ActionEvent e){ btnstr = e.getActionCommand(); System.out.println("actionPerformed Method is called, btnstr Is:"+btnstr); } public void mouseClicked(MouseEvent e){ System.out.println("click"); } public void mousePressed(MouseEvent e){ System.out.println("Press"); x1=e.getX(); y1=e.getY(); } public void mouseReleased(MouseEvent e){ System.out.println("release"); x2=e.getX(); y2=e.getY();//Release point coordinates if(btnstr.equals("Menger sponge ")) drawSponge(g,n,x1,y1,99,99,198);//The value can be changed by yourself //Friendly tips: dx,dy,d try to choose a multiple of 3. Don't let a straight line appear on the sponge } public void mouseEntered(MouseEvent e){ System.out.println("get into"); } public void mouseExited(MouseEvent e){ System.out.println("sign out"); } public void drawSponge(Graphics g,int n,int x,int y,int dx,int dy,int d){ //Draw from the inside out, from the top down n--; if(n>0){ //Lowest layer drawSponge(g,n,x+2*dx/3,y-2*dy/3+2*d/3,dx/3,dy/3,d/3); drawSponge(g,n,x+2*dx/3+d/3,y-2*dy/3+2*d/3,dx/3,dy/3,d/3); drawSponge(g,n,x+2*dx/3+2*d/3,y-2*dy/3+2*d/3,dx/3,dy/3,d/3); drawSponge(g,n,x+dx/3,y-dy/3+2*d/3,dx/3,dy/3,d/3); drawSponge(g,n,x+dx/3+2*d/3,y-dy/3+2*d/3,dx/3,dy/3,d/3); drawSponge(g,n,x,y+2*d/3,dx/3,dy/3,d/3); drawSponge(g,n,x+d/3,y+2*d/3,dx/3,dy/3,d/3); drawSponge(g,n,x+2*d/3,y+2*d/3,dx/3,dy/3,d/3); //Mesosphere drawSponge(g,n,x+2*dx/3,y-2*dy/3+d/3,dx/3,dy/3,d/3); drawSponge(g,n,x+2*dx/3+2*d/3,y-2*dy/3+d/3,dx/3,dy/3,d/3); drawSponge(g,n,x,y+d/3,dx/3,dy/3,d/3); drawSponge(g,n,x+2*d/3,y+d/3,dx/3,dy/3,d/3); //Topmost drawSponge(g,n,x+2*dx/3,y-2*dy/3,dx/3,dy/3,d/3); drawSponge(g,n,x+2*dx/3+d/3,y-2*dy/3,dx/3,dy/3,d/3); drawSponge(g,n,x+2*dx/3+2*d/3,y-2*dy/3,dx/3,dy/3,d/3); drawSponge(g,n,x+dx/3,y-dy/3,dx/3,dy/3,d/3); drawSponge(g,n,x+dx/3+2*d/3,y-dy/3,dx/3,dy/3,d/3); drawSponge(g,n,x,y,dx/3,dy/3,d/3); drawSponge(g,n,x+d/3,y,dx/3,dy/3,d/3); drawSponge(g,n,x+2*d/3,y,dx/3,dy/3,d/3); }else{ int[] xs1=; int[] ys1=; g.setColor(new Color(0,50,50)); g.fillPolygon(xs1, ys1, 4); int[] xs2=; int[] ys2=; g.setColor(new Color(0,100,100)); g.fillPolygon(xs2, ys2, 4); g.setColor(new Color(0,160,160)); g.fillRect(x, y, d, d); } } }

13 June 2020, 21:18 | Views: 3002

Add new comment

For adding a comment, please log in
or create account

0 comments