3

I am creating a small JPanel program to generate mazes. All of my code works but I want to display the maze generation in action. I edited my program to cause the paintComponent function to show the in progress maze. But I am in another class which is being passed my JPanel object so it can "refresh" the display.

What should I do that will call the paintComponent function to draw all the cells AND refresh the displays?

This is the configuration of the panel:

JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
gamePanel = new GamePanel();
controlPanel = new ControlPanel();
mainPanel.add(gamePanel, BorderLayout.CENTER);
mainPanel.add(controlPanel, BorderLayout.SOUTH);
mainPanel.revalidate();

This is the code expected to be called on repaint:

protected void paintComponent(Graphics pen){
      if(started){
          int count=0;
          for (MapCell c:coms){
              if (c.type!='w'){
                  count++;
              }
              c.paint(pen);
          }
          System.out.println("White space: " +count);
          System.out.println("Components: "+coms.size());
          for(Anno an:mapGen.ans){
              an.draw(pen,img,colWidth,rowHeight);
          }
      }

The print statements work fine after the maze generation but only the image "Anno" is drawn and it leaves a trail behind it. I have confirmed the list is getting correct data.

Others
  • 2,876
  • 2
  • 30
  • 52
  • 2
    You invoke `repaint()` on a component to force a repaint of it. – camickr Nov 04 '13 at 06:01
  • @camickr I have tried that but it doesn't even call paintComponent. – Others Nov 04 '13 at 06:05
  • 1
    Then something is wrong with your code. The panel must be displayed on a visible GUI and must have a size greater than zero. Maybe you just created the panel and added it to the GUI but didn't revalidate() the parent panel. – camickr Nov 04 '13 at 06:13
  • @camickr nope the code is in the post now – Others Nov 04 '13 at 06:17
  • @Gregor: Try using repaint() instead of revalidate(): http://stackoverflow.com/questions/1097366/java-swing-revalidate-vs-repaint - I also don't see a "setVisible(true)" anywhere. Are you sure you are actually enabling your UI? – Birb Nov 04 '13 at 06:24
  • @Izmaki I am sure the window shows up just fine and acts as I would expect. However when a new maze is generated it does not show it in progress in fact it just does nothing. And when it finishes it stops displaying cells. I have included my paintComponent code as well for refrence – Others Nov 04 '13 at 06:33
  • You don't add the mainPanel to the GUI anywhere. revalidating and repainting it before you add it to the GUI won't do anything. – camickr Nov 04 '13 at 06:49
  • @camickr mainPanel.add(gamePanel, BorderLayout.CENTER); does it I believe. Also as I have explained the panel mostly functions perfectly. I just want to know what function I should call to call paintComponent AND(big and) update the display to show the new data. Which is the part that is broken. – Others Nov 04 '13 at 06:55
  • @Imaki I fixed one part. the cells now render correctly afterwards but it still doesn't show during the algorithm. – Others Nov 04 '13 at 07:05
  • @Gregor: your code lacks the calculation entirely. There’s nothing showing us how you invoke it and how you (attempt to) trigger the refresh. So why do you think we have the slightest chance of guessing what your do wrong in that code? – Holger Nov 04 '13 at 08:43

1 Answers1

2

The cells now render correctly afterwards, but it still doesn't show during the algorithm.

Your calculation is probably blocking the event dispatch thread. Instead, iterate your algorithm in a background thread using SwingWorker, as shown here and here, and publish() interim results that process() can use to update your panel.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045