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

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

ID: 3850979 • Letter: N

Question

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;

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.

Explanation / Answer

Here is the modified code. It just needed a minor change of multiplying by 5 with direction in the listener classes.

Note: Since you did not provide me the code for RTModel, it was difficult to test it. Please let me know by posting comments whether or not it worked for you. I can help you.

If the answer helped, please don't forget to rate the answer. Thank you very much.

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 * 5);

}

}

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

/**

* 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 * 5);

}

}

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

/**

* 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());

}

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

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote