Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions src/app/observer/ballistics4/BallisticsLabel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package app.observer.ballistics4;

/*
* Copyright (c) 2001, 2005. Steven J. Metsker.
*
* Steve Metsker makes no representations or warranties about
* the fitness of this software for any particular purpose,
* including the implied warranty of merchantability.
*
* Please use this software as you wish with the sole
* restriction that you may not claim that you wrote it.
*/

import com.oozinoz.utility.Format;

import javax.swing.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

/**
* Instances of this class are labels that show the value of a Tpeak model.
*
* @author Steven J. Metsker
*/
public class BallisticsLabel extends JLabel implements PropertyChangeListener {
/**
* Construct a label that will show the value of a Tpeak object.
*
* @param tPeak
* the model to observe
*/
public BallisticsLabel(Tpeak tPeak) {
tPeak.addObserver(this);
showValue(tPeak.getValue());
}

/**
* Respond to a change in the observed Tpeak model
*
* @param evt
* change event
*/

@Override
public void propertyChange(PropertyChangeEvent evt) {
showValue((double) evt.getNewValue());
}

private void showValue(double value) {
setText(Format.formatToNPlaces(value, 2));
repaint();
}
}
73 changes: 73 additions & 0 deletions src/app/observer/ballistics4/BallisticsPanel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package app.observer.ballistics4;

/*
* Copyright (c) 2001, 2005. Steven J. Metsker.
*
* Steve Metsker makes no representations or warranties about
* the fitness of this software for any particular purpose,
* including the implied warranty of merchantability.
*
* Please use this software as you wish with the sole
* restriction that you may not claim that you wrote it.
*/

import com.oozinoz.ballistics.BallisticsFunction;

import javax.swing.*;
import java.awt.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

/**
* Plot a ballistics function. This class is refactored from BallisticsPanel_2
* to rely on a Tpeak object from the business domain.
*
* @author Steven J. Metsker
*/
public class BallisticsPanel extends JPanel implements PropertyChangeListener {
protected BallisticsFunction func;

protected int nPoint = 101;

double tPeak = 0;

protected int[] x = new int[nPoint];

protected int[] y = new int[nPoint];

/**
* Create a panel that can display the provided function.
*
* @param func
* the ballistics function to plot. Ballistics functions vary
* with time and with the time of peak burn area.
* @param tPeak
* an observable model of the time when burn area peaks
*/
public BallisticsPanel(BallisticsFunction func, Tpeak tPeak) {
this.func = func;
tPeak.addObserver(this);
}

protected void paintComponent(Graphics g) {
super.paintComponent(g); // paint the background
for (int i = 0; i < nPoint; i++) {
double t = ((double) i) / (nPoint - 1);
x[i] = (int) (t * getWidth());
y[i] = (int) (getHeight() * (1 - func.function(t, tPeak)));
}
g.drawPolyline(x, y, nPoint);
}

/**
* Respond to a change in the observed Tpeak model
*
* @param evt
* change event from Tpeak
*/
@Override
public void propertyChange(PropertyChangeEvent evt) {
tPeak = (double) evt.getNewValue();
repaint();
}
}
137 changes: 137 additions & 0 deletions src/app/observer/ballistics4/ShowBallistics4.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package app.observer.ballistics4;

/*
* Copyright (c) 2001, 2005. Steven J. Metsker.
*
* Steve Metsker makes no representations or warranties about
* the fitness of this software for any particular purpose,
* including the implied warranty of merchantability.
*
* Please use this software as you wish with the sole
* restriction that you may not claim that you wrote it.
*/

import com.oozinoz.ballistics.Ballistics;
import com.oozinoz.ui.SwingFacade;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;

/**
* Show the standard burn rate and thrust equations. This class is refactored
* from ShowBallistics2. The changes involve pushing the value of tPeak down
* into a business layer and using a BallisticsLabel that knows about tPeak.
*
* @author Steven J. Metsker
*/
public class ShowBallistics4 {
/**
* Show the standard burn rate and thrust equations.
*/
public static void main(String[] args) {
SwingFacade.launch(new ShowBallistics4().mainPanel(),
"Effects of tPeak");
}

protected BallisticsPanel burnPanel;

protected JSlider slider;

protected double sliderMax;

protected double sliderMin;

protected BallisticsPanel thrustPanel;

protected JLabel valueLabel;

protected Tpeak tPeak = new Tpeak(0);

protected BallisticsPanel burnPanel() {
if (burnPanel == null) {
burnPanel = new BallisticsPanel(Ballistics.rate(), tPeak);
burnPanel.setPreferredSize(new Dimension(300, 200));
}
return burnPanel;
}

/*
* A panel to contain the two plots.
*/
protected JPanel curvePanel() {
JPanel p = new JPanel();
p.setLayout(new GridLayout(1, 2));
p.add(SwingFacade.createTitledPanel("Burn Rate", burnPanel()));
p.add(SwingFacade.createTitledPanel("Thrust", thrustPanel()));
return p;
}

/*
* The main panel -- the one that actually gets displayed.
*/
protected JPanel mainPanel() {
JPanel p = new JPanel();
p.setLayout(new BorderLayout());
p.add(curvePanel(), "Center");
p.add(sliderBox(), "South");
return p;
}

/*
* Now the slider just tells the business domain tPeak object that the
* slider moved.
*/
protected JSlider slider() {
if (slider == null) {
slider = new JSlider();
sliderMax = slider.getMaximum();
sliderMin = slider.getMinimum();
slider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
if (sliderMax == sliderMin) return;

tPeak.setValue((slider.getValue() - sliderMin)
/ (sliderMax - sliderMin));
}
});
slider.setValue(slider.getMinimum());
}
return slider;
}

/*
* The box that holds the slider plus a textual label and a changing label
* that shows the value of the slider.
*/
protected Box sliderBox() {
Box b = Box.createHorizontalBox();
JLabel label = new JLabel("tPeak");
label.setFont(SwingFacade.getStandardFont());
label.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 10));
label.setForeground(java.awt.Color.black);
b.add(label);
b.add(valueLabel());
b.add(slider());
return b;
}

protected BallisticsPanel thrustPanel() {
if (thrustPanel == null) {
thrustPanel = new BallisticsPanel(Ballistics.thrust(), tPeak);
thrustPanel.setPreferredSize(new Dimension(300, 200));
}
return thrustPanel;
}

protected JLabel valueLabel() {
if (valueLabel == null) {
valueLabel = new BallisticsLabel(tPeak);
valueLabel.setFont(SwingFacade.getStandardFont());
valueLabel.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 10));
valueLabel.setForeground(java.awt.Color.black);
}
return valueLabel;
}
}
59 changes: 59 additions & 0 deletions src/app/observer/ballistics4/Tpeak.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package app.observer.ballistics4;

/*
* Copyright (c) 2001, 2005. Steven J. Metsker.
*
* Steve Metsker makes no representations or warranties about
* the fitness of this software for any particular purpose,
* including the implied warranty of merchantability.
*
* Please use this software as you wish with the sole
* restriction that you may not claim that you wrote it.
*/

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

/**
* Instances of this class provide an observably changeable model of the time at
* which a fuel's burn area peaks.
*
* @author Steven J. Metsker
*/
public class Tpeak {
protected double value;
private PropertyChangeSupport support;

/**
* Create a model of the time at which a fuel's burn area peaks.
*
* @param value
* the initial peak time
*/
public Tpeak(double value) {
this.value = value;
this.support = new PropertyChangeSupport(this);
}

/**
* @return the current value of the peak time
*/
public double getValue() {
return value;
}

/**
* @param newValue
* the new value for the peak time
*/
public void setValue(double newValue) {
double oldValue = this.value;
this.value = newValue;
support.firePropertyChange("value", oldValue, newValue);

}

public void addObserver(PropertyChangeListener listener) {
support.addPropertyChangeListener(listener);
}
}
15 changes: 9 additions & 6 deletions src/app/visitor/FindVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,19 @@ public class FindVisitor implements MachineVisitor {

private MachineComponent found;

public FindVisitor(int soughtId) {
this.soughtId = soughtId;
}

/**
* @param mc the composite to look within
* @param id the id of the machine to find
* @return a machine with the given id, within the given machine composite
*/
public MachineComponent find(MachineComponent mc, int id) {
found = null;
soughtId = id;
mc.accept(this);
return found;
public static MachineComponent find(MachineComponent mc, int id) {
FindVisitor visitor = new FindVisitor(id);
mc.accept(visitor);
return visitor.found;
}

/**
Expand All @@ -59,4 +62,4 @@ public void visit(MachineComposite mc) {
while (found == null && iter.hasNext())
iter.next().accept(this);
}
}
}
4 changes: 2 additions & 2 deletions src/app/visitor/ShowFindVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
public class ShowFindVisitor {
public static void main(String[] args) {
MachineComponent factory = OozinozFactory.dublin();
MachineComponent machine = new FindVisitor().find(factory, 3404);
MachineComponent machine = FindVisitor.find(factory, 3404);
System.out.println(machine != null ? machine.toString() : "Not found");
}
}
}
5 changes: 5 additions & 0 deletions src/com/oozinoz/iterator/AcycliclyIterable.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package com.oozinoz.iterator;

import java.util.HashSet;
import java.util.Set;

public interface AcycliclyIterable<E> extends Iterable<E> {
default ComponentIterator<E> iterator() {
return iterator(new HashSet<>());
}

ComponentIterator<E> iterator(Set<E> visited);
}
5 changes: 1 addition & 4 deletions src/com/oozinoz/iterator/ComponentIterator.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,4 @@ public void setShowInterior(boolean value) {
*/
public abstract int getDepth();

public void remove() {
throw new UnsupportedOperationException("ComponentIterator.Remove");
}
}
}
Loading