Some guidelines for this week's studio:
package percent;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
// import javax.swing.event.ChangeEvent;
// import javax.swing.event.ChangeListener;
public class Controller extends JPanel {
public Controller() {
this.add(new JLabel("Controller"));
// add other things you want to see here:
}
public static void main(String[] args) {
Controller panel = new Controller();
//
// What you see below is what YOPS did for you
//
JFrame frame = new JFrame();
frame.add(panel);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Your PercentModel class must have the following method:
public int computePercentOf(int n)The method takes in n and returns this.getValue() percent of n.
In your group, discuss how to write computePercentOf so that it performs arithmetic using the appropriate types.
package percent;
import static org.junit.Assert.*;
import org.junit.Test;
public class PercentTest {
@Test
public void test() {
PercentModel pm = new PercentModel();
int checkType = pm.computePercentOf(0);
// initially, pm at 100%
assertEquals(40, pm.computePercentOf(40));
// try 50%
pm.setValue(50);
assertEquals(20, pm.computePercentOf(40));
// try 50% again (make sure it's not cummulative)
pm.setValue(50);
assertEquals(20, pm.computePercentOf(40));
// try 0%
pm.setValue(0);
assertEquals(0, pm.computePercentOf(40));
// try 300% which should be same as 100%
pm.setValue(300);
assertEquals(40, pm.computePercentOf(40));
// try -300% which should be same as 0%
pm.setValue(-300);
assertEquals(0, pm.computePercentOf(40));
}
}
Instantiate a couple of sliders hooked to the same model and watch them work in concert.
Show this to a TA and to other groups as needed.
Show this to a TA and to other students as needed.
JButton reset = new JButton("reset");
//
// or maybe you'd like a Benjamin Button
//
this.add(reset);
// ...
reset.addActionListener(this);
It's the last line above that will cause eclipse to suggest some
things to you. Get help as you need it.
There is a description of JButton
here, but you only need the bold stuff. You can see how the action events
are handled there at least.