Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8711277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:50:15+00:00 2026-06-13T04:50:15+00:00

I am making a maze and I would like to use the recursive method

  • 0

I am making a maze and I would like to use the recursive method as defined here. However, I need some help as to how to randomly open up the lines once I randomly draw them. Right now I’m creating the lines (walls of the maze) simply by drawing them with their beginning and end x- and y-coordinates. I just can’t seem to find a simple way to “erase” (or “open up”) parts of the lines.

EDIT: Okay I need to be slightly more specific. How could I randomly select places on each line to “open up?”

EDIT 2: Here is some code of what I’m trying to do:

public static void draw() {
    // picks a random spot in the rectangle
    Random r = new Random();
    int x0 = r.nextInt(w)
    int y0 = r.nextInt(h)

    // draws the 4 lines that are perpendicular to each other and meet
    // at the selected point
    StdDraw.line(x0, 0, x0, y0);
    StdDraw.line(0, y0, x0, y0);
    StdDraw.line(x0, h, x0, y0);
    StdDraw.line(w, y0, x0, y0);
}

public static void main(String[] args) {   
    // set up the walls of the maze
    // given w = width and h = height
    StdDraw.setXscale(0, w);
    StdDraw.setYscale(0, h);

    StdDraw.line(0, 0, 0, h);
    StdDraw.line(0, h, w, h);
    StdDraw.line(w, h, w, 0);
    StdDraw.line(w, 0, 0, 0);

    draw();
}

Now I just need to figure out how to randomly select 3 of these lines, and for each line randomly erase a portion.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T04:50:16+00:00Added an answer on June 13, 2026 at 4:50 am

    Assuming you’re using swing and the paintComponent method, you would set the Graphic‘s color to the background color and draw over the line again. Here’s an example:

    public class DrawTest extends JPanel{
        public static void main(String[] args)
        {
    
            final JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(new DrawTest());
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    
        public Dimension getPreferredSize(){
            return new Dimension(400,300);
        }
    
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            g.setColor(Color.black);
            g.drawLine(10, 10, 100, 10);
            g.setColor(getBackground());
            g.drawLine(50, 10, 60, 10);
        }
    }
    

    EDIT

    I suppose you’re not creating the Maze in the paintComponent method (or you’d end up with a new maze every time you repainted). So, I’d recommend creating a sub-class similar to below and storing instances of it in an ArrayList field of your main class. Then you can iterate through your ArrayList when you’re doing your panting.

    public static class MazeWall{
        public static final int OpeningWidth = 10;
    
        Point start;
        Point end;
        Point opening;
    
        public MazeWall(Point start, Point end, boolean hasOpening){
            this.start = start;
            this.end = end;
    
            if(hasOpening){
                int range;
                if(start.x == end.x){
                    range = end.x - start.x - OpeningWidth;
                    int location = (int)(Math.random() * range + start.x);
                    opening = new Point(location, start.y);
                } else{
                    range = end.y - start.y - OpeningWidth;
                    int location = (int)(Math.random() * range + start.y);
                    opening = new Point(location, start.x);
                }
            }   
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to have a go at making some simple games for personal/learning
Making the transition from Vim to gVim, I would like to disable all toolbars
Making a ternary logic table, and I would like to make my own function
I'm making another batch based video game and its like a maze game and
Making my Bluetooth application I need access to some broadcast actions on the Android
Making a simple Java code for Blackjack and need some advice. OK I am
Making an adobe flex ui in which data that is calculated must use proprietary
I am making a simple maze program where the user can create walls, paths,
I'm making a maze game. The character can't walk through the walls of the
I am making a maze program in Java which consists of a grid of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.