Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

2.1.1 Display Area Create a sub-class of the class JPanel named DisplayArea . An

ID: 3794162 • Letter: 2

Question

2.1.1 Display Area

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:

Declare a constant (a public static final class variable) named DISPLAY_SIZE with the value of 500

Add a constructor in which you will call the method setBackground to change the background color of this component. Use the color java.awt.Color.WHITE. You can simply use Color.WHITE if you have importedjava.awt.Color at the beginning of your file with the other import directives.

Finally, you will need to redefine the method getPreferredSize. This method returns an object of type java.awt.Dimension, or simply Dimension if you have imported java.awt.Dimension. The method getPreferredSize has to return a new object Dimension whose width and height is DISPLAY_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.

Explanation / Answer

import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JPanel;

public class DisplayArea extends JPanel
{
   public static final int DISPLAY_SIZE = 500;
  
   public DisplayArea() {
       setBackground(Color.WHITE);
   }

   @Override
   public Dimension getPreferredSize() {
       Dimension d=new Dimension();
       d.setSize(DISPLAY_SIZE, DISPLAY_SIZE);
       return d;
   }
   public static void main(String[] args) {
       DisplayArea obj=new DisplayArea();
       obj.setVisible(true);
      
   }
}