6 Replies Latest reply on Jan 28, 2012 10:32 PM by 892826

    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.
      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() {
              setToolTipText("This is the maze");
          public void addNewMaze() {
              startNewMaze = true;
              mazeGenerator = new MazeGenerator();
          public void paintComponent(Graphics g) {
              if (startNewMaze) {
                  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.

      Edited by: 889823 on Jan 28, 2012 3:28 PM (Code was not formatted correctly. I forgot to make use of
        • 1. Re: Graphics plain 2D objects are not rendered while an action occurred!!!
          Don't call long running code on the EDT, and never from a painting method override. Do you realize that you have absolutely no control over how often and how many times a painting method executes?

          Go through the tutorials on performing custom painting and concurrency in Swing.

          1 person found this helpful
          • 2. Re: Graphics plain 2D objects are not rendered while an action occurred!!!
            Hello Darryl,
            Can you please explain me in more details? If possible,please provide any web link or tutorial of the same.
            Thank you so much.
            • 3. Re: Graphics plain 2D objects are not rendered while an action occurred!!!
              Cross posted


              edit And http://forums.netbeans.org/topic46231.html
              edit(2): http://www.java-forums.org/awt-swing/54658-graphics-plain-2d-objects-not-rendered-while-action-occurred.html

              edit(3): The OP edited and removed its post content on JavaRanch.

              Edited by: Darryl Burke
              • 4. Re: Graphics plain 2D objects are not rendered while an action occurred!!!
                Hello Darryl Burke,
                The reason, why i posted the same thread over the internet was to find the ans in less time. Actually i am a student doing some college project and i don't have access to the internet all the time.
                Hope you don't mind my serious mistake.
                • 5. Re: Graphics plain 2D objects are not rendered while an action occurred!!!
                  This kind of thing sounds simple but in reality is fairly tricky. I dealt with these kinds of situations for years while learning java, and still sometimes do.

                  You only posted a snippet of your code, from which we can't go through and debug what your problem is, but I would just say that this is the general pattern

                  1. Receive the event to create a new board. This can in reality be any event that will cause your UI to change
                  2. Do whatever processing you need to do to get your Component ready to be painted again. You can do short jobs on the UI thread. For longer jobs, look at the SwingWorker class
                  3. When you are done processing and ready to redisplay, call repaint() on your Component. Make sure after you call repaint() that any work being done on the UIThread finishes, so that this thread can then handle the repaint request

                  Edited by: tjacobs01 on Jan 28, 2012 1:37 PM
                  1 person found this helpful
                  • 6. Re: Graphics plain 2D objects are not rendered while an action occurred!!!
                    Hey tjacobs,
                    Yeah you are right. Graphics g looks easy but it's not that much easy. You know my algorithm is working fine if i didn't render the maze. Only this rendering portion is creating prob;ems to me. Anyway inside the mazeGenerator.generate(g) there is a stack which keeps track of my visited blokcs (In the simple words, i am trying to generate a random maze using DFS algorithm). This algorithm recursively checks all the neighbor blocks of a maze and draws the maze. The whole code is large and it is not a good idea to put the whole code in a thread. Because it will definitely irritate us.
                    According to your suggestions, i must remove the mazeGenerator.generate(g) from the paintComponent(Graphics g) method and put it outside. Thank you for your suggestion. I will try this trick and report you back.
                    Thank you so much.