Create a sub-class of the class jpanel named displayarea

Assignment Help JAVA Programming
Reference no: EM131590865

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.

Reference no: EM131590865

Questions Cloud

Compute their tentative minimum tax : Renee and Sanjeev, who are married, report taxable income of $273,000. Compute their tentative minimum tax
How did the discussion debates help you advance : How did the discussion debates help you advance your considerations of the topics within this course? What were the most compelling topics learned in course?
What does the software provide : Thinking of the software that they may have, what does the software provide? Explain how it makes the device/hardware better?
Compute the regular taxable income : Problem - Yanni, a single individual, reports the following information for the tax year. Compute the following for Yanni: Regular taxable income
Create a sub-class of the class jpanel named displayarea : Create a sub-class of the class JPanel named DisplayArea. An object of the class DisplayArea will be used as a canvas
Local government for the protection safety of its citizens : What duty is owed by a local government for the protection/safety of its citizens
The present state of the problem and research focus : Explains both the history of and the present state of the problem and research focus.It identifies the gap or need based on a summary of the current literature.
Energy might make solutions easier : What are two other examples where using energy might make solutions easier?
What is chuck amti for the year : In 2015, he claims a $4,000 personal exemption and records taxable income of $120,000. Chuck's AMT preferences total $51,000. What is Chuck's AMTI for the year

Reviews

Write a Review

JAVA Programming Questions & Answers

  Create a program that has an object type of contact

Create a program that has an object type of Contact. This contact will need to have the following attributes: Name, Birth Date, Phone Number, Address and Eye Color

  Write an application calccircumference

Write an application (CalcCircumference) that inputs from the user the radius of a circle as an integer and prints the circle's diameter, circumference and area using the floating-point value 3.14159 for ?.

  One acre of land is equivalent to

One acre of land is equivalent to 43,560 square feet. Write a program that calculates the number of square feet in a tract of land with 3.5acres.

  Print out the stock with the highest profit if sold

After the table is displayed, print out the stock with the highest profit if sold and its position in the array[row][column].

  Write a program that displays a frame window w pixels wide

Write a program that displays a frame window W pixels wide and H pixels high. Use the Scanner to enter the values for W and H. The title of the frame is also entered by the user.

  The program should ask the user to enter the student'' answer

Your program should store these correct answers in an array. (Store each question's correct answer in an element of a string array.) the program should ask the user to enter the student's answers for each of the 20 questions, which should be stored i..

  Compute area and perimeter of a polygon

Create a project that would let a user compute area and perimeter of a polygon

  Design class to represent any household item

Design and implement your own simple class to represent any household item of your choices (toaster, fan, and hair dryer, piano....)

  Java program for a music social network system

Java program for a Music Social Network system. You can work on this coursework either alone or in a pair with a colleague, not necessarily from the same seminar or lab group

  Write gui program in java to implement linked list to store

Write a GUI program in Java to implement linked list to store a database of students. Store the student's first and last name, ID number, major, and GPA.

  What are the three required expressions of a for-loop

What are the three required expressions of a for-loop. Consider the following code. This code was written to generate the output as shown in

  Code that assigns true to isamember

write code that assigns true to isAMember if the value of memberID can be found in currentMembers , and that assigns false to isAMember otherwise. Use only k , currentMembers , memberID , and isAMember

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd