Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

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

Assignment - java program using array of objectsobjectives

Assignment - 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 Menu ...

Assessment socket programmingtaskwrite a java gui program

Assessment: Socket Programming Task Write a JAVA GUI program that would facilitate text chatting/exchanging between two or multiple computers over the network/internet, using the concept of JAVA socket programming. If yo ...

Overviewyou are required to use java se 80 and javafx to

Overview You are required to use Java SE 8.0 and JavaFX to develop a Graphical User Interface (GUI) for the FlexiRent rental property management program created in Assignment 1. This assignment is designed to help you: 1 ...

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 ...

Simple order processing systemquestion given the classes

Simple Order Processing System Question: Given the classes Ship (with getter and setter), Speedboat, and SpeedboatTest. Answer the following questions: Refine the whole application (all classes) and create Abstract class ...

Solving 2nd degree equationsbull write the following java

Solving 2nd degree equations • Write the following Java methods • boolean real-sols(double a, double b, double c): it returns true if the 2nd degree equation ax2 + bx + c has real solutions • double solution1(double a, d ...

Part a specification - robot simulationpart a

PART A Specification - Robot Simulation PART A Requirements To complete this assignment you will use the supplied eclipse project Robot P1/. It is already set up to execute a simple arm movement loop which you will build ...

Assessment database and multithread programmingtasktask 1

Assessment: Database and Multithread Programming Task Task 1: Grade Processing University grading system maintains a database called "GradeProcessing" that contains number of tables to store, retrieve and manipulate stud ...

Can someone help me please with those question1what is the

Can someone help me please with those question 1:what is the best data type for student id datatime,currency,number,decimal 2:which relationshipis preferable? one to one,one to many,many to many 3:if you add table A's pr ...

Overviewyou are required to use java se 80 and javafx to

Overview You are required to use Java SE 8.0 and JavaFX to develop a Graphical User Interface (GUI) for the FlexiRent rental property management program created in Assignment 1. This assignment is designed to help you: 1 ...

  • 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