import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import java.awt.Color;
public class EventHand {
public static void main(String[] args) {
JFrame frame = new JFrame ();
frame.setSize(1000,1000);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
draw Shapes = new draw ();
frame.add(Shapes);
frame.setLocationRelativeTo(null);
frame.setTitle("Cool");
}
}
public class draw extends JPanel {
public JButton day;
public JButton night;
public draw() {
setLayout(new FlowLayout());
day= new JButton("Day");
night = new JButton("Night");
add(day);
add(night);
// building the class name The Handler
theHandler handler = new theHandler();
day.addActionListener(handler);
night.addActionListener(handler);
}
public class theHandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
Color color = getBackground();
if(event.getSource()== day)
color = Color.YELLOW;
else if (event.getSource()== night)
color = Color.BLACK;
repaint();
}
}
public void drawing() {
repaint();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.WHITE);
g.setColor(Color.BLUE);
g.fillRect(600,400,150,125);
g.setColor(Color.RED);
g.fillRoundRect(500,500,350,100,50,40);
//Bumper
g.setColor(Color.BLUE);
g.fillRect(525,600,300,40);
// windshield Wiper
g.setColor(Color.BLACK);
g.drawLine(660, 440, 700, 500);
// HeadLights
g.setColor(Color.WHITE);
g.fillOval(570,560,40,30);
g.setColor(Color.WHITE);
g.fillOval(750,560,40,30);
g.setColor(Color.BLACK);
g.fillOval(740,640,40,60);
// TiRE
g.setColor(Color.BLACK);
g.fillOval(585,640,40,60);
}
}