diff options
author | toma <toma@283d02a7-25f6-0310-bc7c-ecb5cbfe19da> | 2009-11-25 17:56:58 +0000 |
---|---|---|
committer | toma <toma@283d02a7-25f6-0310-bc7c-ecb5cbfe19da> | 2009-11-25 17:56:58 +0000 |
commit | 90825e2392b2d70e43c7a25b8a3752299a933894 (patch) | |
tree | e33aa27f02b74604afbfd0ea4f1cfca8833d882a /qtjava/javalib/examples/drawlines | |
download | tdebindings-90825e2392b2d70e43c7a25b8a3752299a933894.tar.gz tdebindings-90825e2392b2d70e43c7a25b8a3752299a933894.zip |
Copy the KDE 3.5 branch to branches/trinity for new KDE 3.5 features.
BUG:215923
git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/kdebindings@1054174 283d02a7-25f6-0310-bc7c-ecb5cbfe19da
Diffstat (limited to 'qtjava/javalib/examples/drawlines')
-rw-r--r-- | qtjava/javalib/examples/drawlines/ConnectWidget.java | 122 | ||||
-rw-r--r-- | qtjava/javalib/examples/drawlines/README | 3 |
2 files changed, 125 insertions, 0 deletions
diff --git a/qtjava/javalib/examples/drawlines/ConnectWidget.java b/qtjava/javalib/examples/drawlines/ConnectWidget.java new file mode 100644 index 00000000..c51f2703 --- /dev/null +++ b/qtjava/javalib/examples/drawlines/ConnectWidget.java @@ -0,0 +1,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 QWidget +{ +static final int MAXPOINTS = 2000; // maximum number of points +static final int MAXCOLORS = 40; + +private QPoint[] points; // point array +private QColor[] 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( QWidget parent, String name ) +{ + super( parent, name, WStaticContents ); + setBackgroundColor( white() ); // white background + count = 0; + down = false; + points = new QPoint[MAXPOINTS]; + colors = new QColor[MAXCOLORS]; + for ( int i=0; i<MAXCOLORS; i++ ) // init color array + colors[i] = new QColor( generator.nextInt(255), generator.nextInt(255), generator.nextInt(255) ); +} + + +// +// Handles paint events for the connect widget. +// + +protected void paintEvent( QPaintEvent e ) +{ + QPainter paint = new QPainter( 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( QMouseEvent e ) +{ + down = true; + count = 0; // start recording points + erase(); // erase widget contents +} + + +// +// Handles mouse release events for the connect widget. +// + +protected void mouseReleaseEvent( QMouseEvent e ) +{ + down = false; // done recording points + update(); // draw the lines +} + + +// +// Handles mouse move events for the connect widget. +// + +protected void mouseMoveEvent( QMouseEvent e ) +{ + if ( down && count < MAXPOINTS ) { + QPainter paint = new QPainter( this ); + points[count++] = new QPoint(e.pos()); // add point + paint.drawPoint( e.pos() ); // plot point + paint.end(); + } +} + + +// +// Create and display a ConnectWidget. +// + +public static void main( String[] args ) +{ + QApplication a = new QApplication( args ); + ConnectWidget connect = new ConnectWidget(); + connect.setCaption( "Qt Example - Draw lines"); + a.setMainWidget( connect ); + connect.show(); + a.exec(); + return; +} + +static { + qtjava.initialize(); +} + +} diff --git a/qtjava/javalib/examples/drawlines/README b/qtjava/javalib/examples/drawlines/README new file mode 100644 index 00000000..2a7c577c --- /dev/null +++ b/qtjava/javalib/examples/drawlines/README @@ -0,0 +1,3 @@ +The connect program connects points with lines. Drag the mouse +inside the widget and release it. It was inspired by an example in a +Windows book. |