Create a sub-class of the class jpanel named displayarea an


Create a sub-class of the class JPanel named DisplayArea. An object of the class DisplayArea will be used as a canvas (an area on which the application will draw). Later, we will add what is needed to move the colored point or square on this surface.

You can read more about the class JPanel here:https://docs.oracle.com/javase/8/docs/api/javax/swing/JPanel.html

To make the name JPanel available in this compiling unit, add the following import directive to the beginning of your file, before the class declaration:
import javax.swing.JPanel;

Declare a constant (a public static final class variable) namedDISPLAY_SIZEwith the value of 500

Add a constructor in which you will call the methodsetBackgroundto change the background color of this component. Use the colorjava.awt.Color.WHITE. You can simply useColor.WHITEif you have importedjava.awt.Colorat the beginning of your file with the other import directives.

Finally, you will need to redefine the methodgetPreferredSize. This method returns an object of typejava.awt.Dimension, or simplyDimensionif you have importedjava.awt.Dimension. The method getPreferredSize has to return a new object Dimension whose width and height isDISPLAY_SIZE.

What did we get? When an object DisplayArea will be created, there will be all the characteristics of the class JPanel. However, the background component will be White. When the application will try to determine the size of this graphical application, there will be a call to the method getPreferredSize. This method will return a new object Dimension with the width and height of 500 pixels.
2.1.2 GUI (Graphical User Interface)

Create a subclass of the class JFrame that you will name GUI. This will be the main window of the application.

You can read the documentation about the class JFrame here:https://docs.oracle.com/javase/8/docs/api/javax/swing/JFrame.html

You will need to import the package java.swing.JFrame to make the name JFrame available.
import javax.swing.JFrame;

Add a constructor to the class GUI. In a graphical application, the constructor is usually the place where we construct the graphical representation of the application. This is logical since the constructor is called the moment the object is created. Here is the desired visual:

Figure 2 : the desired visual

To obtain a window like this, you will need to add the following elements to the constructor

Add a title to the window. This can be done in two different ways: by calling the constructor of thesuperclassand passing the title as a parameter (as a String) or by using the methodsetTitle.

It is necessary to add a call the methodSystem.exitwhen we close the window.
setDefaultCloseOperation(EXIT_ON_CLOSE);

You can consult the documentation at the following address for more information: https://docs.oracle.com/javase/8/docs/api/javax/swing/JFrame.html#setDefaultCloseOperation-int-

Create an objectDisplayAreaand add it to the center of the window. Since the classGUIis a subclass of the classJFrame, we can use theLayoutManger. Chose the one that will allow you to add the objectDisplayAreato thelower part of the window

Add the buttons to the lower part of the window. To do so, follow these steps:

Create an objectJPanel. It will act as a container for the buttons. Make sure that the background of this component is white.

Create four object of the classJButton. Use the labels "Left", "Right", "Up", and "Down".
Add these buttons to the objectJPanel. Note that the default layout ofJPanelisFlowLayout. This layout places the object from left to right then up to bottom based on the order that the elements have been added.

Add an object of typeJPanelto the classGUI. Since GUI is a subclass of JFame, it should be easy to add the panel at the desired position.

Finally, add the two calls to the following methods to the constructor. These methods have been inherited from the parent classes. The methodpackis inherited from the classjava.awt.Window, andsetResizablefrom the classjava.awt.Frame.
setResizable(false);
pack();

The first call makes the size of the application fixed, while pack determines the ideal size of the window based on its elements. This call to the method pack forces a call to the method getPreferredSize() from the classDisplayArea.

Add amainmethod of the classGUI. This method has to create an object of the class GUI. The main method also has to make the window visible. To do so, use the methodsetVisiblelike it the previous section.

Compile your application and make sure it resembles the application above.

Solution Preview :

Prepared by a verified Expert
JAVA Programming: Create a sub-class of the class jpanel named displayarea an
Reference No:- TGS02386407

Now Priced at $15 (50% Discount)

Recommended (95%)

Rated (4.7/5)