-
Notifications
You must be signed in to change notification settings - Fork 2
/
SplashScreen.m
493 lines (437 loc) · 19 KB
/
SplashScreen.m
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
classdef SplashScreen < hgsetget
%SplashScreen create a splashscreen
%
% s = SplashScreen(title,imagefile) creates a splashscreen using
% the specified image. The title is the name of the window as shown
% in the task-bar. Use "delete(s)" to remove it. Note that images
% must be in PNG, GIF or JPEG format. Use the addText method to add
% text to your splashscreen
%
% Examples:
% s = SplashScreen( 'Splashscreen', 'example_splash.png', ...
% 'ProgressBar', 'on', ...
% 'ProgressPosition', 5, ...
% 'ProgressRatio', 0.4 )
% s.addText( 30, 50, 'My Cool App', 'FontSize', 30, 'Color', [0 0 0.6] )
% s.addText( 30, 80, 'v1.0', 'FontSize', 20, 'Color', [0.2 0.2 0.5] )
% s.addText( 300, 270, 'Loading...', 'FontSize', 20, 'Color', 'white' )
% delete( s )
%
% See also: SplashScreen/addText
% Copyright 2008-2011 The MathWorks, Inc.
% Revision: 1.1
%% Public properties
properties
Visible = 'on' % Is the splash-screen visible on-screen [on|off]
Border = 'on' % Is the edge pixel darkened to form a border [on|off]
ProgressBar = 'off' % Is the progress bar visible [on|off]
ProgressPosition = 10% Height (in pixels) above the bottom of the window for the progress bar
ProgressRatio = 0 % The ratio shown on the progress bar (in range 0 to 1)
Tag = '' % User tag for this object
end % Public properties
%% Read-only properties
properties ( GetAccess = public, SetAccess = private )
Width = 0 % Width of the window
Height = 0 % Height of the window
end % Read-only properties
%% Private properties
properties ( Access = private )
Icon = []
BufferedImage = []
OriginalImage = []
Label = []
Frame = []
end % Read-only properties
%% Public methods
methods
function obj = SplashScreen( title, imagename, varargin )
% Construct a new splash-screen object
if nargin<2
error('SplashScreen:BadSyntax', 'Syntax error. You must supply both a window title and imagename.' );
end
% First try to load the image as an icon
fullname = iMakeFullName( imagename );
if exist(fullname,'file')~=2
% Try on the path
fullname = which( imagename );
if isempty( fullname )
error('SplashScreen:BadFile', 'Image ''%s'' could not be found.', imagename );
end
end
% Create the interface
obj.createInterfaceComponents( title, fullname );
obj.updateAll();
% Set any user-specified properties
for ii=1:2:nargin-2
param = varargin{ii};
value = varargin{ii+1};
obj.(param) = value;
end
% If the user hasn't overridden the default, finish by putting
% it onscreen
if strcmpi(obj.Visible,'on')
obj.Frame.setVisible(true);
end
end % SplashScreen
function addText( obj, x, y, text, varargin )
%addText Add some text to the background image
%
% S.addText(X,Y,STR,...) adds some text to the splashscreen S
% showing the string STR. The text is located at pixel
% coordinates [X,Y]. Additional options can be set using
% parameter value pairs and include:
% 'Color' the font color (e.g. 'r' or [r g b])
% 'FontSize' the text size (in points)
% 'FontName' the name of the font to sue (default 'Arial')
% 'FontAngle' 'normal' or 'italic'
% 'FontWeight' 'normal' or 'bold'
% 'Shadow' 'on' or 'off' (default 'on')
%
% Examples:
% s = SplashScreen( 'Splashscreen', 'example_splash.png' );
% s.addText( 30, 50, 'My Cool App', 'FontSize', 30, 'Color', [0 0 0.6] )
%
% See also: SplashScreen
% We write into the original image so that the text is
% permanent
gfx = obj.OriginalImage.getGraphics();
% Set the font size etc
[font,color,shadow] = parseFontArguments( varargin{:} );
gfx.setFont( font );
% Switch on anti-aliasing
gfx.setRenderingHint( java.awt.RenderingHints.KEY_ANTIALIASING, java.awt.RenderingHints.VALUE_ANTIALIAS_ON );
% Draw the text semi-transparent as a shadow
if shadow
gfx.setPaint( java.awt.Color.black );
ac = java.awt.AlphaComposite.getInstance( java.awt.AlphaComposite.SRC_OVER, 0.3 );
gfx.setComposite( ac );
gfx.drawString( text, x+1, y+2 );
ac = java.awt.AlphaComposite.getInstance( java.awt.AlphaComposite.SRC_OVER, 0.5 );
gfx.setComposite( ac );
gfx.drawString( text, x, y+1 );
ac = java.awt.AlphaComposite.getInstance( java.awt.AlphaComposite.SRC_OVER, 0.7 );
gfx.setComposite( ac );
gfx.drawString( text, x+1, y+1 );
end
% Now the text itself
gfx.setPaint( color );
ac = java.awt.AlphaComposite.getInstance( java.awt.AlphaComposite.SRC_OVER, 1.0 );
gfx.setComposite( ac );
gfx.drawString( text, x, y );
% Now update everything else
obj.updateAll();
end % addText
function delete(obj)
%delete destroy this object and close all graphical components
if ~isempty(obj.Frame)
dispose(obj.Frame);
end
end % delete
end % Public methods
%% Data-access methods
methods
function val = get.Width(obj)
if isempty(obj.Icon)
val = 0;
else
val = obj.Icon.getIconWidth();
end
end % get.Width
function val = get.Height(obj)
if isempty(obj.Icon)
val = 0;
else
val = obj.Icon.getIconHeight();
end
end % get.Height
function set.Visible(obj,val)
if ~ischar( val ) || ~any( strcmpi( {'on','off'}, val ) )
error( 'SplashScreen:BadValue', 'Property ''ProgressBar'' must be ''on'' or ''off''' );
end
obj.Visible = lower( val );
obj.Frame.setVisible( strcmpi(val,'ON') ); %#ok<MCSUP>
end % set.Visible
function set.Border(obj,val)
if ~ischar( val ) || ~any( strcmpi( {'on','off'}, val ) )
error( 'SplashScreen:BadValue', 'Property ''Border'' must be ''on'' or ''off''' );
end
obj.Border = val;
obj.updateAll();
end % set.Border
function set.ProgressBar(obj,val)
if ~ischar( val ) || ~any( strcmpi( {'on','off'}, val ) )
error( 'SplashScreen:BadValue', 'Property ''ProgressBar'' must be ''on'' or ''off''' );
end
obj.ProgressBar = val;
obj.updateProgressBar();
end % set.ProgressBar
function set.ProgressRatio(obj,val)
if ~isnumeric( val ) || ~isscalar( val ) || val<0 || val > 1
error( 'SplashScreen:BadValue', 'Property ''ProgressRatio'' must be a scalar between 0 and 1' );
end
obj.ProgressRatio = val;
obj.updateProgressBar();
end % set.ProgressRatio
function set.ProgressPosition(obj,val)
if ~isnumeric( val ) || ~isscalar( val ) || val<1 || val > obj.Height %#ok<MCSUP>
error( 'SplashScreen:BadValue', 'Property ''ProgressPosition'' must be a vertical position inside the window' );
end
obj.ProgressPosition = val;
obj.updateAll();
end % set.ProgressPosition
function set.Tag(obj,val)
if ~ischar( val )
error( 'SplashScreen:BadValue', 'Property ''Tag'' must be a character array' );
end
obj.Tag = val;
end % set.Tag
end % Data-access methods
%% Private methods
methods ( Access = private )
function createInterfaceComponents( obj, title, imageFile )
import javax.swing.*;
% Load the image
jImFile = java.io.File( imageFile );
try
obj.OriginalImage = javax.imageio.ImageIO.read( jImFile );
catch err
error('SplashScreen:BadFile', 'Image ''%s'' could not be loaded.', imageFile );
end
% Read it again into the copy we'll draw on
obj.BufferedImage = javax.imageio.ImageIO.read( jImFile );
% Create the icon
obj.Icon = ImageIcon( obj.BufferedImage );
% Create the frame and fill it with the image
obj.Frame = JFrame( title );
obj.Frame.setUndecorated( true );
obj.Label = JLabel( obj.Icon );
p = obj.Frame.getContentPane();
p.add( obj.Label );
% Resize and reposition the window
obj.Frame.setSize( obj.Icon.getIconWidth(), obj.Icon.getIconHeight() );
pos = get(0,'MonitorPositions');
x0 = pos(1,1) + (pos(1,3)-obj.Icon.getIconWidth())/2;
y0 = pos(1,2) + (pos(1,4)-obj.Icon.getIconHeight())/2;
obj.Frame.setLocation( x0, y0 );
end % createInterfaceComponents
function updateAll( obj )
% Update the entire image
% Copy in the original
w = obj.Frame.getWidth();
h = obj.Frame.getHeight();
gfx = obj.BufferedImage.getGraphics();
gfx.drawImage( obj.OriginalImage, 0, 0, w, h, 0, 0, w, h, [] );
% Maybe draw a border
if strcmpi( obj.Border, 'on' )
% Switch on anti-aliasing
gfx.setRenderingHint( java.awt.RenderingHints.KEY_ANTIALIASING, java.awt.RenderingHints.VALUE_ANTIALIAS_ON );
% Draw a semi-transparent rectangle for the background
ac = java.awt.AlphaComposite.getInstance( java.awt.AlphaComposite.SRC_OVER, 0.8 );
gfx.setComposite( ac );
gfx.setPaint( java.awt.Color.black );
gfx.drawRect( 0, 0, w-1, h-1 );
end
obj.updateProgressBar();
end % updateAll
function updateProgressBar( obj )
% Update the progressbar and other bits
% First paint over the progressbar area with the original image
gfx = obj.BufferedImage.getGraphics();
border = 10;
size = 6;
w = obj.Frame.getWidth();
h = obj.Frame.getHeight();
py = obj.ProgressPosition;
x1 = 1;
y1 = h-py-size-1;
x2 = w-2;
y2 = h-py;
gfx.drawImage( obj.OriginalImage, x1, y1, x2, y2, x1, y1, x2, y2, [] );
if strcmpi( obj.ProgressBar, 'on' )
% Draw the progress bar over the image
% Switch on anti-aliasing
gfx.setRenderingHint( java.awt.RenderingHints.KEY_ANTIALIASING, java.awt.RenderingHints.VALUE_ANTIALIAS_ON );
% Draw a semi-transparent rectangle for the background
ac = java.awt.AlphaComposite.getInstance( java.awt.AlphaComposite.SRC_OVER, 0.25 );
gfx.setComposite( ac );
gfx.setPaint( java.awt.Color.black );
gfx.fillRoundRect( border, h-py-size, w-2*border, size, size, size );
% Now draw the foreground bit
progWidth = (w-2*border-2) * obj.ProgressRatio;
ac = java.awt.AlphaComposite.getInstance( java.awt.AlphaComposite.SRC_OVER, 0.5 );
gfx.setComposite( ac );
gfx.setPaint( java.awt.Color.white );
gfx.fillRoundRect( border+1, h-py-size+1, progWidth, size-2, size, size );
end
% Update the on-screen image
obj.Frame.repaint();
end % updateProgressBar
end % Private methods
end % classdef
%-------------------------------------------------------------------------%
function filename = iMakeFullName( filename )
% Absolute paths start with one of:
% 'X:' (Windows)
% '\\' (UNC)
% '/' (Unix/Linux/Mac)
if ~strcmp(filename(2),':') ...
&& ~strcmp(filename(1:2),'\\') ...
&& ~strcmpi(filename(1),'/')
% Relative path, so add current working directory
filename = fullfile( pwd(), filename );
end
end % iMakeFullName
function [font,color,shadow] = parseFontArguments( varargin )
% Create a java font object based on some optional inputs
fontName = 'Arial';
fontSize = 12;
fontWeight = java.awt.Font.PLAIN;
fontAngle = 0;
color = java.awt.Color.red;
shadow = true;
if nargin
params = varargin(1:2:end);
values = varargin(2:2:end);
if numel( params ) ~= numel( values )
error( 'UIExtras:SplashScreen:BadSyntax', 'Optional arguments must be supplied as parameter-value pairs.' );
end
for ii=1:numel( params )
switch upper( params{ii} )
case 'FONTSIZE'
fontSize = values{ii};
case 'FONTNAME'
fontName = values{ii};
case 'FONTWEIGHT'
switch upper( values{ii} )
case 'NORMAL'
fontWeight = java.awt.Font.PLAIN;
case 'BOLD'
fontWeight = java.awt.Font.PLAIN;
otherwise
error( 'UIExtras:SplashScreen:BadParameterValue', 'Unsupported FontWeight: %s.', values{ii} );
end
case 'FONTANGLE'
switch upper( values{ii} )
case 'NORMAL'
fontAngle = 0;
case 'ITALIC'
fontAngle = java.awt.Font.ITALIC;
otherwise
error( 'UIExtras:SplashScreen:BadParameterValue', 'Unsupported FontAngle: %s.', values{ii} );
end
case 'COLOR'
rgb = iInterpretColor( values{ii} );
color = java.awt.Color( rgb(1), rgb(2), rgb(3) );
case 'SHADOW'
if ~ischar( values{ii} ) || ~ismember( upper( values{ii} ), {'ON','OFF'} )
error( 'UIExtras:SplashScreen:BadParameter', 'Option ''Shadow'' must be ''on'' or ''off''.' );
end
shadow = strcmpi( values{ii}, 'on' );
otherwise
error( 'UIExtras:SplashScreen:BadParameter', 'Unsupported optional parameter: %s.', params{ii} );
end
end
end
font = java.awt.Font( fontName, fontWeight+fontAngle, fontSize );
end % parseFontArguments
function col = iInterpretColor(str)
%interpretColor Interpret a color as an RGB triple
%
% rgb = uiextras.interpretColor(col) interprets the input color COL and
% returns the equivalent RGB triple. COL can be one of:
% * RGB triple of floating point numbers in the range 0 to 1
% * RGB triple of UINT8 numbers in the range 0 to 255
% * single character: 'r','g','b','m','y','c','k','w'
% * string: one of 'red','green','blue','magenta','yellow','cyan','black'
% 'white'
% * HTML-style string (e.g. '#FF23E0')
%
% Examples:
% >> uiextras.interpretColor( 'r' )
% ans =
% 1 0 0
% >> uiextras.interpretColor( 'cyan' )
% ans =
% 0 1 1
% >> uiextras.interpretColor( '#FF23E0' )
% ans =
% 1.0000 0.1373 0.8784
%
% See also: ColorSpec
% Copyright 2005-2010 The MathWorks Ltd.
% $Revision: 327 $
% $Date: 2010-08-26 09:53:11 +0100 (Thu, 26 Aug 2010) $
if ischar( str )
str = strtrim(str);
str = dequote(str);
if str(1)=='#'
% HTML-style string
if numel(str)==4
col = [hex2dec( str(2) ), hex2dec( str(3) ), hex2dec( str(4) )]/15;
elseif numel(str)==7
col = [hex2dec( str(2:3) ), hex2dec( str(4:5) ), hex2dec( str(6:7) )]/255;
else
error( 'UIExtras:interpretColor:BadColor', 'Invalid HTML color %s', str );
end
elseif all( ismember( str, '1234567890.,; []' ) )
% Try the '[0 0 1]' thing first
col = str2num( str ); %#ok<ST2NM>
if numel(col) == 3
% Conversion worked, so just check for silly values
col(col<0) = 0;
col(col>1) = 1;
end
else
% that didn't work, so try the name
switch upper(str)
case {'R','RED'}
col = [1 0 0];
case {'G','GREEN'}
col = [0 1 0];
case {'B','BLUE'}
col = [0 0 1];
case {'C','CYAN'}
col = [0 1 1];
case {'Y','YELLOW'}
col = [1 1 0];
case {'M','MAGENTA'}
col = [1 0 1];
case {'K','BLACK'}
col = [0 0 0];
case {'W','WHITE'}
col = [1 1 1];
case {'N','NONE'}
col = [nan nan nan];
otherwise
% Failed
error( 'UIExtras:interpretColor:BadColor', 'Could not interpret color %s', num2str( str ) );
end
end
elseif isfloat(str) || isdouble(str)
% Floating point, so should be a triple in range 0 to 1
if numel(str)==3
col = double( str );
col(col<0) = 0;
col(col>1) = 1;
else
error( 'UIExtras:interpretColor:BadColor', 'Could not interpret color %s', num2str( str ) );
end
elseif isa(str,'uint8')
% UINT8, so range is implicit
if numel(str)==3
col = double( str )/255;
col(col<0) = 0;
col(col>1) = 1;
else
error( 'UIExtras:interpretColor:BadColor', 'Could not interpret color %s', num2str( str ) );
end
else
error( 'UIExtras:interpretColor:BadColor', 'Could not interpret color %s', num2str( str ) );
end
end % iInterpretColor
function str = dequote(str)
str(str=='''') = [];
str(str=='"') = [];
str(str=='[') = [];
str(str==']') = [];
end % dequote