Question
This is the slider part of a right triangle program used to adjust the height and base. The classes with (// your code) here are
This is the slider part of a right triangle program used to adjust the height and base. The classes with (// your code) here are where I'm stuck at to be filled (Mainly implementing innerclasses).
Side Notes to the program: Have a vertical slider that sets the triangle height. Have a horizontal slider that sets the triangle base. Both sliders need to reflect the current value of the base and height, even if set by another view. Have a label that shows the value of the hypotenuse at any time.
-----Code Below ----- RTSliderView.java ----------------------------------------------------------------------------------------------------------- import java.awt.BorderLayout; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import java.text.DecimalFormat;
import javax.swing.BorderFactory; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JSlider; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener;
/** * View and update the right triangle model with sliders. * */ @SuppressWarnings("serial") public class RTSliderView extends JPanel {
/** * An inner class that accesses the base slider. */ private class BaseSliderListener implements ChangeListener {
@Override public void stateChanged(final ChangeEvent e) {
// your code here
} }
/** * An inner class that accesses the height slider. */ private class HeightSliderListener implements ChangeListener {
@Override public void stateChanged(final ChangeEvent e) {
// your code here
} }
/** * An inner class the updates the sliders and hypotenuse label whenever the * model's attributes are updated. */ private class ValuesListener implements PropertyChangeListener {
@Override public void propertyChange(final PropertyChangeEvent evt) {
// your code here
} }
// --------------------------------------------------------------- /** * The format string for reading / displaying numeric input / output. */ private static final String FORMAT_STRING = "###.##"; /** * The formatters for reading / displaying numeric input / output. */ private static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat(FORMAT_STRING); /** * The slider to adjust the Right Triangle base. */ private final JSlider baseSlider = new JSlider(JSlider.HORIZONTAL, 0, (int) RTModel.MAX_SIDE, 1); /** * The slider to adjust the Right Triangle height. */ private final JSlider heightSlider = new JSlider(JSlider.VERTICAL, 0, (int) RTModel.MAX_SIDE, 1); /** * The hypotenuse value field - cannot be edited by the user. */ private final JLabel hypo = new JLabel(" "); /** * The right triangle model. */ private final RTModel model;
/** * The View constructor. * * @param model The model to view. */ public RTSliderView(final RTModel model) { this.model = model; this.layoutView(); this.registerListeners(); // Initialize the widget values. this.baseSlider.setValue((int) model.getBase()); this.heightSlider.setValue((int) model.getHeight()); this.hypo.setText(DECIMAL_FORMAT.format(model.getHypotenuse())); }
/** * Uses the BorderLayout to place the widgets. */ private void layoutView() { // Define the panel border. this.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); // Lay out the panel. this.setLayout(new BorderLayout()); this.add(this.hypo, BorderLayout.CENTER); this.add(this.heightSlider, BorderLayout.EAST); this.add(this.baseSlider, BorderLayout.SOUTH); }
/** * Assigns listeners to the field widgets and the model. */ private void registerListeners() { // Add widget listeners. this.baseSlider.addChangeListener(new BaseSliderListener()); this.heightSlider.addChangeListener(new HeightSliderListener()); // Add model listeners. this.model.addPropertyChangeListener(new ValuesListener()); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started