jdorsey2018

cis 461 hw1p1

Sep 29th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. package hw461;
  2.  
  3. /**
  4. * Please do not modify this file!!!
  5. */
  6.  
  7. /**
  8. * CIS 461: Formal Methods for Software Engineering
  9. * FM Radio Demonstration
  10. * The radio project class: DisplayPanel.java
  11. *
  12. * @author Haiping Xu
  13. * Revised on Sept. 15, 2016
  14. **/
  15.  
  16.  
  17. import java.awt.*;
  18. import javax.swing.JPanel;
  19.  
  20. public class DisplayPanel extends JPanel {
  21. private static final long serialVersionUID = 1L;
  22. private boolean on = false;
  23. private float frequency = 108;
  24. private String title;
  25.  
  26. private Font f1 = new Font("Helvetica",Font.BOLD,36);
  27. private Font f2 = new Font("Times",Font.ITALIC+Font.BOLD,24);
  28.  
  29. public DisplayPanel(String title) {
  30. this.title = title;
  31. setBackground(Color.green);
  32. }
  33.  
  34. public void setValue(float value){
  35. this.frequency = value;
  36. repaint();
  37. }
  38.  
  39. public boolean isOn() {
  40. return on;
  41. }
  42.  
  43. public void turnOn() {
  44. on = true;
  45. repaint();
  46. }
  47.  
  48. public void turnOff() {
  49. on = false;
  50. repaint();
  51. }
  52.  
  53. public void paintComponent(Graphics g) {
  54. g.setColor(Color.green);
  55. g.fillRect(0, 0, getWidth(), getHeight());
  56.  
  57. // Display the title
  58. g.setFont(f2);
  59. g.setColor(Color.black);
  60. FontMetrics fm = g.getFontMetrics();
  61. int fontWidth = fm.stringWidth(title);
  62. int fontHeight = fm.getHeight();
  63. int x = (getSize().width - fontWidth)/2;
  64. int y = fontHeight;
  65. g.drawString(title, x, y);
  66. g.drawLine(x, y+3, x+fontWidth, y+3);
  67.  
  68. // Display the content
  69. g.setFont(f1);
  70. g.setColor(Color.red);
  71. fm = g.getFontMetrics();
  72. String content;
  73. if (!on) content = "----";
  74. else content = String.valueOf(Math.round(frequency*10)/10.0) + " MHz";
  75. fontWidth = fm.stringWidth(content);
  76. fontHeight = fm.getHeight();
  77. x = (getSize().width - fontWidth)/2;
  78. y = (getSize().height + fontHeight)/2;
  79. g.drawString(content, x, y);
  80. }
  81. }
Add Comment
Please, Sign In to add comment