I am trying to draw graphics based on a button click using boolean flags. Before, I set the boolean value as a public static boolean type in the ShapeGUI class, and called it in ShapeListener using Shape.draw and that worked, although it took around 10 seconds to conjure a drawing.
Now I set the boolean flag inside the ShapeListener class itself. Now its either not repainting or is very slow. How can I make a drawing appear instantly when I click a button?
public class ShapeGUI extends JFrame
{
public static final int WIDTH = 500;
public static final int HEIGHT = 500;
public ShapeGUI()
{
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//setLayout(new FlowLayout());
setLocationRelativeTo(null);
JMenuBar menu = new JMenuBar();
JMenu file = new JMenu("File");
menu.add(file);
JMenuItem save = new JMenuItem("Save Information");
JMenuItem exit = new JMenuItem("Exit");
file.add(save);
file.add(exit);
setJMenuBar(menu);
ShapeListener test = new ShapeListener();
JButton button = new JButton("Hello");
test.setBackground(Color.CYAN);
button.addActionListener(new ShapeListener());
test.add(button);
add(test);
//followed this person's advice
//First off, your drawing should be done in a paintComponent method override that is held in a class that extends JPanel. Next, this class could have a boolean variable that the paintComponent uses to decide if it will draw a rectangle or not. The button press simply sets the boolean variable and calls repaint on the drawing JPanel.
}
}
public class ShapeListener extends JPanel implements ActionListener
{
boolean draw = false;
Shape s = new Circle();
Shape a = new Rectangle();
@Override
public void actionPerformed(ActionEvent e)
{
draw = true;
repaint();
}
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if(draw)
{
s.draw(g);
}
else
{
a.draw(g);
}
}
}
Your logic is a little off…
You create two instance of the
ShapeListenerclass, add one to the container and use the other as the action listener. Neither will know about each other, meaning that you can click to the cows come home, the first instance ofShapeListenerwill never change.Also, unless you’re planning to override the
preferredSizemethod of theShapeListenerpane, I’d use something like aBorderLayouton the frame, to ensure that the panel is properly laid out, otherwise it will look like the panel isn’t being painted…Try something like…
instead…