Question: Download the Java source code file Clock.java ( see below ) into an empty Eclipse project. Then, add Java code ( i . e .

Download the Java source code file Clock.java (see below) into an empty Eclipse project. Then, add Java code (i.e., pen method calls) to the paint method so that when the program is compiled and executed, a clock like in the picture is drawn. Randomly set the time each time the program runs (i.e., random number of hours between 1 and 12, random number of minutes between 1 and 60)
/*
* Clock.java
*/
import java.awt.*;
@SuppressWarnings("serial")
public class Clock extends javax.swing.JFrame
{
public void paint(Graphics g)
{
// draw the window title, menu, buttons
super.paint(g);
// get the Graphics context for the canvas
Graphics pen = canvas.getGraphics();
//*** add pen Graphics method calls after this line ***
/**
**********************************************************
* WARNING!!! DO NOT ADD OR MODIFY CODE BELOW THIS LINE!!! *
**********************************************************
*/
}// end paint
/**
* Creates new form Clock
*/
public Clock()
{
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Clock Editor.
*/
////GEN-BEGIN:initComponents
private void initComponents()
{
canvas = new javax.swing.JPanel();
resetBtn = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Clock (Programming Assignment 4: 2-D Graphics Programs in Java)");
canvas.setBackground(new java.awt.Color(255,255,255));
canvas.setLayout(new java.awt.BorderLayout());
getContentPane().add(canvas, java.awt.BorderLayout.CENTER);
resetBtn.setText("Reset");
resetBtn.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
resetBtnActionPerformed(evt);
}
});
getContentPane().add(resetBtn, java.awt.BorderLayout.PAGE_END);
setSize(new java.awt.Dimension(816,639));
setLocationRelativeTo(null);
}////GEN-END:initComponents
private void resetBtnActionPerformed(java.awt.event.ActionEvent evt){//GEN-FIRST:event_resetBtnActionPerformed
repaint();
}//GEN-LAST:event_resetBtnActionPerformed
/**
* @param args the command line arguments
*/
public static void main(String args[])
{
/* Set the Nimbus look and feel */
//
try
{
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels())
{
if ("Nimbus".equals(info.getName()))
{
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex)
{
java.util.logging.Logger.getLogger(Clock.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex)
{
java.util.logging.Logger.getLogger(Clock.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex)
{
java.util.logging.Logger.getLogger(Clock.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex)
{
java.util.logging.Logger.getLogger(Clock.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//
//
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run()
{
new Clock().setVisible(true);
}
});
}// end main method
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JPanel canvas;
private javax.swing.JButton resetBtn;
// End of variables declaration//GEN-END:variables
}// end Clock class
Download the Java source code file Clock.java (

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!