Input hours and minutes to implement the hour and minute-hour and image graphics in a circle

Source: Internet
Author: User

Import java. AWT .*;
Import java. AWT. Geom .*;
Import javax. Swing. event .*;

Import javax. Swing .*;

Public class clocktest
{
Public static void main (string [] ARGs ){
// Add a frame
Texttestframe frame = new texttestframe ();
Frame. setdefaclocloseoperation (jframe. exit_on_close );
Frame. setvisible (true );
}
}

// Create a clock Frame
Class TextTestFrame extends JFrame
{
/**
*
*/
Private static final long serialVersionUID = 1L;

Public TextTestFrame ()
{
SetTitle ("TextTest ");

// Draw the bottom line
JPanel panel = new JPanel ();
JLabel label = new JLabel ("Hours :");
Panel. add (label );
Hour_text = new JTextField ("12", 3 );
Panel. add (hour_text );
JLabel label2 = new JLabel ("Minutes :");
Panel. add (label2 );
Minute_text = new JTextField ("00", 3 );
Panel. add (minute_text );
Add (panel, BorderLayout. SOUTH );

// Add the listener
ClockFieldListener listener = new clockFieldListener ();
Hour_text.getDocument (). addDocumentListener (listener );
Minute_text.getDocument (). addDocumentListener (listener );

// Draw clock panel
ClockPanel = new DrawPanel1 ();
Add (clockPanel, BorderLayout. CENTER );
Pack ();
}

Public void DrawClock ()
{
Try
{
ClockPanel. setTime (Integer. parseInt (hour_text.getText (), Integer
. ParseInt (minute_text.getText ()));
ClockPanel. repaint ();
} Catch (NumberFormatException e ){
//
}
}

Public JTextField hour_text;
Public JTextField minute_text;
Private DrawPanel1 clockPanel;

Public class clockFieldListener implements DocumentListener
{
Public void insertUpdate (DocumentEvent event ){
DrawClock ();
}

Public void changedUpdate (DocumentEvent event ){

}

Public void removeUpdate (deleentevent event ){
DrawClock ();
}
}
Class DrawPanel1 extends JPanel
{

Private static final long serialVersionUID = 1L;
Public DrawPanel1 ()
{
SetPreferredSize (new Dimension (2 * RADIUS + 1, 2 * RADIUS + 1 ));
}

Public void paintComponent (Graphics g)
{// -- This is where it is. If it is switched out during debugging, it will be stopped here again.
Super. paintComponent (g );
Graphics2D g2 = (Graphics2D) g;

// Draw the circle
Ellipse2D elli = new Ellipse2D. Double ();
Elli. setFrameFromCenter (new Point2D. Double (RADIUS, RADIUS ),
New Point2D. Double (0, 0 ));
G2.draw (elli );

// Draw the hour hand minute hand
Double hourAngle = Math. toRadians (90-360 * minute/(12*60 ));
DrawHand (g2, hourAngle, HOUR_HAND_LENGTH );

// Draw the minute hand
Double minuteAngle = Math. toRadians (90-360 * minute/60 );
DrawHand (g2, minuteAngle, MINUTE_HAND_LENGTH );
}

Public void DrawHand (Graphics2D g2, double angle, double length)
{
Point2D p = new Point2D. Double (RADIUS + length * Math. cos (angle ),
RADIUS-length * Math. sin (angle ));
G2.draw (new Line2D. Double (p, new Point2D. Double (RADIUS, RADIUS )));
}

Public void setTime (int h, int m)
{
Minute = h * 60 + m;
}

Public static final int RADIUS = 100;
Private double minute = 0;
Private double HOUR_HAND_LENGTH = 0.6 * RADIUS;
Private double MINUTE_HAND_LENGTH = 0.8 * RADIUS;
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.