I am trying to draw a circle when a button is pressed.
public Buttons(Panel panel){
addStud = new JButton("+ Student");
addStud.setToolTipText("Add a student");
addStud.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
//draw circle here
}
});
This is my button in Button.java
public void draw(Graphics g){
g.setColor(Color.BLUE);
g.fillOval(200, 200, 400, 50);
}
This is what I am trying to call in Panel.java
I have a public void paintComponent(Graphics g)
but I don't want the circle to be drawn immediately.
I have tried initializing Panel() and calling it with no success. What can I do here?
class MyPanel extends JPanel {
Boolean drawBlue = false;
public void drawBlueCircle( Boolean draw ) {
drawBlue = draw;
repaint();
}
protected void paintComponent( Graphics g ) {
if ( drawBlue ) {
g.setColor(Color.BLUE);
g.fillOval(200, 200, 400, 50);
}
}
}
Then in your button's actionPerformed method call
myPanel.drawBlueCircle(true);
where myPanel is the instance of MyPanel that you created.