Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help writing java code. Below are the code of RTButtonView import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import java.text.DecimalFormat; import javax.swing.JButton;

Need help writing java code. Below are the code of RTButtonView

image text in transcribed

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.beans.PropertyChangeEvent;

import java.beans.PropertyChangeListener;

import java.text.DecimalFormat;

import javax.swing.JButton;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.SwingConstants;

*/

@SuppressWarnings("serial")

public class RTButtonView extends JPanel {

// ---------------------------------------------------------------

/**

* An inner class that uses an ActionListener to access the buttons. It sets

* the model values when the button is pressed.

*/

private class BaseButtonListener implements ActionListener {

/**

* Determines whether values are incremented (+) or decremented (-).

*/

private int direction = 0;

public BaseButtonListener(final int direction) {

this.direction = direction;

}

@Override

public void actionPerformed(final ActionEvent evt) {

RTButtonView.this.model.setBase(

RTButtonView.this.model.getBase() + this.direction);

}

}

// -------------------------------------------------------------------------------

/**

* An inner class the updates the base and hypotenuse labels whenever the

* model's base attribute is updated.

*/

private class BaseListener implements PropertyChangeListener {

@Override

public void propertyChange(final PropertyChangeEvent evt) {

RTButtonView.this.base.setText(

RTButtonView.f.format(RTButtonView.this.model.getBase()));

RTButtonView.this.hypo.setText(RTButtonView.f

.format(RTButtonView.this.model.getHypotenuse()));

}

}

// -------------------------------------------------------------------------------

/**

* An inner class that uses an ActionListener to access the buttons. It sets

* the model values when the button is pressed.

*/

private class HeightButtonListener implements ActionListener {

/**

* Determines whether values are incremented (+) or decremented (-).

*/

private int direction = 0;

public HeightButtonListener(final int direction) {

this.direction = direction;

}

@Override

public void actionPerformed(final ActionEvent evt) {

RTButtonView.this.model.setHeight(

RTButtonView.this.model.getHeight() + this.direction);

}

}

// -------------------------------------------------------------------------------

/**

* An inner class the updates the height and hypotenuse labels whenever the

* model's height attribute is updated.

*/

private class HeightListener implements PropertyChangeListener {

@Override

public void propertyChange(final PropertyChangeEvent evt) {

RTButtonView.this.height.setText(

RTButtonView.f.format(RTButtonView.this.model.getHeight()));

RTButtonView.this.hypo.setText(RTButtonView.f

.format(RTButtonView.this.model.getHypotenuse()));

}

}

// -------------------------------------------------------------------------------

/**

* The formatter for displaying numeric output.

*/

private static final DecimalFormat f = new DecimalFormat("###.##");

/**

* Displays the model's base value.

*/

private final JLabel base = new JLabel(" ");

/**

* Decrements base by 1.

*/

private final JButton baseDown = new JButton("-");

/**

* Increments base by 1.

*/

private final JButton baseUp = new JButton("+");

/**

* Displays the model's height value.

*/

private final JLabel height = new JLabel(" ");

/**

* Decrements height by 1.

*/

private final JButton heightDown = new JButton("-");

/**

* Increments height by 1.

*/

private final JButton heightUp = new JButton("+");

/**

* Displays the model's hypotenuse value.

*/

private final JLabel hypo = new JLabel(" ");

/**

* The right triangle model.

*/

private final RTModel model;

// ---------------------------------------------------------------

/**

* The view constructor.

*

* @param newModel

* The right triangle model.

*/

public RTButtonView(final RTModel newModel) {

this.model = newModel;

this.layoutView();

this.registerListeners();

// Initialize the view labels.

this.base.setText(RTButtonView.f.format(this.model.getBase()));

this.height.setText(RTButtonView.f.format(this.model.getHeight()));

this.hypo.setText(RTButtonView.f.format(this.model.getHypotenuse()));

}

// ---------------------------------------------------------------

/**

* Uses the GridLayout to place the labels and buttons.

*/

private void layoutView() {

this.setLayout(new GridLayout(3, 4));

this.add(new JLabel("Base: "));

this.add(this.baseUp);

this.add(this.baseDown);

this.base.setHorizontalAlignment(SwingConstants.RIGHT);

this.add(this.base);

this.add(new JLabel("Height: "));

this.add(this.heightUp);

this.add(this.heightDown);

this.height.setHorizontalAlignment(SwingConstants.RIGHT);

this.add(this.height);

this.add(new JLabel("Hypotenuse: "));

this.add(new JLabel());

this.add(new JLabel());

this.hypo.setHorizontalAlignment(SwingConstants.RIGHT);

this.add(this.hypo);

}

// ---------------------------------------------------------------

/**

* Assigns listeners to the view widgets and the model.

*/

private void registerListeners() {

// Add widget listeners.

this.baseUp.addActionListener(new BaseButtonListener(1));

this.baseDown.addActionListener(new BaseButtonListener(-1));

this.heightUp.addActionListener(new HeightButtonListener(1));

this.heightDown.addActionListener(new HeightButtonListener(-1));

// Add model listeners.

this.model.addPropertyChangeListener(RTModel.BASE_CHANGE,

new BaseListener());

this.model.addPropertyChangeListener(RTModel.HEIGHT_CHANGE,

new HeightListener());

}

// ---------------------------------------------------------------

}

Alter the code for the RTButtonView so that the buttons increment or decrement the base or height by five

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Relational Database Design With Microcomputer Applications

Authors: Glenn A. Jackson

1st Edition

0137718411, 978-0137718412

More Books

Students also viewed these Databases questions

Question

Question How are IRAs treated for state tax law purposes?

Answered: 1 week ago