Results 1 to 5 of 5
  1. #1
    ravi.joshi53 is offline Member
    Join Date
    Oct 2011
    Posts
    24
    Rep Power
    0

    Unhappy Graphics plain 2D objects are not rendered while an action occurred!!!

    Hi, I am designing a game in Swing. Currently I am designing the maze for this game. The maze is generated by using Depth First Search algorithm. In my main JFrame, I have some JPanel. One JPanel, named mazePanel contains the maze. There are some other JPanel also, which contains the JButton for controlling. Following is the mazePanel code.
    Java Code:
    import java.awt.Graphics;
    import javax.swing.BorderFactory;
    import javax.swing.JPanel;
    
    public class MazePanel extends JPanel {
    
        private MazeGenerator mazeGenerator;
        private boolean startNewMaze = false;
    
        public MazePanel() {
            setBorder(BorderFactory.createTitledBorder("Maze"));
            setToolTipText("This is the maze");
        }
    
        public void addNewMaze() {
            startNewMaze = true;
            mazeGenerator = new MazeGenerator();
        }
    
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (startNewMaze) {
                mazeGenerator.generate(g);
                startNewMaze = false;
            }
        }
    }
    There is one JButton, which calls the method mazePanel.addNewMaze() and set the Boolean startNewMaze to true. After setting the startNewMaze, maze should be generated. i.e. mazeGenerator.generate(g) is inside if() condition. Method mazeGenerator.generate(g) recursively draw the random maze. That is why I donít want to run this method not more than once.
    Up to this everything is looking fine. But while I am running the main JFrame and clicks on the JButton, maze is not rendered in the mazePanel. Sometimes when I minimize and maximize the JFrame, maze rendered (might be because of repaint() occur). Even if I comment mazeGenerator.generate(g) inside if() condition and put some g.drawString(). The string is not rendered while action performed (i.e.Pressing JButton).

    Where is the problem? Please help.

    Thank you.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Graphics plain 2D objects are not rendered while an action occurred!!!

    Do not do program logic inside of your paintComponent method since you don't have full control over when or if this method is called, and you never want code in this method that could potentially slow it down which would freeze your application. Instead, only have drawing code in your paintComponent method that uses data held by the class. Call your generate method in a SwingWorker object's doInBackground method, in this method change the data held by the class (the information used by paintComponent to draw the maze), and then when it is done, call repaint.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default Re: Graphics plain 2D objects are not rendered while an action occurred!!!

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  5. #5
    ravi.joshi53 is offline Member
    Join Date
    Oct 2011
    Posts
    24
    Rep Power
    0

    Unhappy Re: Graphics plain 2D objects are not rendered while an action occurred!!!

    Hey Fubarable,
    Basically the MazeGenerator class has a generate(Graphics g) method, which is recursively (obviously after some if() conditions check) drawing each block of the maze.
    I am passing the graphics parameter of the MazePanel to the generate(Graphics g) method after casting it with Graphics2D.

    Java Code:
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import javax.swing.BorderFactory;
    import javax.swing.JPanel;
    
    public class MazePanel extends JPanel {
    
        private MazeGenerator mazeGenerator;
        private Graphics2D g2d;
        
        public MazePanel() {
            setBorder(BorderFactory.createTitledBorder("Maze"));
            setToolTipText("This is the maze");
        }
    
        public void addNewMaze() {
            startNewMaze = true;
            mazeGenerator = new MazeGenerator();
            mazeGenerator.generate(g2d);
            repaint();
        }
    
        public void paintComponent(Graphics g) {
            Graphics2D g2D = (Graphics2D) g;
            this.g2d = g2D;
            super.paintComponent(g2D);
            repaint();
        }
    }
    
    public class MazeGenerator {
    
        public void generate(Graphics g) {
            /*
             * Recursion takes place here with some if() checks.
             * In between each recursion, it draws two blocks using the Graphics object,
             * which are neighbours of each other.
             * Drawing a done by g.fillRect(i, j, LENGTH, WIDTH);
             * Here i and j depends on the block location. LENGTH and WIDTH are final variable.
             * I am passing the Graphics2D object of the mazePanel to this method
             */
        }
    }
    In the main class where i have JButtons with action listeners, i call the addNewMaze() in the following manner-
    Java Code:
        
        private void newMazeButtonActionPerformed(ActionEvent evt) {
            mazePanel.addNewMaze();
        }
    Still i am not getting the maze.

Similar Threads

  1. Replies: 6
    Last Post: 01-14-2012, 06:14 PM
  2. Instantiating Multiple Graphics objects
    By theCardboardBox in forum New To Java
    Replies: 2
    Last Post: 11-25-2010, 02:26 PM
  3. Multiple Graphics Objects?
    By MrFish in forum Java 2D
    Replies: 7
    Last Post: 10-29-2010, 08:37 PM
  4. Accessing objects from within Action listener
    By cog in forum New To Java
    Replies: 4
    Last Post: 12-24-2009, 09:17 PM
  5. Clear Graphics Objects from Jpanel
    By DavidG24 in forum AWT / Swing
    Replies: 2
    Last Post: 05-20-2009, 10:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •