summaryrefslogtreecommitdiffstats
path: root/qtjava/javalib/examples/picture/PictureDisplay.java
blob: 2f311d51521a7effc0ed06ede1136cdf883c5cdd (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
/****************************************************************************
** $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 PictureDisplay extends TQWidget		// picture display widget
{

static void paintCar( TQPainter p )			// paint a car
{
    TQPointArray a = new TQPointArray();
    TQBrush brush = new TQBrush( Qt.yellow(), Qt.SolidPattern );
    p.setBrush( brush );			// use solid, yellow brush

    a.setPoints( 5, new short[] { 50,50, 350,50, 450,120, 450,250, 50,250 } );
    p.drawPolygon( a );			// draw car body

    TQFont f = new TQFont( "courier", 12, TQFont.Bold, false );
    p.setFont( f );

    TQColor windowColor = new TQColor( 120, 120, 255 );	// a light blue color
    brush.setColor( windowColor );		// set this brush color
    p.setBrush( brush );			// set brush
    p.drawRect( 80, 80, 250, 70 );		// car window
    p.drawText( 180, 80, 150, 70, Qt.AlignCenter, "--  Qt  --\nTrolltech AS" );

    TQPixmap pixmap = new TQPixmap();
    if ( pixmap.load("flag.bmp") )		// load and draw image
	p.drawPixmap( 100, 90, pixmap );

    p.setBackgroundMode( Qt.OpaqueMode );		// set opaque mode
    p.setBrush( Qt.DiagCrossPattern );		// black diagonal cross pattern
    p.drawEllipse( 90, 210, 80, 80 );		// back wheel
    p.setBrush( Qt.CrossPattern );		// black cross fill pattern
    p.drawEllipse( 310, 210, 80, 80 );		// front wheel
}



private TQPicture   pict;
private String	name;

public PictureDisplay( String fileName )
{
    pict = new TQPicture();
    name = fileName;
    if ( !pict.load(fileName) ) {		// cannot load picture
	pict = null;
	name = "Not able to load picture: " + fileName;
    }
}

protected void paintEvent( TQPaintEvent event )
{
    TQPainter paint = new TQPainter( this );			// paint widget
    if ( pict != null )
    	paint.drawPicture( pict );		// draw picture
    else
	paint.drawText( rect(), AlignCenter, name );
}

protected void keyPressEvent( TQKeyEvent k )
{
//   switch ( tolower(k.ascii()) ) {
    switch ( k.ascii() ) {
	case 'r':				// reload
	    pict.load( name );
	    update();
	    break;
	case 'q':				// quit
	    TQApplication.exit();
	    break;
    }
}


static public void main( String[] args )
{
    TQApplication a = new TQApplication( args );		// TQApplication retquired!

    String fileName = "car.pic";			// default picture file name

    if ( args.length == 1 )				// use argument as file name
	fileName = args[0];

    if ( !TQFile.exists(fileName) ) {
	TQPicture pict = new TQPicture();				// our picture
	TQPainter paint = new TQPainter();				// our painter

	paint.begin( pict );			// begin painting onto picture
	paintCar( paint );				// paint!
	paint.end();				// painting done

	pict.save( fileName );			// save picture
	TQMessageBox.information(null, "Qt picture example", "Saved.  Run me again!");
	return;
    } else {
	PictureDisplay test = new PictureDisplay( fileName );		// create picture display
	a.setMainWidget( test);			// set main widget
	test.show();				// show it

	a.exec();				// start event loop
	return;
    }
}

static {
	qtjava.initialize();
}

}