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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
/***************************************************************************
* $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.*;
class Scribble extends QMainWindow
{
protected Canvas canvas;
protected QSpinBox bPWidth;
protected QToolButton bPColor, bSave, bClear;
class Canvas extends QWidget
{
void setPenColor( QColor c )
{ pen.setColor( c ); }
void setPenWidth( int w )
{ pen.setWidth( w ); }
QColor penColor()
{ return pen.color(); }
int penWidth()
{ return pen.width(); }
protected QPen pen;
protected QPointArray polyline;
protected boolean mousePressed;
protected QPixmap buffer = new QPixmap();
public boolean no_writing = false;
Canvas( QWidget parent )
{
this(parent, null);
}
Canvas( QWidget parent, String name )
{
super( parent, name, WStaticContents );
pen = new QPen( Qt.red(), 3 );
polyline = new QPointArray(3);
if ((qApp().args().length > 0) && !buffer.load(qApp().args()[0]))
buffer.fill( colorGroup().base() );
setBackgroundMode( QWidget.PaletteBase );
setCursor( Qt.crossCursor() );
}
void save( String filename, String format )
{
if ( !no_writing )
buffer.save( filename, format.toUpperCase() );
}
void clearScreen()
{
buffer.fill( colorGroup().base() );
repaint( false );
}
protected void mousePressEvent( QMouseEvent e )
{
mousePressed = true;
polyline.setPoint(0, e.pos());
polyline.setPoint(1, e.pos());
polyline.setPoint(2, e.pos());
}
protected void mouseReleaseEvent( QMouseEvent e )
{
mousePressed = false;
}
protected void mouseMoveEvent( QMouseEvent e )
{
if ( mousePressed ) {
QPainter painter = new QPainter();
painter.begin( buffer );
painter.setPen( pen );
polyline.setPoint(2, polyline.at(1));
polyline.setPoint(1, polyline.at(0));
polyline.setPoint(0, e.pos());
painter.drawPolyline( polyline );
painter.end();
QRect r = polyline.boundingRect();
r = r.normalize();
r.setLeft( r.left() - penWidth() );
r.setTop( r.top() - penWidth() );
r.setRight( r.right() + penWidth() );
r.setBottom( r.bottom() + penWidth() );
bitBlt( this, r.x(), r.y(), buffer, r.x(), r.y(), r.width(), r.height() );
}
}
protected void resizeEvent( QResizeEvent e )
{
super.resizeEvent( e );
int w = width() > buffer.width() ?
width() : buffer.width();
int h = height() > buffer.height() ?
height() : buffer.height();
QPixmap tmp = new QPixmap( buffer );
buffer.resize( w, h );
buffer.fill( colorGroup().base() );
bitBlt( buffer, 0, 0, tmp, 0, 0, tmp.width(), tmp.height() );
}
protected void paintEvent( QPaintEvent e )
{
super.paintEvent( e );
ArrayList rects = e.region().rects();
for ( int i = 0; i < rects.size(); i++ ) {
QRect r = (QRect) rects.get(i);
bitBlt( this, r.x(), r.y(), buffer, r.x(), r.y(), r.width(), r.height() );
}
}
}
//------------------------------------------------------
Scribble( )
{
this(null, null);
}
Scribble( QWidget parent, String name )
{
super( parent, name );
canvas = new Canvas( this );
setCentralWidget( canvas );
QToolBar tools = new QToolBar( this );
bSave = new QToolButton( new QIconSet(), "Save", "Save as PNG image", this, SLOT(" slotSave()"), tools );
bSave.setText( "Save as..." );
tools.addSeparator();
bPColor = new QToolButton( new QIconSet(), "Choose Pen Color", "Choose Pen Color", this, SLOT(" slotColor()"), tools );
bPColor.setText( "Choose Pen Color..." );
tools.addSeparator();
bPWidth = new QSpinBox( 1, 20, 1, tools );
QToolTip.add( bPWidth, "Choose Pen Width" );
connect( bPWidth, SIGNAL(" valueChanged( int )"), this, SLOT(" slotWidth( int )") );
bPWidth.setValue( 3 );
tools.addSeparator();
bClear = new QToolButton( new QIconSet(), "Clear Screen", "Clear Screen", this, SLOT(" slotClear()"), tools );
bClear.setText( "Clear Screen" );
}
void slotSave()
{
QPopupMenu menu = new QPopupMenu( null );
HashMap formats = new HashMap();
for ( int i = 0; i < QImageIO.outputFormats().size(); i++ ) {
String str = (String) QImageIO.outputFormats().get( i );
formats.put( new Integer(menu.insertItem( str + "..." )), str );
}
menu.setMouseTracking( true );
int id = menu.exec( bSave.mapToGlobal( new QPoint( 0, bSave.height() + 1 ) ) );
if ( id != -1 ) {
String format = (String) formats.get( new Integer(id) );
String filename = QFileDialog.getSaveFileName( "", "*." + format.toLowerCase(), this );
if ( !filename.equals("") )
canvas.save( filename, format );
}
}
void slotColor()
{
QColor c = QColorDialog.getColor( canvas.penColor(), this );
if ( c.isValid() )
canvas.setPenColor( c );
}
void slotWidth( int w )
{
canvas.setPenWidth( w );
}
void slotClear()
{
canvas.clearScreen();
}
}
|