Here is a Java Program to Make a Sleepy Face Using a AWT Class
Output of Above Java Program
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; /***********class Sleepy*******************/ class Sleepy { public static void main(String[] args) { JFrame windo = new JFrame("Sleepy"); windo.getContentPane().add(new SleepyPanel()); windo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); windo.pack(); windo.show(); } } class SleepyPanel extends JPanel { private Sleeper face = new Sleeper(); SleepyPanel() { JButton awakeButton = new JButton("Awake"); awakeButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { face.setAwake(true); } } ); JButton asleepButton = new JButton("Asleep"); asleepButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { face.setAwake(false); } } ); JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new FlowLayout()); buttonPanel.add(awakeButton); buttonPanel.add(asleepButton); this.setLayout(new BorderLayout()); this.add(buttonPanel, BorderLayout.NORTH); this.add(face, BorderLayout.CENTER); } } class Sleeper extends JPanel { private boolean awake = false; // show face awake or asleep Sleeper() { this.setBackground(Color.lightGray); this.setPreferredSize(new Dimension(400, 400)); // size } public void setAwake(boolean awakeAsleep) { awake = awakeAsleep; // remember expression on face repaint(); // redraw it with new value } public void paintComponent(Graphics g) { super.paintComponent(g); // MUST be first line //--- draw head g.setColor(Color.yellow); g.fillOval(8, 8, 384, 384); //--- draw eyes g.setColor(Color.black); if (awake) { g.fillOval(100, 150, 50, 100); // left eye g.fillOval(250, 150, 50, 100); // right eye } else { g.fillRect(50 , 200, 100, 4); // left eye g.fillRect(250, 200, 100, 4); // left eye } } }
Output of Above Java Program
When Clicked on Awake Button
When Clicked on Asleep Button