summaryrefslogtreecommitdiffstats
path: root/qtjava/javalib/examples/aclock/AnalogClock.java
blob: 99dd902aae6f493f535744182435bc343e189ed8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/****************************************************************************
** $Id$
**
** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
**
** This file is part of an example program for Qt.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/

import org.trinitydesktop.qt.*;
import java.util.Date;

class AnalogClock extends TQWidget		// analog clock widget
{

private Date	time;

//
// Constructs an analog clock widget that uses an internal TQTimer.
//

public AnalogClock( TQWidget parent )
{
	this(parent, null);
}

public AnalogClock( TQWidget parent, String name )
{
	super( parent, name );
    time = new Date();		// get current time
    TQTimer internalTimer = new TQTimer( this );	// create internal timer
    connect( internalTimer, TQ_SIGNAL("timeout()"), TQ_SLOT("timeout()") );
    internalTimer.start( 5000 );		// emit signal every 5 seconds
}


public void setTime( Date t )
{
    time = t;
    timeout();
}

//
// The TQTimer.timeout() signal is received by this slot.
//

public void timeout()
{
    Date old_time = new Date();	// get the current time
	old_time.setTime( time.getTime() - 5000 );
	time.setTime( time.getTime() + 5000 );
    if ( old_time.getMinutes() != time.getMinutes() ) {	// minute has changed
	if (autoMask())
	    updateMask();
	else
	    update();
    }
}


public void paintEvent( TQPaintEvent event )
{
    if ( autoMask() )
	return;
    TQPainter paint = new TQPainter( this );
    paint.setBrush( colorGroup().foreground() );
    drawClock( paint );
}

// If the clock is transparent, we use updateMask()
// instead of paintEvent()

public void updateMask()	// paint clock mask
{
    TQBitmap bm = new TQBitmap( size() );
    bm.fill( color0() );			//transparent

    TQPainter paint = new TQPainter();
    paint.begin( bm, this );
    paint.setBrush( color1() );		// use non-transparent color
    paint.setPen( color1() );

    drawClock( paint );

    paint.end();
    setMask( bm );
}

//
// The clock is painted using a 1000x1000 square coordinate system, in
// the a centered square, as big as possible.  The painter's pen and
// brush colors are used.
//
public void drawClock( TQPainter paint )
{
    paint.save();
    
    paint.setWindow( -500,-500, 1000,1000 );

    TQRect v = paint.viewport();
    int d = v.width() < v.height() ? v.width() : v.height();
    paint.setViewport( v.left() + (v.width()-d)/2,
			v.top() + (v.height()-d)/2, d, d );

    // time = TQTime.currentTime();
    TQPointArray pts = new TQPointArray();

    paint.save();
    paint.rotate( 30*(time.getHours()%12-3) + time.getMinutes()/2 );
    pts.setPoints( 4, new short[] { -20,0,  0,-20, 300,0, 0,20 } );
    paint.drawConvexPolygon( pts );
    paint.restore();

    paint.save();
    paint.rotate( (time.getMinutes()-15)*6 );
    pts.setPoints( 4, new short[] { -10,0, 0,-10, 400,0, 0,10 } );
    paint.drawConvexPolygon( pts );
    paint.restore();

    for ( int i=0; i<12; i++ ) {
	paint.drawLine( 440,0, 460,0 );
	paint.rotate( 30 );
    }

    paint.restore();
}


public void setAutoMask(boolean b)
{
    if (b)
	setBackgroundMode( PaletteForeground );
    else
	setBackgroundMode( PaletteBackground );
    super.setAutoMask(b);
}

public static void main( String[] args )
{
    TQApplication a = new TQApplication( args );
    AnalogClock clock = new AnalogClock(null, null);
    if ( args.length == 1 && args[0].equals("-transparent") )
        clock.setAutoMask( true );
    clock.resize( 100, 100 );
    a.setMainWidget( clock );
    clock.setCaption("Qt Example - Analog Clock");
    clock.show();
    int result = a.exec();
    return;
}

static {
	qtjava.initialize();
}

}