Trying to create a maze generator, to get the repaint()
to work I had to add the swingUtilities.invokeLater
in main, and make the graphics class implement Runnable with new Thread(this).start();
in the constructor.
However, I want the maze to regenerate on a button click, which is in the button class.
Simply calling the method from another class seems to work as println
still prints but nothing is repainted onto the panel, resizing the window also doesn't change anything.
I've tried creating a new thread using the below code but the same thing happens?
MazeArea ma = new MazeArea();
Thread t = new Thread(ma);
t.start();
Main Class
public class MainGenerator {
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// executes code
}
});
}
}
Graphics Class
public class MazeArea extends JComponent implements Runnable {
public MazeArea() {
new Thread(this).start();
}
public void run() {
generateMaze();
clear();
}
synchronized protected void paintComponent(Graphics g) {
super.paintComponent(g);
drawMaze(g);
}
}
Button Class
public class VariableDetails extends JPanel {
public VariableDetails() {
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
MazeArea ma = new MazeArea();
Thread t = new Thread(ma);
t.start();
}
});
}
How would I just rerun a thread?