java编程之GUI小试

import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.lang.Thread;
import java.text.DecimalFormat;
public class StillClock extends JPanel {
 /**
  * @param args
  */
 private int hour;
 private int minute;
 private int second;
 
 //构造函数
 public StillClock() {
  setCurrentTime();
 }
 //返回小时
 public int getHour() {
  return hour;
 }
 
 
 public int getMinute() {
  return minute;
 }
 
 
 
 public int getSecond() {
  return second;
 }
 
 
 //绘制时钟
 protected void paintComponent(Graphics g) {
  
  super.paintComponent(g);
  //初始化
  int clockRadius = (int)(Math.min(getWidth(), getHeight()) * 0.8 * 0.5);
  int xCenter = getWidth() / 2;
  int yCenter = getHeight() / 2;
  //画圆
  g.setColor(Color.black);
  g.drawOval(xCenter - clockRadius, yCenter - clockRadius, 2 * clockRadius, 2 * clockRadius);
  g.drawString("12", xCenter - 5, yCenter - clockRadius + 15);
  g.drawString("9", xCenter - clockRadius + 3, yCenter + 5);
  g.drawString("3", xCenter + clockRadius - 10, yCenter + 3);
  g.drawString("6", xCenter - 3, yCenter + clockRadius - 3);
  //画秒针
  int sLength = (int)(clockRadius * 0.8);
  int xSecond = (int)(xCenter + sLength * Math.sin(second * (2 * Math.PI / 60)));
  int ySecond = (int)(yCenter - sLength * Math.cos(second * (2 * Math.PI / 60)));
  
  g.setColor(Color.red);
  g.drawLine(xCenter, yCenter, xSecond, ySecond);
  
  //画分针
  int mLenth = (int)(clockRadius * 0.65);
  int xMinute = (int)(xCenter + mLenth * Math.sin(minute * (2 * Math.PI / 60)));
  int yMinute = (int)(xCenter - mLenth * Math.cos(minute * (2 * Math.PI / 60)));
  g.setColor(Color.blue);
  g.drawLine(xCenter, yCenter, xMinute, yMinute);
  
  //画时针
  int hLength = (int)(clockRadius * 0.5);
  int xHour = (int)(xCenter + hLength * Math.sin((hour % 12 + minute / 60.0) * (2 * Math.PI / 12)));
  int yHour = (int)(yCenter - hLength * Math.cos((hour % 12 + minute / 60.0) * (2 * Math.PI / 12)));
  g.setColor(Color.green);
  g.drawLine(xCenter, yCenter, xHour, yHour);
  
  //画数字时钟
  g.setColor(Color.black);
  DecimalFormat s=new DecimalFormat("00");
  g.drawString(s.format(getHour()) + ":" + s.format(getMinute()) + ":" + s.format(getSecond()), xCenter - 22, yCenter - clockRadius - 15);
 }
 
 public void setCurrentTime() {
  Calendar calendar = new GregorianCalendar();
  
  this.hour = calendar.get(Calendar.HOUR_OF_DAY);
  this.minute = calendar.get(Calendar.MINUTE);
  this.second = calendar.get(Calendar.SECOND);
 }
 
 
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  JFrame frame = new JFrame("DiaplayClock");
                frame.setResizable(false);
  frame.setTitle("DiaplayClock");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setSize(300,350);
  frame.setVisible(true);
  
  while(true) {
   StillClock clock = new StillClock();
   frame.getContentPane().add(clock);
   clock.setVisible(true);
   frame.validate();
   try {
    Thread.sleep(1000);
    
   }
   catch (InterruptedException e) {
    e.printStackTrace();  
   }
   clock.setVisible(false);
   frame.remove(clock);
   clock = null;
   frame.validate();
   
  }
 }
}


转载于:https://my.oschina.net/u/2428195/blog/486690

Java基础篇中的StillClock类用于画出时钟以方便Java进阶篇P228第二十九章多线程(第八版)ClockWithAudio类的调用。 package 多线程练习; import java.awt.*; import javax.swing.*; import java.util.*; public class StillClock extends JPanel { private int hour; private int minute; private int second; /** Construct a default clock with the current time */ public StillClock() { setCurrentTime(); } /** Construct a clock with specified hour, minute, and second */ public StillClock(int hour, int minute, int second) { this.hour = hour; this.minute = minute; this.second = second; } /** Return hour */ public int getHour() { return hour; } /** Set a new hour */ public void setHour(int hour) { this.hour = hour; repaint(); } /** Return minute */ public int getMinute() { return minute; } /** Set a new minute */ public void setMinute(int minute) { this.minute = minute; repaint(); } /** Return second */ public int getSecond() { return second; } /** Set a new second */ public void setSecond(int second) { this.second = second; repaint(); } /** Draw the clock */ protected void paintComponent(Graphics g) { super.paintComponent(g); // Initialize clock parameters int clockRadius = (int) (Math.min(getWidth(), getHeight()) * 0.8 * 0.5); int xCenter = getWidth() / 2; int yCenter = getHeight() / 2; // Draw circle g.setColor(Color.black); g.drawOval(xCenter - clockRadius, yCenter - clockRadius, 2 * clockRadius, 2 * clockRadius); g.drawString("12", xCenter - 5, yCenter - clockRadius + 12); g.drawString("9", xCenter - clockRadius + 3, yCenter + 5); g.drawString("3", xCenter + clockRadius - 10, yCenter + 3); g.drawString("6", xCenter - 3, yCenter + clockRadius - 3); // Draw second hand int sLength = (int) (clockRadius * 0.8); int xSecond = (int) (xCenter + sLength * Math.sin(second * (2 * Math.PI / 60))); int ySecond = (int) (yCenter - sLength * Math.cos(second * (2 * Math.PI / 60))); g.setColor(Color.red); g.drawLine(xCenter, yCenter, xSecond, ySecond); // Draw minute hand int mLength = (int) (clockRadius * 0.65); int xMinute = (int) (xCenter + mLength * Math.sin(minute * (2 * Math.PI / 60))); int yMinute = (int) (yCenter - mLength * Math.cos(minute * (2 * Math.PI / 60))); g.setColor(Color.blue); g.drawLine(xCenter, yCenter, xMinute, yMinute); // Draw hour hand int hLength = (int) (clockRadius * 0.5); int xHour = (int) (xCenter + hLength * Math.sin((hour % 12 + minute / 60.0) * (2 * Math.PI / 12))); int yHour = (int) (yCenter - hLength * Math.cos((hour % 12 + minute / 60.0) * (2 * Math.PI / 12))); g.setColor(Color.BLACK); g.drawLine(xCenter, yCenter, xHour, yHour); } public void setCurrentTime() { // Construct a calendar for the current date and time Calendar calendar = new GregorianCalendar(); // Set current hour, minute and second this.hour = calendar.get(Calendar.HOUR_OF_DAY); this.minute = calendar.get(Calendar.MINUTE); this.second = calendar.get(Calendar.SECOND); } public Dimension getPreferredSize() { return new Dimension(200, 200); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值