- /**
- *
- */
- package com.suziwen;
- import com.trolltech.qt.core.QPoint;
- import com.trolltech.qt.core.QTime;
- import com.trolltech.qt.core.QTimer;
- import com.trolltech.qt.gui.QApplication;
- import com.trolltech.qt.gui.QColor;
- import com.trolltech.qt.gui.QHideEvent;
- import com.trolltech.qt.gui.QPaintEvent;
- import com.trolltech.qt.gui.QPainter;
- import com.trolltech.qt.gui.QPen;
- import com.trolltech.qt.gui.QPolygon;
- import com.trolltech.qt.gui.QShowEvent;
- import com.trolltech.qt.gui.QWidget;
- /**
- * @author suziwen
- *
- */
- public class Clock extends QWidget {
- QTimer timer = new QTimer();
- static QPolygon hourHand = new QPolygon();
- static QPolygon minuteHand = new QPolygon();
- static QPolygon secondHand = new QPolygon();
- static QColor hourColor = new QColor(127, 0, 127);
- static QColor minuteColor = new QColor(0,129,129,129);
- static QColor secondColor = new QColor(0,100,100,100);
- {
- hourHand.append(new QPoint(7,8));
- hourHand.append(new QPoint(-7,8));
- hourHand.append(new QPoint(0,-30));
- minuteHand.append(new QPoint(7,8));
- minuteHand.append(new QPoint(-7,8));
- minuteHand.append(new QPoint(0,-70));
- secondHand.append(new QPoint(3,8));
- secondHand.append(new QPoint(-3,8));
- secondHand.append(new QPoint(0,-90));
- }
- Clock (){
- this(null);
- // QTimer timer = new QTimer();
- // this.signalSender();
- // timer.start(1000);
- }
- Clock(QWidget parent){
- super(parent);
- timer.timeout.connect(this, "update()");
- }
- public static void main(String[] args){
- QApplication app = new QApplication(args);
- Clock c = new Clock();
- c.resize(200, 200);
- c.show();
- app.exec();
- }
- public void paintEvent(QPaintEvent e){
- QPainter painter = new QPainter(this);
- painter.setRenderHint(QPainter.RenderHint.Antialiasing);
- int side = Math.min(this.width(), this.height());
- painter.translate(this.width()/2, this.height()/2);
- painter.scale(side/200, side/200);
- QTime time = QTime.currentTime();
- painter.setPen(QPen.NoPen);
- painter.setBrush(hourColor);
- painter.save();
- painter.rotate(30.0*(time.hour()+time.minute()/60.0));
- painter.drawConvexPolygon(hourHand);
- painter.restore();
- painter.setPen(hourColor);
- for(int i = 0 ; i < 12 ; i++ ){
- painter.drawLine(0, 82, 0, 92);
- painter.rotate(30.0);
- }
- painter.setPen(QPen.NoPen);
- painter.setBrush(minuteColor);
- painter.save();
- painter.rotate(6.0* (time.minute() + time.second()/60.0));
- painter.drawConvexPolygon(minuteHand);
- painter.restore();
- painter.setPen(minuteColor);
- for ( int i =0 ; i < 60 ; i++){
- if( i%5 != 0){
- painter.drawLine(0, 90, 0, 96);
- }
- painter.rotate(6.0);
- }
- painter.setPen(QPen.NoPen);
- painter.setBrush(secondColor);
- painter.save();
- painter.rotate(6.0 * time.second());
- painter.drawConvexPolygon(secondHand);
- painter.restore();
- }
- public void hideEvent(QHideEvent e){
- timer.stop();
- }
- public void showEvent(QShowEvent e){
- timer.start(1000);
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/030420149225.html
来源: http://www.codesnippet.cn/detail/030420149225.html