Swing-Timing Task (Analog Clock)

Posted by jmajeremy on Tue, 08 Oct 2019 14:10:03 +0200

Design ideas:

1. Design of Start Monitor

Define a Timer object under swing, trigger the timer monitor (redraw the clock panel) every other time, that is to say, the clock can move.

2. Design of clocks and watches

The difficulty lies in drawing the angle of the clock.

Here we add a radiation gradient background to the clock.

See Code Notes for details:

3. Design of Stop Button

That is timer.stop();

 

The three category:

1. Design Class of Bell Surface Plate

2. Design Class of Injection Panel

3, class main

package my3;

import java.awt.Container;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Swing2
{
	private static void createGUI()
	{
		// JFrame refers to a window whose parameters are the title of the window.
		// Syntax: Because MyFrame is a subclass of JFrame, it can be written as follows
		JFrame frame = new MyFrame("Js Clocks and watches");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		
		// Set other parameters of the window, such as window size
		frame.setSize(700, 450);
		
		// Display window
		frame.setVisible(true);
		
		
	}
	
	public static void main(String[] args)
	{
		// This code indirectly calls createGUI()
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run()
			{
				createGUI();
			}
		});

	}
}
package my3;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class MyFrame extends JFrame
{
	CustomClock clock=new CustomClock();
	
	Timer timer;
	
	public MyFrame(String title)
	{
		super(title);
		
		// Content Pane
		JPanel root = new JPanel();
		this.setContentPane(root);
		root.setLayout(new BorderLayout());	
		
		root.add(clock, BorderLayout.CENTER);
		
		// toolbar
		Box toolbar = Box.createHorizontalBox();
		JButton startButton = new JButton("start");
		JButton stopButton = new JButton("Stop it");
		toolbar.add(startButton);
		toolbar.add(Box.createHorizontalStrut(10));
		toolbar.add(stopButton);
		root.add(toolbar, BorderLayout.PAGE_START);
				
		startButton.addActionListener( (e)->{
				startClock();
			});
		stopButton.addActionListener( (e)->{
				stopClock();
			});
	}
	
	private void startClock()
	{
		if(timer!=null) return;
		
		//Create a timer that executes every 1000 milliseconds
		//The first parameter is delay
		//The second parameter is ActionListener, which can be used with anonymous classes or Lamada expressions
//		timer=new Timer(1000,(e)-> {
//			clock.repaint(); // reset clock
//		});
		
		//Anonymous class
		timer=new Timer(1000,new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				
				clock.repaint();
			}
			
		});
		timer.start();
	}
	
	private void stopClock()
	{
		if(timer != null)
		{
			timer.stop();
			timer = null;
		}
	}
}
package my3;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RadialGradientPaint;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.util.Calendar;

import javax.swing.JPanel;

public class CustomClock extends JPanel
{

	@Override
	protected void paintComponent(Graphics g) 
	{
		int width=this.getWidth();
		int height=this.getHeight();
		Graphics2D g2d=(Graphics2D)g;
		
		
		// Smooth rendering (anti-aliasing)
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		g2d.setPaint(Color.white);
		g2d.fillRect(0, 0, width, height);
		
		// Get the largest text
		//Rectangle rect = new Rectangle(0,0,200,200);
		int w = width;
		int h = width;
		if( h > height)
		{
			h = height;
			w = height;
		}
		Rectangle rect = new Rectangle((width-w)/2, (height-h)/2, w, h);		
		rect.grow(-4,-4); // Shrink in a little
		
		//Center point and radius
		int centerX=(int)rect.getCenterX();
		int centerY=(int)rect.getCenterY();
		int radius=(int)rect.width/2;
	
	
		////////////// Round frame of dial/////////////
		if(true)
		{			
			// Background Filled with White
//			g2d.setPaint(new Color(0xFFFFFF));
//			g2d.fillOval(rect.x, rect.y, rect.width, rect.height);
			
			// Frame
			g2d.setStroke(new BasicStroke(4));
			g2d.setPaint(Color.RED);
			g2d.drawOval(rect.x, rect.y, rect.width, rect.height);
		}
		
		
		/////////////// Calibration/////////////////
		if(true)
		{
			g2d.setStroke(new BasicStroke(4));
			for(double angle=0;angle<360;angle+=30)
			{
				drawRadialLine(g2d, centerX, centerY, radius, angle);
			}
			//Calibration by truncating spokes
			radius-=5;
//			g2d.setPaint(Color.WHITE);
//			g2d.fillOval(centerX-radius, centerY-radius, radius*2, radius*2);
			//Draw a small dot in the center
			int r=4;
			g2d.setPaint(Color.RED);
			g2d.fillOval(centerX-r, centerY-r, r*2, r*2);
		}
		
		//Radiation Gradual Filling
		if(true)	
		{
			Point2D center=new Point2D.Double(centerX,centerY);
			float[] dist= {0.0f,0.2f,1.0f};  //distance
			Color[] colors = { Color.RED, Color.WHITE, Color.BLUE };  //colour
			//Gradient gradient
			RadialGradientPaint paint=new RadialGradientPaint(center,radius,dist,colors);
			g2d.setPaint(paint);
			g2d.fillOval(centerX-radius, centerY-radius, radius*2, radius*2);

		}
			
			////////////// Second hand, minute hand, hour hand///////////
			
			//Get the time-seconds value
			Calendar cal=Calendar.getInstance();
			int hour=cal.get(Calendar.HOUR);
			int minute=cal.get(Calendar.MINUTE);
			int second=cal.get(Calendar.SECOND);
			
			//Hour hand
			if(true)
			{
				double radius2=radius*0.5;
				g2d.setPaint(Color.ORANGE);
				g2d.setStroke(new BasicStroke(6));
				
				//Calculating angle
				double vv=hour+minute/60.0;
				drawRadialLine(g2d,centerX,centerY,radius2,vv/12.0*360-90);
			}
			
			// Minute hand
			if(true)
			{
				double radius2 = radius *0.7;
				g2d.setPaint(Color.GREEN);
				g2d.setStroke(new BasicStroke(3));
				
				double vv = minute + second/60.0; // Angle calculation is a little complicated.
				drawRadialLine(g2d, centerX, centerY, radius2, minute/60.0 * 360 - 90);
			}
			
			// Second hand
			if(true)
			{
				double radius2 = radius *0.9;
				g2d.setPaint(Color.RED);
				g2d.setStroke(new BasicStroke(1));			
				drawRadialLine(g2d, centerX, centerY, radius2, second/60.0 * 360 - 90 );
			}	
		
	}

	
	
	// Draw spoke lines, center X, center Y, radius, angle from the center of the circle
	private void drawRadialLine(Graphics2D g2d,
			double centerX, double centerY,
			double radius, double angle)
	{
		double radian=angle/180*Math.PI; //Angle to radian
		
		double x = centerX + radius * Math.cos( radian );
		double y = centerY + radius * Math.sin( radian );	
		
		Line2D line=new Line2D.Double(x, y, centerX, centerY);
		g2d.draw(line);
	}
	
}

 

Topics: Java