Ask Java Expert


Home >> Java

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.

Java, Programming

  • Category:- Java
  • Reference No.:- M92399578
  • Price:- $15

Priced at Now at $15, Verified Solution

Have any Question?


Related Questions in Java

Chatbotscreate a small networked chat application that is

Chatbots Create a small, networked chat application that is populated by bots. Introduction On an old server park, filled with applications from the early days of the internet, a few servers still run one of the earliest ...

Assignment taskwrite a java console application that allows

Assignment task Write a java console application that allows the user to read, validate, store, display, sort and search data such as flight departure city (String), flight number (integer), flight distance (integer), fl ...

Assignment game prototypeoverviewfor this assessment task

Assignment: Game Prototype Overview For this assessment task you are expected to construct a prototype level/area as a "proof of concept" for the game that you have designed in Assignment 1. The prototype should function ...

Assignment taskwrite a java console application that allows

Assignment task Write a java console application that allows the user to read, validate, store, display, sort and search data such as flight departure city (String), flight number (integer), flight distance (integer), fl ...

In relation to javaa what is constructor the purpose of

(In relation to Java) A. What is constructor? the purpose of default constructor? B. How do you get a copy of the object but not the reference of the object? C. What are static variables and instance variables? D. Compar ...

Project descriptionwrite a java program to traverse a

Project Description: Write a java program to traverse a directory structure (DirWalker.java) of csv files that contain csv files with customer info. A simple sample in provided in with the sample code but you MUST will r ...

Fundamentals of operating systems and java

Fundamentals of Operating Systems and Java Programming Purpose of the assessment (with ULO Mapping) This assignment assesses the following Unit Learning Outcomes; students should be able to demonstrate their achievements ...

Assessment -java program using array of Assessment -JAVA Program using array of objects

Assessment -JAVA Program using array of objects Objectives This assessment item relates to the course learning outcomes as stated in the Unit Profile. Details For this assignment, you are required to develop a Windowed G ...

Applied software engineering assignment 1 -learning

Applied Software Engineering Assignment 1 - Learning outcomes - 1. Understand the notion of software engineering and why it is important. 2. Analyse the risk factors associated with phases of the software development lif ...

Retail price calculatorwrite a java program that asks the

Retail Price Calculator Write a JAVA program that asks the user to enter an item's wholesale cost and its markup percentage. It should then display the item's retail price. For example: (If an item's wholesale cost is 5. ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As