summaryrefslogtreecommitdiffstats
path: root/qtjava/javalib/examples/hello/Hello.java
blob: c5e85041e3fb018f19a3b730643c7b8efbb2a0a9 (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
/***************************************************************************
* $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.kde.qt.*;

class Hello  extends QWidget
{
private    String t;
private    int     b = 0;


/*
  Constructs a Hello widget. Starts a 40 ms animation timer.
*/
Hello( String text )
{
	this(text, null, null);
}

Hello( String text, QWidget parent, String name )
{
    super(parent,name);
	t = text;
    QTimer timer = new QTimer(this);
    connect( timer, SIGNAL("timeout()"), SLOT("animate()") );
    timer.start( 40 );

    resize( 260, 130 );
}


/*
  This private slot is called each time the timer fires.
*/

void animate()
{
    b = (b + 1) & 15;
    repaint( false );
}


/*
  Handles mouse button release events for the Hello widget.

  We emit the clicked() signal when the mouse is released inside
  the widget.
*/

protected void mouseReleaseEvent( QMouseEvent e )
{
    if ( rect().contains( e.pos() ) )
        emit("clicked");
}


    static int sin_tbl[] = {
        0, 38, 71, 92, 100, 92, 71, 38,	0, -38, -71, -92, -100, -92, -71, -38};

/*
  Handles paint events for the Hello widget.

  Flicker-free update. The text is first drawn in the pixmap and the
  pixmap is then blt'ed to the screen.
*/
protected void paintEvent( QPaintEvent e )
{
    if ( t.equals("") )
        return;

    // 1: Compute some sizes, positions etc.
    QFontMetrics fm = fontMetrics();
    int w = fm.width(t) + 20;
    int h = fm.height() * 2;
    int pmx = width()/2 - w/2;
    int pmy = height()/2 - h/2;

    // 2: Create the pixmap and fill it with the widget's background
    QPixmap pm = new QPixmap( w, h );
    pm.fill( this, pmx, pmy );

    // 3: Paint the pixmap. Cool wave effect
    QPainter p = new QPainter();
    int x = 10;
    int y = h/2 + fm.descent();
    int i = 0;
    p.begin( pm );
    p.setFont( font() );
    while ( i < t.length() ) {
        int i16 = (b+i) & 15;
        p.setPen( new QColor((15-i16)*16,255,255,QColor.Hsv) );
        p.drawText( x, y-sin_tbl[i16]*h/800, t.substring(i,i+1), 1 );
        x += fm.width( t.substring(i,i+1) );
        i++;
    }
    p.end();

    // 4: Copy the pixmap to the Hello widget
    bitBlt( this, pmx, pmy, pm );
}
}