<span style="font-family: Arial, Helvetica, sans-serif;">package com.diyitian.zaiyixun;</span>
import java.awt.Graphics;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.JFrame;
import javax.swing.JPanel;
 
public class TimeTest extends JFrame {
 
   public TimeTest()
   {
      ClockPanel jp_clock=new ClockPanel();
      add(jp_clock);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setSize(400,400);
      this.setLocationRelativeTo(null);
      setVisible(true);
      Thread ClockRun=new Thread(jp_clock);
      ClockRun.start();
   }
   public static void main(String[] args) {
       
      new TimeTest();
   }
}
class ClockPanel extends JPanel implements Runnable
{
    private double CenterX,CenterY;
    private double Radius=100;
    private double HPointX,HPointY,MPointX,MPointY,SPointX,SPointY;
    private int Year,Month,Day,WeekDay,Hour,Minute,Second;
    private final double PI=3.141592653;
   public synchronized void paintComponent(Graphics g)
   {
      super.paintComponent(g);
      CenterX=this.getWidth()/2;
      CenterY=this.getHeight()/2;
     
      double HRadian=(PI/6)*Hour+(PI/360)*Minute+(PI/21600)*Second;
      double MRadian=(PI/30)*Minute+(PI/1800)*Second;
      double SRadian=(PI/30)*Second;
      HPointX=CenterX+Radius*0.6*Math.sin(HRadian);
      HPointY=CenterY-Radius*0.6*Math.cos(HRadian);
      MPointX=CenterX+Radius*0.8*Math.sin(MRadian);
      MPointY=CenterY-Radius*0.8*Math.cos(MRadian);
      SPointX=CenterX+Radius*Math.sin(SRadian);
      SPointY=CenterY-Radius*Math.cos(SRadian);
      //画表盘
      g.drawOval((int)(CenterX-Radius), (int)(CenterY-Radius), (int)(2*Radius),(int)(2*Radius));//椭圆
      g.drawString("12",(int)(CenterX),(int)(CenterY-Radius)+10);//字符串
      g.drawString("3",(int)(CenterX+Radius)-6 , (int)(CenterY));
      g.drawString("6", (int)(CenterX), (int)(CenterY+Radius));
      g.drawString("9",(int)(CenterX-Radius), (int)(CenterY));
    
      g.drawLine((int)(CenterX), (int)(CenterY), (int)(SPointX), (int)(SPointY));//画线
      g.drawLine((int)(CenterX), (int)(CenterY), (int)(MPointX), (int)(MPointY));
      g.drawLine((int)(CenterX), (int)(CenterY), (int)(HPointX), (int)(HPointY));
    
      g.drawString(WeekDay+" "+Month+" "+Day+" "+Hour+":"+Minute+":"+Second+" "+Year, 20, 350);
   }
   @Override
   public void run() {
        
      while(true)
      {
         try {
            setCurrentTime();
            repaint();
            Thread.sleep(1000);
         } catch (InterruptedException e) {
              
            e.printStackTrace();
         }
    
      }
     }
   public void setCurrentTime()
   {
      Calendar calendar=new GregorianCalendar();
      Year=calendar.get(Calendar.YEAR);
      Month=calendar.get(Calendar.MONTH);
      Day=calendar.get(Calendar.DAY_OF_MONTH);
      WeekDay=calendar.get(Calendar.DAY_OF_WEEK);
      Hour=calendar.get(Calendar.HOUR);
      Minute=calendar.get(Calendar.MINUTE);
      Second=calendar.get(Calendar.SECOND);
    
   }
}

实现二、

三个类。

1、主函数类

public class Programe extends JFrame {
    /*
     * 四个部分: 1.画出窗体和面板 2.画出指针 3.时间转换的算法 4.让指针动起来
     */
    public static void main(String[] string) {
        Programe frame = new Programe();// 创建窗体对象
        frame.setVisible(true);// 设置窗体可见,没有该语句,窗体将不可见,此语句必须有,否则没有界面就没有意义
    }

    public Programe() {
        setUndecorated(false);// 打开窗体修饰
        setAlwaysOnTop(true);// 窗体置顶
        getContentPane().setLayout(new BorderLayout()); // 在窗体的基础上加入面板:Panel   而后就可以在面板上进行其他操作
                                                        // 指定的组件之间的水平间距构造一个边界布局
        setBounds(100, 30, 217, 257);// 用于设置窗口尺寸和位置;
        ClockPaint clockPaint = new ClockPaint();// 创建时钟面板
        getContentPane().add(clockPaint);

        new Thread() {// 继承Thread类创建线程,更新时钟面板界面
            @Override
            public void run() {// 重写run方法
                try {
                    while (true) {
                        sleep(1000);// 休眠1秒
                        clockPaint.repaint();// 重新绘制时钟面板界面
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();// 在命令行打印异常信息在程序中出错的位置及原因。
                }
            }
        }.start();
    }
}

2、画时钟的类。

package clock;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class ClockPaint extends JPanel {
    private static final BasicStroke H = new BasicStroke(4);// 指针粗细
    private static final BasicStroke M = new BasicStroke(3);
    private static final BasicStroke S = new BasicStroke(2);
    private final static int secLen = 60; // 指针长度
    private final static int minuesLen = 55;
    private final static int hoursLen = 36;
    ImageIcon background;// 背景
    private int X;// 中心坐标
    private int Y;

    public ClockPaint() {
    
        background = new ImageIcon(getClass().getResource("时钟.jpg"));// 加载图片
        int Width = background.getIconWidth();// 获取图片宽度
        X = Width / 2 + 2;// 获取图片中间坐标
        int Height = background.getIconHeight();// 获取图片长度
        Y = Height / 2 - 8;// 获取图片中间坐标
        setPreferredSize(new Dimension(Width, Height));// 设置最好的大小(固定用法)
    }

    public void paint(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        g2.drawImage(background.getImage(), 0, 0, this);
        ClockData data = new ClockData(secLen, minuesLen, hoursLen);
        // 绘制时针
        g2.setStroke(H);// 设置时针的宽度
        g2.setColor(Color.RED);// 设置时针的颜色
        g2.drawLine(X, Y, X + data.hX, Y - data.hY);// 绘制时针
        // 绘制分针
        g2.setStroke(M);// 设置分针的宽度
        g2.setColor(Color.orange);// 设置时针的颜色
        g2.drawLine(X, Y, X + data.mX, Y - data.mY);// 绘制分针
        // 绘制秒针
        g2.setStroke(S);// 设置秒针的宽度
        g2.setColor(Color.GREEN);// 设置时针的颜色
        g2.drawLine(X, Y, X + data.sX, Y - data.sY);// 绘制秒针
        // 绘制中心圆
        g2.setColor(Color.BLUE);
        g2.fillOval(X - 5, Y - 5, 10, 10);

    }
}

3、获取时钟的数据

package clock;


import static java.util.Calendar.HOUR;
import static java.util.Calendar.MINUTE;
import static java.util.Calendar.SECOND;

import java.util.Calendar;

public class ClockData {
    public int sX, sY, mX, mY, hX, hY;

    public ClockData(int secLen, int minuesLen, int hoursLen) {
        Calendar calendar = Calendar.getInstance();// 获取日历对象
        int sec = calendar.get(SECOND);// 获取秒值
        int minutes = calendar.get(MINUTE);// 获取分值
        int hours = calendar.get(HOUR);// 获取时值
        // 计算角度
        int hAngle = hours * 360 / 12 + (minutes / 2) ;// 时针角度(每分钟时针偏移角度)
        int sAngle = sec * 6; // 秒针角度
        int mAngle = minutes * 6 + (sec / 10);// 分针角度
        // 计算秒针、分针、时针指向的坐标
        sX = (int) (secLen * Math.sin(Math.toRadians(sAngle)));// 秒针指向点的X坐标(将角度转换为弧度)
        sY = (int) (secLen * Math.cos(Math.toRadians(sAngle))); // 秒针指向点的Y坐标
        mX = (int) (minuesLen * Math.sin(Math.toRadians(mAngle))); // 分针指向点的X坐标
        mY = (int) (minuesLen * Math.cos(Math.toRadians(mAngle))); // 分针指向点的Y坐标
        hX = (int) (hoursLen * Math.sin(Math.toRadians(hAngle))); // 时针指向点的X坐标
        hY = (int) (hoursLen * Math.cos(Math.toRadians(hAngle))); // 时针指向点的Y坐标

    }

}

import javax.swing.*;  
import java.awt.*;  
import java.text.*;  
import java.util.Timer;  
import java.util.TimerTask;  
  
public class SimpleClock extends JFrame {  
    private final Timer timer;  
    private final JLabel clockLabel;  
  
    public SimpleClock() {  
        clockLabel = new JLabel();  
        clockLabel.setFont(new Font("Serif", Font.BOLD, 72));  
        clockLabel.setHorizontalAlignment(JLabel.CENTER);  
        clockLabel.setVerticalAlignment(JLabel.CENTER);  
        clockLabel.setForeground(Color.black);  
        clockLabel.setBackground(Color.white);  
        clockLabel.setOpaque(true);  
        add(clockLabel);  
  
        timer = new Timer(1000, e -> {  
            updateTime();  
        });  
        timer.start();  
    }  
  
    private void updateTime() {  
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");  
        String currentTime = sdf.format(new Date());  
        clockLabel.setText(currentTime);  
    }  
  
    public static void main(String[] args) {  
        SimpleClock clock = new SimpleClock();  
        clock.setSize(400, 400);  
        clock.setVisible(true);  
        clock.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
    }  
}

这个程序创建了一个继承自JFrameSimpleClock类,其中包含一个标签clockLabel用于显示时间。在SimpleClock的构造函数中,我们创建了一个Timer对象,每秒钟调用一次updateTime()方法。updateTime()方法使用SimpleDateFormat类来获取当前时间,并将其格式化为字符串,然后更新clockLabel的文本。在main()方法中,我们创建了一个SimpleClock对象,并设置窗口的大小和可见性,以及在关闭窗口时退出程序。 

-----------------------

java算法总结

一、引言

算法是计算机科学的核心,它是解决问题的步骤或过程。一个好的算法可以更有效地使用计算机资源,快速准确地解决问题。Java语言由于其跨平台特性和丰富的标准库,已经成为许多程序员的首选语言。以下是一些常见的Java算法和它们的实现。

二、排序算法

  1. 冒泡排序
    冒泡排序是一种简单的排序算法,它重复地遍历待排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来。遍历数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成。

  2. public void bubbleSort(int[] array) {  
        for (int i = 0; i < array.length - 1; i++) {  
            for (int j = 0; j < array.length - i - 1; j++) {  
                if (array[j] > array[j + 1]) {  
                    // swap array[j+1] and array[j]  
                    int temp = array[j];  
                    array[j] = array[j + 1];  
                    array[j + 1] = temp;  
                }  
            }  
        }  
    }

  3. 快速排序
    快速排序是一种分而治之的排序算法。它选择一个"基准"元素,将数组分为两部分,一部分小于基准,一部分大于基准。然后对这两部分递归地进行快速排序。以下是快速排序的Java实现:

  4. public void quickSort(int[] array, int low, int high) {  
        if (low < high) {  
            int pivotIndex = partition(array, low, high);  
            quickSort(array, low, pivotIndex - 1);  
            quickSort(array, pivotIndex + 1, high);  
        }  
    }  
      
    private int partition(int[] array, int low, int high) {  
        int pivot = array[high]; // Choose the last element as pivot  
        int i = (low - 1); // Index of smaller element  
        for (int j = low; j < high; j++) {  
            // If current element is smaller than or equal to pivot  
            if (array[j] <= pivot) {  
                i++; // increment index of smaller element  
                // Swap current element with element at index i  
                int temp = array[i];  
                array[i] = array[j];  
                array[j] = temp;  
            }  
        }  
        // Swap pivot element with the element at index i+1  
        int temp = array[i + 1];  
        array[i + 1] = array[high];  
        array[high] = temp;  
        return i + 1;  
    }

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐