0

I am trying to create a simple Java applet which should display generated Hilberts Curves in Java. I mixed up my code and some from a book, and now I got stuck. I dont get the display I want to get.

Here is the whole code :

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    
    //Defining classes 
    public class HilbertCurves extends JApplet {
        //Declare data fields
        private JTextField OrderTextfield = new JTextField("0",5);
        private HilbertCurve  hilbertCurvePanel = new HilbertCurve();
        
        //Constructors
        public HilbertCurves() {
        //Creating a panel object
        JPanel panel = new JPanel();
        panel.add(new JLabel("Enter the order : "));
        panel.add(OrderTextfield);
        
        OrderTextfield.setHorizontalAlignment(SwingConstants.RIGHT);
        
        //Adding panel to the app
        add(hilbertCurvePanel);
        add(panel, BorderLayout.SOUTH);
        OrderTextfield.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
        hilbertCurvePanel.setOrder(Integer.parseInt(OrderTextfield.getText()));
            }
        });
        }
        
        
        private static class HilbertCurve extends JPanel {
        //Declare variables
            private int length;
            private int order=1;
            private int a,b;
            
        //For order
            public void setOrder(int order){
            this.order=order;
            repaint();
            }
            
            public void paintComponent(Graphics g){
            super.paintComponent(g);
            
        //Find the length
            length = Math.min(getWidth(),getHeight());
            for (int i = 0; i < order; i++)
                length/=2;
            
        //Reset
            a = length/2; b=length/2;
            Upper(g,order);
            }
            
        //Display upper
            private void Upper(Graphics g, int order){
                if (order >0){
                    Left(g, order-1);
                    lineNewPosition(g,0,length);
                    Upper(g,order-1);
                    lineNewPosition(g,0,-length);
                    right(g, order-1);
                
                }
            }
        
        //Display left
            private void Left(Graphics g, int order){
                if (order>0){
                    Upper(g,order-1);
                    lineNewPosition(g,length,0);
                    Left(g,order-1);
                    lineNewPosition(g,0,length);
                    Left(g,order-1);
                    lineNewPosition(g,-length,0);
                    Down(g,order-1);
                
                }
            }
            
        //Display right 
             private void right(Graphics g, int order){
                if (order>0){
                    Down(g,order-1);
                    lineNewPosition(g,-length,0);
                    right(g,order-1);
                    lineNewPosition(g,0,-length);
                    right(g,order-1);
                    lineNewPosition(g,length,0);
                    Upper(g,order-1);
                
                }
            }
        
        //Display down 
             private void Down(Graphics g, int order){
                if (order>0){
                    right(g,order-1);
                    lineNewPosition(g,0,-length);
                    Down(g,order-1);
                    lineNewPosition(g,-length,0);
                    Down(g,order-1);
                    lineNewPosition(g,0,length);
                    Left(g,order-1);
                
                }
            }     
        //Draw a line
             public void lineNewPosition(Graphics g, int deltaX, int deltaY){
             g.drawLine(a, b, a + deltaX, b + deltaY);
             a+=deltaX;
             b+=deltaY;
            }
        }
        
        public static void main(String[] args) {
        JApplet applet = new HilbertCurves();
        applet.init();
        applet.start();
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("Visualising Hilbert Curves");
        frame.getContentPane().add(applet,BorderLayout.CENTER);
        frame.setSize(512,512);
        frame.setVisible(true);
        }
    }

In my opinion there is something wrong when defining the drawing functions, but at this point it seems I cannot figure out the solution. Here is the output I get (it is different from my goal):

wrong output

Spektre
  • 49,595
  • 11
  • 110
  • 380
f1avj0
  • 1
  • 1
  • Welcome to Stack Overflow! Could you edit your answer to specify your end goal, and what’s wrong with the current output? – Nikolay Shebanov Dec 03 '20 at 23:30
  • See these for some inspiration [smoth hilbert curves](https://stackoverflow.com/a/50464341/2521214) and [parametric 2D/3D hilbert curves](https://stackoverflow.com/a/50488348/2521214) I also got stuck similarly to you and then it hit me... I switch to turtle graphics first compute the string describing the path and then convert to polygon. Suddenly the curve generation was much simpler,clearer and correct (see the first link). The second link is skipping the turtle gfx and computing directly the points (but would not get there without the first)... – Spektre Dec 04 '20 at 09:49

1 Answers1

0

Your drawing code seems a lot longer than necessary.

With a state that consists of a current position and direction, and these primitive operations:

  • turn(x) will turn left x*90 degrees (negative numbers turn right)
  • step() will draw a line and move forward one step

You can make a Hilbert curve like this:

// draw a hilbert curve.  Resulting total movement will be forward,
// and the current direction is left unchanged
// order: order of curve to draw
// side: -1 to draw on the left, 1 to draw on the right

hilbert(order, side):
    if (order > 0):
        turn(side)
        hilbert(order-1,-side)
        step()
        turn(-side)
        hilbert(order-1,side)
        step()
        hilbert(order-1,side)
        turn(-side)
        step()
        hilbert(order-1,-side)
        turn(side)
    
    
Matt Timmermans
  • 53,709
  • 3
  • 46
  • 87