Question: Your task is to create a custom Swing GUI component Head that extends JPanel. To practice working with Java graphics, this component displays a simple

Your task is to create a custom Swing GUI component Head that extends JPanel. To practice working with Java graphics, this component displays a simple human head that, to also practice the Swing event handling mechanism, reacts to the mouse cursor entering and exiting the surface of that component. You should copy-paste a bunch of boilerplate code from the example class ShapePanel into this class. Your class should contain one field a private boolean mouseInside, and the following methods:

public Head()

The constructor of the class should first set the preferred size of this component to be 500 by 500 pixels, and then give this component a decorative raised bevel border using the utility method BorderFactory.createBevelBorder. Next, this constructor adds a MouseListener to this component, this event listener object constructed from an inner class MyMouseListener that extends MouseAdapter. Override both methods mouseEntered and mouseExited inside your event listener class to first set the value of the field mouseInside accordingly and then call repaint.

@Override public void paintComponent(Graphics g)

Renders some kind of image of a simple human head on the surface of this component. This is not an art course so this head does not need to look fancy, but a couple of simple rectangles and ellipses suffice. (Of course, interested students of more artistic bent might want to try out more complicated shapes from the package java.awt.geom to produce a more aesthetically pleasing image.) If the value of the field mouseInside is true, the eyes should be drawn to be open, whereas if mouseInside is false, the eyes should be drawn to be closed. (As long as the eyes are somehow noticeably visually different based on the mouse entering and exiting the component surface, that is enough for this lab.)

To admire your reactive Swing component on the screen, write a separate HeadMain class that contains a main method that creates a JFrame that contains four separate Head components arranged in a neat 2-by-2 grid using the GridLayout layout manager.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!