I am using JButton's Action listener to draw different shapes. It is working fine but how to keep previously drawn shapes on panel all time? Because when another button pressed previous shapes has gone.
jButton1.setText("Button1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("Button2");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
s = evt.getActionCommand();
repaint();
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
s = evt.getActionCommand();
repaint();
}
....... and paintComponent method is
protected void paintComponent(Graphics g) {
super.paintComponent(g);
System.out.println("====>>> " + s);
switch (s) {
case "Button1":
g.drawRoundRect(20,20,40,40,100,200);
break;
case "Button2":
g.drawRect(0, 0, 200, 200);
break;
default:
g.drawOval(40, 40, 100, 100);
Here String s contain pressed buttons caption.