当前位置:编程学习 > JAVA >>

Java钟表线程问题

谁来看看我的钟表的秒针为什么不正常运动呢?是不是逻辑判断有误还是什么别的原因?最好是能正确编译的并且运行成功的

import java.awt.*;
import javax.swing.*;

public class Clock extends JFrame {
 MyPanel mp = null;

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Clock clock = new Clock();
 }

 public Clock() {
  mp = new MyPanel();
  this.add(mp);
  this.setSize(500, 500);
  this.setTitle("一个简单的时钟");
  this.setResizable(false);
  this.setLocation(300, 250);
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  this.setVisible(true);
 }

}

class MyPanel extends JPanel {
 JPanel jp;
 int x = 200;
 int y = 100;

 public MyPanel() {
  Thread t = new Thread(new TimeThread(this));
  t.start();
 }

 public MyPanel(int x,  int y) {
  this.x = x;
  this.y = y;
  jp = new JPanel();
  this.add(jp);
 }

 public void paint(Graphics g) {
  super.paint(g);
  g.setColor(Color.CYAN);
  g.fillOval(100, 100, 200, 200);

  g.setColor(Color.BLACK);
  g.fillRect(198, 200, 5, 5);

  g.setColor(Color.BLACK);
  g.drawLine(x, y ,200, 200);

  g.setColor(Color.RED);
  g.setFont(new Font("宋体", Font.BOLD, 20));
  g.drawString("12", 200, 100);

  g.setColor(Color.RED);
  g.setFont(new Font("宋体", Font.BOLD, 20));
  g.drawString("3", 300, 200);

  g.setColor(Color.RED);
  g.setFont(new Font("宋体", Font.BOLD, 20));
  g.drawString("6", 200, 300);

  g.setColor(Color.RED);
  g.setFont(new Font("宋体", Font.BOLD, 20));
  g.drawString("9", 100, 200);

 }
}

class TimeThread implements Runnable {
 MyPanel mp = null;

 public TimeThread(MyPanel mp) {
  this.mp = mp;
 }

 public void run() {
  while (true) {
 
  if (mp.x>=200&&mp.x<=300&&mp.y>=100&&mp.y<=200)
   {
    mp.x +=100*Math.sin(6);
    mp.y +=(100-100*Math.cos(6));
   }
  
  else if (mp.x>=200 &&mp.x<=300&& mp.y >= 200&&mp.y<=300) {
    mp.x -= 100*Math.sin(6);
    mp.y += (100-100*Math.cos(6));
   }
  else  if (mp.x>=100 &&mp.x<=200&& mp.y >= 200&&mp.y<=300) {
   mp.x -= 100*Math.sin(6);
   mp.y -= (100-100*Math.cos(6));
   }
  else if (mp.x>=100 &&mp.x<=200&& mp.y >= 100&&mp.y<=200) {
    mp.x +=100*Math.sin(6);
    mp.y -= (100-100*Math.cos(6));
   }

   try{
    mp.repaint();
    Thread.sleep(1000);
   } catch (Exception e) {
    e.printStackTrace();
   }
  }
 }
}

 

答案:先注释下重新贴出来吧,怎么只有一个针呢??

代码很混乱,看不懂你的意思。

我给我以前写的给你看看吧

 

 

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
/**
 * 时钟界面类
 * @author Kinganpo
 *
 */
public class Clock extends JFrame {
 
    public Clock(int r) {
     //创建时钟钟面类对象
        ClockPaint cp = new ClockPaint(r);
        this.add(cp);
        this.setTitle("Clock");
        this.setSize(3*r, 3*r);     
        this.setVisible(true);
        this.setResizable(false);//设置为不可改变窗口大小
        this.setAlwaysOnTop(true);//设置为窗口总在前面显示
        this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);//窗口‘关闭’按钮事件处理
            }
        });
    }

    public static void main(String[] s) {
        new Clock(100);
    }
}
/**
 *
 * 该类用于显示时钟钟面
 * @author kinganpo
 *
 */
class ClockPaint extends JPanel implements Runnable {

    int x, y, r;//上边距,左边距,钟面半径
    int h, m, s;// 时钟,分钟,秒钟在钟面上形成的角度数(与“12:00:00”指针形成的夹角)
    double rad = Math.PI / 180;//角度1°所对应的弧度

    public ClockPaint(int r) {
        this.x = r/2;
        this.y = r/2;
        this.r = r;
        Calendar now = new GregorianCalendar();//创建当前日期时间对象
        s = now.get(Calendar.SECOND) * 6;// 初始化秒钟角度;一圆360度,秒钟转一周60秒,所以每走1秒角度转过6度
        m = now.get(Calendar.MINUTE) * 6;// 同上
        h = (now.get(Calendar.HOUR_OF_DAY) - 12) * 30//钟面是12时制的,每1个小时所占的角度是360/12=30度
                + now.get(Calendar.MINUTE) / 12 * 6;// 1个小时里时钟经过5个刻度点,所以每12分钟才过1刻度点,
                     //而每个刻度点只见角度相差6度

        Thread t = new Thread(this);//创建一个线程对象
        t.start();//启动线程
    }
/**
 * 方法重写,绘制钟面各元素
 */
    public void paint(Graphics g) {
     Graphics2D g2d = (Graphics2D)g;
     
        // 清屏
     g2d.setColor(Color.BLACK);//设置画笔颜色
     g2d.fillRect(0, 0, r * 3, r * 3);//填充矩形,参数分别为左上角顶点横、纵坐标,宽、高
        // 画圆
     g2d.setColor(Color.WHITE);
     g2d.drawOval(x, y, r * 2, r * 2);//参数分别为矩形的左上角顶点横、纵坐标,矩形宽,矩形高(矩形内切椭圆)
       
        int d = 0,x1 = 0,y1 = 0;//角度数,横坐标,纵坐标
       
        // 刻度点
        for (int i = 0; i < 60; i++) {//循环绘出60个刻度点
            x1 = (int) ((r - 2) * Math.sin(rad * d));//刻度点横坐标 (r - 2):距圆面向内缩进2像素画点;
            y1 = (int) ((r - 2) * Math.cos(rad * d));//刻度点纵坐标 (rad * d):表示的是弧度
            g2d.drawString(".", x + r + x1 - 1, y + r - y1 + 1);//后2个参数表示横纵坐标,即从该坐标处画“.”
            d += 6;//一个圆360度,每隔6度画一个点,共60个点
        }
       
        // 数字
        g2d.setColor(Color.YELLOW);
        d = 30;//时钟刻度数从1开始,角度的基线是“00”秒时的秒钟,刻度‘1’的夹角为30°
        for (int i = 1; i <= 12; i++) {
            x1 = (int) ((r - 10) * Math.sin(rad * d));//(r - 10): 缩进10像素标刻度数
            y1 = (int) ((r - 10) * Math.cos(rad * d));
            g2d.drawString(i + "", x + r + x1 - 4, y + r - y1 + 5);
            d += 30;
        }
        // 时针
        g2d.setColor(Color.GREEN);
        g2d.setStroke(new BasicStroke(3.5f));
        x1 = (int) ((2*r / 5) * Math.sin(rad * h));
        y1 = (int) ((2*r / 5) * Math.cos(rad * h));
        g2d.drawLine(x + r, y + r, x + r + x1, y + r - y1);//画直线, 前2参数表起点坐标,后2参数表终点坐标
        // 分针
        g2d.setColor(Color.BLUE);
        g2d.setStroke(new BasicStroke(2.5f));
        x1 = (int) ((3*r / 5) * Math.sin(rad * m));
        y1 = (int) ((3*r / 5) * Math.cos(rad * m));
        g2d.drawLine(x + r, y + r, x + r + x1, y + r - y1);
        // 秒针
        g2d.setColor(Color.RED);
        g2d.setStroke(new BasicStroke(1.5f));
        x1 = (int) ((4*r / 5) * Math.sin(rad * s));
        y1 = (int) ((4*r / 5) * Math.cos(rad * s));
        g2d.drawLine(x + r, y + r, x + r + x1, y + r - y1);

        // 获得时间、星期、日期
        Calendar now1 = new GregorianCalendar();//获得当前的日期时间对象
        int hour = now1.get(Calendar.HOUR_OF_DAY);//小时,获得当前的时刻<

上一个:java 小问题 创建对象
下一个:JAVA是什么啊

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,