SHOW:
|
|
- or go back to the newest paste.
| 1 | package hw461; | |
| 2 | ||
| 3 | /*********************************************************** | |
| 4 | * Program Assignment #1 (CIS 461, Fall 2016) * | |
| 5 | * Submitted By: Jason Dorsey, Michael White * | |
| 6 | * SID: 0001502537 0001385222 * | |
| 7 | * Date: 9/29/2017 * | |
| 8 | ***********************************************************/ | |
| 9 | ||
| 10 | /** | |
| 11 | * CIS 461: Formal Methods for Software Engineering | |
| 12 | * FM Radio Demonstration | |
| 13 | * The radio project class: RadioProj.java | |
| 14 | * | |
| 15 | * @author Haiping Xu | |
| 16 | * Revised on Sept. 15, 2016 | |
| 17 | **/ | |
| 18 | ||
| 19 | ||
| 20 | import java.awt.BorderLayout; | |
| 21 | import java.awt.Container; | |
| 22 | import java.awt.event.ActionEvent; | |
| 23 | import java.awt.event.ActionListener; | |
| 24 | ||
| 25 | import javax.swing.JButton; | |
| 26 | import javax.swing.JFrame; | |
| 27 | import javax.swing.JPanel; | |
| 28 | ||
| 29 | public class RadioProj implements Runnable, ActionListener {
| |
| 30 | private final static int freqTop = 108; | |
| 31 | private final static int freqBottom = 88; | |
| 32 | private float frequency = freqTop; | |
| 33 | private double[] lockFrequency = {105.9, 101.8, 98.5, 95.6, 92.1};
| |
| 34 | private DisplayPanel display; | |
| 35 | private JButton on; | |
| 36 | private JButton off; | |
| 37 | private JButton scan; | |
| 38 | private JButton reset; | |
| 39 | private volatile Thread searchChannel; | |
| 40 | ||
| 41 | public void init() {
| |
| 42 | JFrame frame = new JFrame("FM Radio");
| |
| 43 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
| 44 | Container pane = frame.getContentPane(); | |
| 45 | ||
| 46 | display=new DisplayPanel("FM Radio");
| |
| 47 | pane.add(display, BorderLayout.CENTER); | |
| 48 | ||
| 49 | JPanel p = new JPanel(); | |
| 50 | on = new JButton("on");
| |
| 51 | on.addActionListener(this); | |
| 52 | p.add(on); | |
| 53 | ||
| 54 | off = new JButton("off");
| |
| 55 | off.addActionListener(this); | |
| 56 | p.add(off); | |
| 57 | ||
| 58 | scan = new JButton("scan");
| |
| 59 | scan.addActionListener(this); | |
| 60 | p.add(scan); | |
| 61 | ||
| 62 | reset = new JButton("reset");
| |
| 63 | reset.addActionListener(this); | |
| 64 | p.add(reset); | |
| 65 | ||
| 66 | pane.add(p, BorderLayout.SOUTH); | |
| 67 | ||
| 68 | frame.pack(); | |
| 69 | frame.setSize(350, 300); | |
| 70 | frame.setVisible(true); | |
| 71 | } | |
| 72 | ||
| 73 | public static void main(String[] argv) {
| |
| 74 | RadioProj radio = new RadioProj(); | |
| 75 | radio.init(); | |
| 76 | } | |
| 77 | ||
| 78 | private boolean scanner = false; | |
| 79 | @Override | |
| 80 | public void actionPerformed(ActionEvent e) {
| |
| 81 | // TODO Auto-generated method stub | |
| 82 | if (e.getActionCommand().equals("on")) {
| |
| 83 | display.turnOn(); | |
| 84 | frequency = freqTop; | |
| 85 | display.setValue(frequency); | |
| 86 | } | |
| 87 | else if (e.getActionCommand().equals("off")) {
| |
| 88 | display.turnOff(); | |
| 89 | // searchChannel.stop(); -- deprecated, do not use | |
| 90 | searchChannel = null; | |
| 91 | } | |
| 92 | else if (e.getActionCommand().equals("scan")) {
| |
| 93 | if (display.isOn()) scanning(); | |
| 94 | } | |
| 95 | else if (e.getActionCommand().equals("reset")) {
| |
| 96 | if (display.isOn()) reset(); | |
| 97 | }; | |
| 98 | } | |
| 99 | ||
| 100 | ||
| 101 | // ====>>>>> Complete the methods below this line! <<<<<==== | |
| 102 | ||
| 103 | private void scanning() {
| |
| 104 | ||
| 105 | // ==> 1. Add your code here! | |
| 106 | ||
| 107 | if (!this.scanner) {
| |
| 108 | this.searchChannel = new Thread(this); | |
| 109 | this.searchChannel.start(); | |
| 110 | this.scanner = true; | |
| 111 | } | |
| 112 | else this.scanner = false; | |
| 113 | } | |
| 114 | ||
| 115 | private void reset() {
| |
| 116 | // ==> 2. Add your code here! | |
| 117 | this.display.setValue(this.frequency); | |
| 118 | this.frequency = (float)108.0; | |
| 119 | } | |
| 120 | ||
| 121 | @Override | |
| 122 | public void run() {
| |
| 123 | // ==> 3. Add your code here! | |
| 124 | while ((this.searchChannel != null) && (this.frequency > freqBottom)) {
| |
| 125 | this.frequency -= 0.1; | |
| 126 | this.display.setValue(this.frequency); | |
| 127 | try {
| |
| 128 | Thread.sleep(100); | |
| 129 | } | |
| 130 | catch (InterruptedException e) {
| |
| 131 | e.printStackTrace(); | |
| 132 | } | |
| 133 | int freq = -1; | |
| 134 | for (int x = 0; x < 5; x++) | |
| 135 | if (Math.abs(this.frequency - this.lockFrequency[x]) < 0.01) | |
| 136 | freq = x; | |
| 137 | if (freq != -1) | |
| 138 | break; | |
| 139 | } | |
| 140 | this.scanner = false; | |
| 141 | } | |
| 142 | } |