Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package hw461;
- /***********************************************************
- * Program Assignment #1 (CIS 461, Fall 2016) *
- * Submitted By: Jason Dorsey, Michael White *
- * SID: 0001502537 0001385222 *
- * Date: 9/29/2017 *
- ***********************************************************/
- /**
- * CIS 461: Formal Methods for Software Engineering
- * FM Radio Demonstration
- * The radio project class: RadioProj.java
- *
- * @author Haiping Xu
- * Revised on Sept. 15, 2016
- **/
- import java.awt.BorderLayout;
- import java.awt.Container;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- public class RadioProj implements Runnable, ActionListener {
- private final static int freqTop = 108;
- private final static int freqBottom = 88;
- private float frequency = freqTop;
- private double[] lockFrequency = {105.9, 101.8, 98.5, 95.6, 92.1};
- private DisplayPanel display;
- private JButton on;
- private JButton off;
- private JButton scan;
- private JButton reset;
- private volatile Thread searchChannel;
- public void init() {
- JFrame frame = new JFrame("FM Radio");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- Container pane = frame.getContentPane();
- display=new DisplayPanel("FM Radio");
- pane.add(display, BorderLayout.CENTER);
- JPanel p = new JPanel();
- on = new JButton("on");
- on.addActionListener(this);
- p.add(on);
- off = new JButton("off");
- off.addActionListener(this);
- p.add(off);
- scan = new JButton("scan");
- scan.addActionListener(this);
- p.add(scan);
- reset = new JButton("reset");
- reset.addActionListener(this);
- p.add(reset);
- pane.add(p, BorderLayout.SOUTH);
- frame.pack();
- frame.setSize(350, 300);
- frame.setVisible(true);
- }
- public static void main(String[] argv) {
- RadioProj radio = new RadioProj();
- radio.init();
- }
- private boolean scanner = false;
- @Override
- public void actionPerformed(ActionEvent e) {
- // TODO Auto-generated method stub
- if (e.getActionCommand().equals("on")) {
- display.turnOn();
- frequency = freqTop;
- display.setValue(frequency);
- }
- else if (e.getActionCommand().equals("off")) {
- display.turnOff();
- // searchChannel.stop(); -- deprecated, do not use
- searchChannel = null;
- }
- else if (e.getActionCommand().equals("scan")) {
- if (display.isOn()) scanning();
- }
- else if (e.getActionCommand().equals("reset")) {
- if (display.isOn()) reset();
- };
- }
- // ====>>>>> Complete the methods below this line! <<<<<====
- private void scanning() {
- // ==> 1. Add your code here!
- if (!this.scanner) {
- this.searchChannel = new Thread(this);
- this.searchChannel.start();
- this.scanner = true;
- }
- else this.scanner = false;
- }
- private void reset() {
- // ==> 2. Add your code here!
- this.display.setValue(this.frequency);
- this.frequency = (float)108.0;
- }
- @Override
- public void run() {
- // ==> 3. Add your code here!
- while ((this.searchChannel != null) && (this.frequency > freqBottom)) {
- this.frequency -= 0.1;
- this.display.setValue(this.frequency);
- try {
- Thread.sleep(100);
- }
- catch (InterruptedException e) {
- e.printStackTrace();
- }
- int freq = -1;
- for (int x = 0; x < 5; x++)
- if (Math.abs(this.frequency - this.lockFrequency[x]) < 0.01)
- freq = x;
- if (freq != -1)
- break;
- }
- this.scanner = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement