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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With