I have a class named ControlsPanel. If I press a JButton (Start) in this class, the value of a boolean (isPressed) changes to true. In another class (CashRegistersPanel) I want to draw an Image, but only of the value of the boolean in the previous class is true. Of course the value of this boolean is false in the begining, so it doesn't draw anything.
These are my two classes:
public ControlsPanel(final ParametersPanel panel) {
start = new JButton("Start");
stop = new JButton("Stop");
start.setFont(new Font("Arial", Font.BOLD, 14));
stop.setFont(new Font("Arial", Font.BOLD, 14));
this.setLayout(null);
this.setBackground(new Color(199,202,255));
this.add(start);
this.add(stop);
start.setBounds(10, 10, 280, 30);
stop.setBounds(10, 50, 280, 30);
stop.setEnabled(false);
start.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
if (start.getText().equals("Start")) {
start.setText("Pause");
stop.setEnabled(true);
startIsPressed = true;
}
}
});
public boolean StartisPressed() {
return startIsPressed;
}
}
And
public class CashRegistersPanel extends JPanel{
private Image img;
private int amount;
private ParametersPanel parametersPanel;
private ControlsPanel controlsPanel;
private boolean startIsPressed;
public CashRegistersPanel(ParametersPanel parametersPanel, ControlsPanel controlPanel) {
this.parametersPanel = parametersPanel;
this.controlsPanel = controlPanel;
startIsPressed = controlsPanel.StartisPressed();
this.setBackground(new Color(237,237,237));
this.setLayout(null);
CashRegister cashRegister = new CashRegister();
img = cashRegister.getImg();
amount = parametersPanel.getAmountOfRegisters();
}
//Painting CashRegisters
public void paintComponent(Graphics g) {
if(startIsPressed) {
super.paintComponent(g);
for (int i = 1; i <= amount; i++) {
g.drawImage(img, 30, i*50, this);
}
}
}
}