summaryrefslogtreecommitdiffstats
path: root/qtjava/javalib/examples/drawlines/ConnectWidget.java
blob: 0292b9a21835f27c21b55597e76884daed7df543 (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
/***************************************************************************
* $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.*;
import java.util.Random;

//
// ConnectWidget - draws connected lines
//
class ConnectWidget  extends TQWidget
{
static final int MAXPOINTS = 2000;			// maximum number of points
static final int MAXCOLORS = 40;

private    TQPoint[]     points;				// point array
private    TQColor[]     colors;				// color array
private    int		count;				// count = number of points
private    boolean	down;				// true if mouse down
private    Random generator = new Random(System.currentTimeMillis());

//
// Constructs a ConnectWidget.
//
public ConnectWidget( )
{
	this(null, null);
}

public ConnectWidget( TQWidget parent, String name )
{
    super( parent, name, WStaticContents );
    setBackgroundColor( white() );		// white background
    count = 0;
    down = false;
    points = new TQPoint[MAXPOINTS];
    colors = new TQColor[MAXCOLORS];
    for ( int i=0; i<MAXCOLORS; i++ )		// init color array
	colors[i] = new TQColor( generator.nextInt(255), generator.nextInt(255), generator.nextInt(255) );
}


//
// Handles paint events for the connect widget.
//

protected void paintEvent( TQPaintEvent e  )
{
    TQPainter paint = new TQPainter( this );
    for ( int i=0; i<count-1; i++ ) {		// connect all points
	for ( int j=i+1; j<count; j++ ) {
	    paint.setPen( colors[generator.nextInt(MAXCOLORS)] ); // set random pen color
	    paint.drawLine( points[i], points[j] ); // draw line
	}
    }
    paint.end();
}


//
// Handles mouse press events for the connect widget.
//

protected void mousePressEvent( TQMouseEvent e  )
{
    down = true;
    count = 0;					// start recording points
    erase();					// erase widget contents
}


//
// Handles mouse release events for the connect widget.
//

protected void mouseReleaseEvent( TQMouseEvent e )
{
    down = false;				// done recording points
    update();					// draw the lines
}


//
// Handles mouse move events for the connect widget.
//

protected void mouseMoveEvent( TQMouseEvent e )
{
    if ( down && count < MAXPOINTS ) {
	TQPainter paint = new TQPainter( this );
	points[count++] = new TQPoint(e.pos());		// add point
	paint.drawPoint( e.pos() );		// plot point
	paint.end();
    }
}


//
// Create and display a ConnectWidget.
//

public static void main( String[] args )
{
    TQApplication a = new TQApplication( args );
    ConnectWidget connect = new ConnectWidget();
    connect.setCaption( "Qt Example - Draw lines");
    a.setMainWidget( connect );
    connect.show();
    a.exec();
	return;
}

static {
	qtjava.initialize();
}

}