Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. package myclock;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Graphics;
  5. import java.awt.Graphics2D;
  6.  
  7. import javax.swing.JComponent;
  8. import javax.swing.JFrame;
  9.  
  10. public class Display {
  11. private String title;
  12. private int size;
  13. private static JFrame frame;
  14.  
  15. // Current clock time
  16. private static int hours = 0, minutes = 0;
  17.  
  18.  
  19. public Display(String title,int size) {
  20. this.title=title;
  21. this.size=size;
  22. createDisplay();
  23. }
  24.  
  25. public void createDisplay() {
  26. // Create a JFrame where we will put our clock
  27. frame = new JFrame(title);
  28.  
  29. // Set up the frame
  30. frame.setSize(size, size);
  31. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  32. frame.setResizable(false);
  33.  
  34. // Add a Component object inside the frame. This Component object
  35. // is responsible for drawing shapes and it does so by overriding
  36. // the paint method of the Component class. See PaintComponent
  37. // definition below
  38. frame.add(new PaintComponent());
  39.  
  40. frame.setVisible(true);
  41.  
  42. }
  43.  
  44. public void setTime(int hours, int minutes) {
  45. this.hours = hours;
  46. this.minutes = minutes;
  47.  
  48. // Let the system know we have to redraw. This will call
  49. // PaintComponent.paint() which will call drawClock()
  50. // which will use the updated hours and minutes values to redraw
  51. // the clock
  52. frame.repaint();
  53. }
  54.  
  55. /*
  56. * drawClock
  57. *
  58. * Helper function to draw the clock and its hands
  59. *
  60. * Arguments:
  61. * g2D: Graphics2D object to use when drawing
  62. */
  63. private static void drawClock(Graphics2D g2D) {
  64. // This is a test line to show you this is working. Still need to draw
  65. // a clock instead
  66. g2D.drawString(String.valueOf(hours) + ":" + String.valueOf(minutes),
  67. 100, 100);
  68.  
  69. // TODO: Draw clock face
  70. // e.g g2D.drawOval(0, 0, 300, 300);
  71.  
  72.  
  73. // TODO: Draw clock hands
  74. // Use the hours and minutes variable to determine
  75. // what the current time is.
  76.  
  77. }
  78.  
  79. static class PaintComponent extends JComponent {
  80.  
  81.  
  82. // This callback function is called every time the system feels the
  83. // need to paint the component (window resized, previously obscured
  84. // area now visible etc.). It helps to think of it this way: the
  85. // function clears up the Component and draws whatever is written below
  86. @Override
  87. public void paint(Graphics g) {
  88. // g is a Graphics type but Graphics doesn't support drawing
  89. // shapes so we cast it to Graphics2D
  90. Graphics2D g2d = (Graphics2D) g;
  91. g2d.setColor(Color.BLACK);
  92. drawClock(g2d);
  93.  
  94. }
  95.  
  96. }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement