Class TspDemoControlPanel

All Implemented Interfaces:
SimulationListener, ISimulationControlPanel, ImageObserver, MenuContainer, Serializable, Accessible

public class TspDemoControlPanel extends JPanel implements ISimulationControlPanel, SimulationListener
Control panel for the TSP simulated annealing demo.

Structure

Follows the standard MDI composite control panel pattern:

  • Left — IconSimulationControlPanel providing the standard play/pause/stop media buttons and progress bar.
  • Center — two labeled sliders:
    • Number Cities (MIN_CITIES..MAX_CITIES) — number of randomly placed cities.
    • River Crossing Penalty or Bonus (−1..+1) — adds a constant to the distance of any edge that crosses the river. Positive values penalize crossings (the tour avoids the river); negative values reward them (the tour seeks crossings).
  • Right — Reset button that rebuilds the model and simulation from the current slider values.

State gating

The sliders are enabled in SimulationState.READY, SimulationState.PAUSED, and SimulationState.TERMINATED. The Reset button is enabled in those same three states plus SimulationState.FAILED, so the user can always recover from a failed run. This prevents the user from changing parameters while the simulation thread is actively mutating the model.

Reset behavior

Pressing Reset calls ITspDemoResettable.requestReset(int, double) on the host if it implements that interface, passing the current slider values. The host (TspDemoView) is responsible for stopping the current engine, building a new simulation with the new parameters, and restarting.

An optional auto-reset mode (autoResetOnSliderRelease) can trigger a reset automatically when the user releases a slider knob in an editable state. It is disabled by default.

River slider precision

JSlider operates on integers, so the river penalty slider is scaled by RIVER_SCALE (10^RIVER_NUM_DECIMALS) internally and divided back to a double in getRiverPenalty().

See Also:
  • Field Details

    • MIN_CITIES

      public static final int MIN_CITIES
      Minimum number of cities available on the city slider.
      See Also:
    • MAX_CITIES

      public static final int MAX_CITIES
      Maximum number of cities available on the city slider.
      See Also:
  • Constructor Details

    • TspDemoControlPanel

      public TspDemoControlPanel()
      Construct with the standard MDI icon set.
    • TspDemoControlPanel

      public TspDemoControlPanel(StandardSimIcons icons)
      Construct with a caller-supplied icon set.
      Parameters:
      icons - simulation icons (non-null)
  • Method Details

    • bind

      public void bind(ISimulationHost host)
      Bind this panel to the given simulation host.

      Binds the embedded basePanel to the same host, registers this panel as a SimulationListener, and applies the current engine state immediately.

      Specified by:
      bind in interface ISimulationControlPanel
      Parameters:
      host - the simulation host (non-null; typically TspDemoView)
    • unbind

      public void unbind()
      Unbind this panel from its host.

      Removes the listener from the engine and unbinds the embedded basePanel. The panel becomes inert until bound again.

      Specified by:
      unbind in interface ISimulationControlPanel
    • getCityCount

      public int getCityCount()
      Return the city count currently selected on the slider.
      Returns:
      number of cities in the range [MIN_CITIES, MAX_CITIES]
    • getRiverPenalty

      public double getRiverPenalty()
      Return the river penalty currently selected on the slider.
      Returns:
      penalty in [−1.0, +1.0]; positive values penalize river crossings, negative values reward them
    • onStateChange

      public void onStateChange(SimulationContext ctx, SimulationState from, SimulationState to, String reason)
      Called on every state transition.
      Specified by:
      onStateChange in interface SimulationListener
      Parameters:
      ctx - simulation context
      from - previous state
      to - new state
      reason - human-readable reason (may be null)
    • onReady

      public void onReady(SimulationContext ctx)
      Called when initialization completes and the engine reaches SimulationState.READY.
      Specified by:
      onReady in interface SimulationListener
      Parameters:
      ctx - simulation context
    • onDone

      public void onDone(SimulationContext ctx)
      Called when the engine ends normally (reaches SimulationState.TERMINATED).
      Specified by:
      onDone in interface SimulationListener
      Parameters:
      ctx - simulation context
    • onFail

      public void onFail(SimulationContext ctx, Throwable error)
      Called when the engine fails (reaches SimulationState.FAILED).
      Specified by:
      onFail in interface SimulationListener
      Parameters:
      ctx - simulation context
      error - the exception/error that caused failure
    • onProgress

      public void onProgress(SimulationContext ctx, ProgressInfo progress)
      Called for progress updates (indeterminate or determinate). No-op — progress display is handled by the embedded basePanel.
      Specified by:
      onProgress in interface SimulationListener
      Parameters:
      ctx - simulation context
      progress - progress payload