File: PresentationStateGraphicsHandler.java

package info (click to toggle)
dicomscope 3.6.0-28
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,256 kB
  • sloc: java: 22,911; cpp: 5,957; sh: 270; makefile: 45
file content (1607 lines) | stat: -rw-r--r-- 56,131 bytes parent folder | download | duplicates (9)
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
/*
 *
 *  Copyright (C) 1999, Institute for MicroTherapy
 *
 *  This software and supporting documentation were developed by
 *
 *    University of Witten/Herdecke
 *    Department of Radiology and MicroTherapy
 *    Institute for MicroTherapy
 *    Medical computer science
 *
 *    Universitaetsstrasse 142
 *    44799 Bochum, Germany
 *
 *    http://www.microtherapy.de/go/cs
 *    mailto:computer.science@microtherapy.de
 *
 *  THIS SOFTWARE IS MADE AVAILABLE,  AS IS,  AND THE INSTITUTE MAKES  NO
 *  WARRANTY REGARDING THE SOFTWARE, ITS PERFORMANCE, ITS MERCHANTABILITY
 *  OR FITNESS FOR ANY PARTICULAR USE, FREEDOM FROM ANY COMPUTER DISEASES
 *  OR ITS CONFORMITY TO ANY SPECIFICATION. THE ENTIRE RISK AS TO QUALITY
 *  AND PERFORMANCE OF THE SOFTWARE IS WITH THE USER.
 *
 *  Author :      $Author: kleber $
 *  Last update : $Date: 2003/09/08 10:17:26 $
 *  Revision :    $Revision: 1.2 $
 *  State:        $State: Exp $
 */
package viewer.presentation;

import viewer.paint.*;
import J2Ci.*;
import main.*;

import java.util.*;
import java.awt.geom.*;
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
/**
 * This class manages the Presentation State.
 *
 * @author Klaus Kleber
 * @since 30.04.1999
 */
public class PresentationStateGraphicsHandler {
    /**
     * Toggle PS on/off. If true the Presentation State is visible, if only the image
     * without the Presentation State is visible.
     *
     * @since 30.04.1999
     */
    public boolean enablePresentationState = true;
    
    /**
     * Contains the data of the Presentation State in a c++ Interface.
     *
     * @since 30.04.1999
     */
    public jDVPresentationState ps;
    
    /**
     * Vector which contains the layers of the presentation state.
     *
     * @since 30.04.1999
     */
    public Vector listPresentationStateGraphicLayer;
    
    
    /**
     * This object manages the overlays.
     *
     * @see OverlayList
     * @since 30.04.1999
     */
    public OverlayList overlayList;
    
    /**
     * This object manages the Shutters.
     *
     * @since 30.04.1999
     * @see ShutterList
     */
    public ShutterList  shutterList;
    
    /**
     * Specified the index of the current active PresentationStateGraphicsLayer.
     * If index = -1 no PresentationStateGraphicsLayer is avalable.
     * The current active PresentationStateGraphicsLayer is the layer on which you can
     * create, manipulate or delete PresentationStateLayerObjects
     *
     * @since 30.04.1999
     */
    int currentLayerIndex = -1;
    
    /**
     * Specified the index of the current active PresentationStateLayerObject in the
     * current active PresentationStateGraphicsLayer (specified form the  currentLayerIndex).
     * If index = -1 no PresentationStateLayerObject is active.
     * The current active PresentationStateLayerObject is the one which you can
     * create, manipulate or delete.
     *
     * @since 30.04.1999
     */
    int currentLayerObjectIndex= -1;
    
    /**
     * A <em>jDVInterface</em> represents the relating C++-Class DVInterface
     * in Java.
     *
     * @since 30.04.1999
     * @see J2Ci.jDVInterface
     */
    public jDVInterface dvi;
    
    /**
     * Contains the currently available size of the imageCanvas.
     *
     * @since 30.04.1999
     * @see GUI.ImageCanvas
     */
    Dimension screenSize;
    
    public double trueSizeX =1d;
    public  double trueSizeY =1d;
    double monitorScalingX = 1d;
    double monitorScalingY =1d;
    
    public  int selectedImageNumber = 1;
    public int selectedLutIndex = -1;
    public int psSelectedLUTIndex = -1;
    /**
     * Contains the vertical scaling factor of the image.
     *
     * @since 30.04.1999
     */
    private double scalingValueY = 1d;
    
    /**
     * Contains the horizontal scaling factor of the image.
     *
     * @since 30.04.1999
     */
    private double scalingValueX = 1d;
    
    /**
     * Contains the zoom factor of the displaying image.
     *
     * @since 30.04.1999
     */
    private double zoomValue=-1;
    
    /**
     * Contains true if the PresentationStateGraphicsHandler have a jDVPresentationState.
     *
     * @since 30.04.1999
     */
    public boolean havePresentationState = false;
    
    /**
     * Constructs the paintStructure from the specified jDVInterface.
     *
     * @param dvi Contains the current jDCInterface
     * @since 30.04.1999
     * @see J2Ci.jDVInterface
     */
    public PresentationStateGraphicsHandler(jDVInterface dvi) {
        this.dvi = dvi;
    }
    
    /**
     * Gets the current active PresentationStateLayerObject. This  PresentationStateLayerObject
     * is specified form the currentLayerObjectIndex and the currentLayerIndex.
     *
     * @return The current active PresentationStateLayerObject.
     * @since 30.04.1999
     */
    public PresentationStateLayerObject getCurrentLayerObject() {
        return ((PresentationStateLayerObject) ((PresentationStateGraphicLayer) listPresentationStateGraphicLayer.elementAt(currentLayerIndex)).layerObjectAt(currentLayerObjectIndex));
    }
    
    /**
     * Sets the currentLayerObjectIndex.
     *
     * @param newIndex New currentLayerObjectIndex.
     * @since 30.04.1999
     */
    public void setCurrentLayerObjectIndex(int newIndex) {
        this.currentLayerObjectIndex = newIndex;
    }
    
    /**
     * Gets the currentLayerObjectIndex.
     *
     * @return The currentLayerObjectIndex.
     * @since 30.04.1999
     */
    public int getCurrentLayerObjectIndex() {
        return currentLayerObjectIndex;
    }
    
    /**
     * Gets the current active PresentationStateGraphicLayer. This  PresentationStateGraphicLayer
     * is specified form the.currentLayerIndex.
     *
     * @return The current active PresentationStateGraphicLayer.
     * @since 30.04.1999
     */
    public PresentationStateGraphicLayer getCurrentLayer() {
        return getLayerAtIndex(currentLayerIndex);
    }
    
    /**
     * Sets the currentLayerIndex.
     *
     * @param newIndex New currentLayerIndex.
     * @since 30.04.1999
     */
    public void setCurrentLayerIndex( int newIndex) {
        currentLayerIndex = newIndex;
        currentLayerObjectIndex= -1;
        
    }
    
    /**
     * Gets the currentLayerObjectIndex.
     *
     * @return The currentLayerObjectIndex.
     * @since 30.04.1999
     */
    public int getCurrentLayerIndex() {
        return currentLayerIndex;
    }
    
    /**
     * Gets the horizontal scaling factor of the image.
     *
     * @return Horizontal scaling factor of the image.
     * @since 30.04.1999
     */
    public double getScalingX() {
        return scalingValueX*monitorScalingX;
        
    }
    
    /**
     * Gets the vertical scaling factor of the image.
     *
     * @return Vertical scaling factor of the image.
     * @since 30.04.1999
     */
    public  double getScalingY() {
        return scalingValueY*monitorScalingY;
    }
    /**
     * Gets the horizontal scaling factor of the image.
     *
     * @return Horizontal scaling factor of the image.
     * @since 30.04.1999
     */
    public double getScX() {
        return scalingValueX;
        
    }
    
    /**
     * Gets the vertical scaling factor of the image.
     *
     * @return Vertical scaling factor of the image.
     * @since 30.04.1999
     */
    public  double getScY() {
        return scalingValueY;
    }
    
    public double getMonitorScalingX() {
        return monitorScalingX;
    }
    
    public double getMonitorScalingY() {
        return monitorScalingY;
    }
    /**
     * Gets the horizontal scaling factor after the rotation. If rotation == 90-deg or 270 deg
     * scalingValueX and scalingValueY will be exchanged.
     *
     * @return Horizontal scaling factor after the rotation.
     * @since 30.04.1999
     */
    public double getCurrentScalingX() {
        if ((getRotation() == 0) ||(getRotation() ==2))return scalingValueX*monitorScalingX;
        else return scalingValueY*monitorScalingX;
    }
    
    /**
     * Gets the vertical scaling factor after  rotation. If rotation == 90-deg or 270 deg
     * scalingValueX and scalingValueY will be exchanged.
     *
     * @return Vertical scaling factor after  rotation.
     * @since 30.04.1999
     */
    public double getCurrentScalingY() {
        if ((getRotation() == 0) ||(getRotation() ==2))return scalingValueY*monitorScalingY;
        else return scalingValueX*monitorScalingY;
    }
    
    
    /**
     * Gets the height of the image.
     *
     * @return Heigth of the image.
     * @since 30.04.1999
     * @see J2Ci.jDVPresentationState
     */
    public int getImageHeight() {
        jIntByRef height = new jIntByRef();
        ps.getImageHeight(height);
        return height.value;
        
    }
    /**
     * Gets the height of the image.
     *
     * @return Heigth of the image.
     * @since 30.04.1999
     * @see J2Ci.jDVPresentationState
     */
    public double getDisplayedAreaPresentationPixelMagnificationRatio() {
        jDoubleByRef ratio = new jDoubleByRef();
        ps.getDisplayedAreaPresentationPixelMagnificationRatio(ratio);
        return ratio.value;
        
    }
    /**
     * Gets the height of the image.
     *
     * @return Heigth of the image.
     * @since 30.04.1999
     * @see J2Ci.jDVPresentationState
     */
    public double getDisplayedAreaPresentationPixelAspectRatio() {
        double i = ps.getDisplayedAreaPresentationPixelAspectRatio();
        if (i<=0) i = 1;
        return i;
    }
    /**
     * Gets the height of the image.
     *
     * @return Heigth of the image.
     * @since 30.04.1999
     * @see J2Ci.jDVPresentationState
     */
    public double getDisplayedAreaPresentationPixelSpacingX() {
        jDoubleByRef x = new jDoubleByRef();
        jDoubleByRef y = new jDoubleByRef();
        ps.getDisplayedAreaPresentationPixelSpacing(x, y);
        double i = x.value;
        if (i<=0) i = 1;
        return i;
        
        
    }
    public boolean haveDisplayedAreaPresentationPixelSpacing() {
        jDoubleByRef x = new jDoubleByRef();
        jDoubleByRef y = new jDoubleByRef();
        int status = ps.getDisplayedAreaPresentationPixelSpacing(x, y);
        if (status == jE_Condition.EC_Normal) return true;
        else return false;
        
    }
    /**
     * Gets the height of the image.
     *
     * @return Heigth of the image.
     * @since 30.04.1999
     * @see J2Ci.jDVPresentationState
     */
    public double getDisplayedAreaPresentationPixelSpacingY() {
        jDoubleByRef x = new jDoubleByRef();
        jDoubleByRef y = new jDoubleByRef();
        ps.getDisplayedAreaPresentationPixelSpacing(x, y);
        double i = y.value;
        if (i<=0) i = 1;
        return i;
        
    }
    
    /**
     * Gets the width of the image.
     *
     * @return Width of the image.
     * @since 30.04.1999
     * @see J2Ci.jDVPresentationState
     */
    public int getImageWidth() {
        jIntByRef width = new jIntByRef();
        ps.getImageWidth(width);
        return width.value;
    }
    
    /**
     * Gets the height of the image after  rotation.If rotation == 90-deg or 270 deg
     * height and width of the image will be exchanged.
     *
     * @return Heigth of the image after  rotation..
     * @since 30.04.1999
     * @see J2Ci.jDVPresentationState
     */
    public int getCurrentImageHeight() {
        if ((getRotation() == 0) ||(getRotation() ==2))return  getImageHeight();
        else return getImageWidth();
        
    }
    
    /**
     * Gets the height of the image after  rotation.If rotation == 90-deg or 270 deg
     * height and width of the image will be exchanged.
     *
     * @return Heigth of the image after  rotation..
     * @since 30.04.1999
     * @see J2Ci.jDVPresentationState
     */
    public int getCurrentImageWidth() {
        if ((getRotation() == 0) ||(getRotation() ==2))return  getImageWidth();
        else return  getImageHeight();
    }
    /**
     * Calculates the horizontal and vertical scaling values. There are two parameters which can
     * influcene these values. First, the Presentation Pixel Spacing value or
     * the Presentation Pixel Aspect Ratio value contains implicit or explicit sacling values.
     * Secoundly the pixel size of the monitor will influence the scale.
     *
     * @return Heigth of the image after  rotation..
     * @since 30.04.1999
     * @see J2Ci.jDVPresentationState
     */
    public void buildScaling() {
        jDoubleByRef xValue = new jDoubleByRef();
        jDoubleByRef yValue = new jDoubleByRef();
        monitorScalingX = 1d;
        monitorScalingY = 1d;
        scalingValueY = 1d;
        scalingValueX = 1d;
        //Calculates the scale values resulting of the monitor characteristics.
        if (trueSizeX > trueSizeY)monitorScalingX = trueSizeY/trueSizeX;
        else monitorScalingY = trueSizeX/trueSizeY;
        
        int status = ps.getDisplayedAreaPresentationPixelSpacing(xValue, yValue);
        if (xValue.value <= 0) xValue.value = 1;
        if (yValue.value <= 0) yValue.value = 1;
        
        if (status ==jE_Condition.EC_Normal) {
            if (ps.getDisplayedAreaPresentationSizeMode() == jDVPSPresentationSizeMode.DVPSD_trueSize) {
                monitorScalingX= xValue.value/trueSizeX;
                monitorScalingY = yValue.value/trueSizeY;
            }
            else {
                if (xValue.value > yValue.value) {
                    scalingValueX = yValue.value/xValue.value;
                    scalingValueY = 1d;
                }
                else {
                    scalingValueY = xValue.value/yValue.value;
                    scalingValueX = 1d;
                    
                }
            }
        }
        else {
            double aspectRatio = ps.getDisplayedAreaPresentationPixelAspectRatio();
            if (aspectRatio >= 1d) {
                scalingValueX = aspectRatio;
                scalingValueY =1d;
            }
            else {
                scalingValueY = (1d/aspectRatio);
                scalingValueX = 1d;
            }
        }
        
        
        //Gets the scalingValues
        
    }
    
    
    /**
     * Builds the internal structure of the PresentationStateGraphicsHandler if a new jDVPresentationState will be received.
     * The overlayList, the shutterList and the listPresentationStateGraphicLayer will be create from the
     * jDVPresentationState obejct.
     *
     * @since 30.04.1999
     * @see J2Ci.jDVPresentationState
     */
    public void buildPresentationState() {
        selectedLutIndex = -1;
        
        
        enablePresentationState = true;
        this.ps = dvi.getCurrentPState();
        buildScaling();
        havePresentationState = true;
        listPresentationStateGraphicLayer = new Vector();
        
        overlayList = new OverlayList(ps);
        
        //builds the ShutterList
        shutterList = new ShutterList(ps);
        
        //builds the layer list.
        ps.sortGraphicLayers();
        
        DisplayArea da = getDisplayArea();
        int numLayer = ps.getNumberOfGraphicLayers()-1;
        for (int i = 0; i <= numLayer; i++) {
            
            listPresentationStateGraphicLayer.add(new PresentationStateGraphicLayer(i,ps,da,overlayList));
        }
        if (listPresentationStateGraphicLayer.size() !=0) currentLayerIndex = ps.getNumberOfGraphicLayers()-1;
        else currentLayerIndex = -1;
        
        //Init size in layer. Calculates dispaly relative annotation.
        for (int i = 0; i < listPresentationStateGraphicLayer.size(); i++) {
            
            ((PresentationStateGraphicLayer)listPresentationStateGraphicLayer.elementAt(i)).setScreenSize(screenSize);
        }
        
    }
    
    /**
     * Gets the pixel aspect ratio of the attached image.
     * Pixel aspect ratio is defined here as the width of a pixel divided
     * by the height of a pixel (x/y).
     *
     * @since 30.04.1999
     * @see J2Ci.jDVPresentationState
     */
    public double getPixelAspectRatio() {
        /*//Gets the scalingValues
        jDoubleByRef aspectRatio = new jDoubleByRef();
        ps.getImageAspectRatio(aspectRatio);
        return aspectRatio.value;
         */
        return 1;
    }
    
    /**
     * Resets the currentLayerObjectIndex.
     *
     * @since 30.04.1999
     */
    
    public void reset() {
        currentLayerObjectIndex   = -1;
        selectedLutIndex = -1;
    }
    
    
    /**
     * Draws the all PresentationStateGraphicLayer in listPresentationStateGraphicLayer and all
     * Overlays defined in OverlayList after the sprecified transformation aff in the specified BufferedImage.
     * All PresntationStateLayerObjects in each PresentationStateGraphicLayer will be drawn.
     *
     * @param bufferedImage Specifies the BufferedImage in which you can draw.
     * @param aff Specifies the transformation which will be used for PresentationStateAnnotationObjects.
     * @param aff Specifies the transformation which will be used for PresentationStateOverlayObjects.
     * @since 30.04.1999
     * @see Viewer.Main.ScreenImageHandler#buildImageBuffer
     */
    public void drawPaintStructure( BufferedImage bufferedImage,AffineTransform aff,AffineTransform overlayAff, boolean trueSize, int bits,boolean scale) {
        if (bufferedImage== null) return;
        //Creating the graphics2D
        Graphics2D g2 = (Graphics2D)bufferedImage.getGraphics();
        if (g2!= null) {
            try {
                for (int i = 0; i < listPresentationStateGraphicLayer.size(); i++) {
                    ((PresentationStateGraphicLayer) listPresentationStateGraphicLayer.elementAt(i)).draw(g2, aff, overlayAff,bits, scale);
                }
            }
            finally {
                g2.dispose();
            }
        }
    }
    
    /**
     * Searched for the PresentationStateLayerObject containing the specified point.
     * The result is a new active PresentationStateLayerObject (specified with a new
     * currentLayerObjectIndex) and a new active PresentationStateGraphicLayer (specified with a new
     * currentLayerIndex.
     *
     * @param point Contains the searched point.
     * @since 30.04.1999
     * @see Viewer.Main.ScreenImageHandler#buildImageBuffer
     */
    
    public void containsObject(Point2D.Float point) {
        
        
        for (int i = listPresentationStateGraphicLayer.size()-1; i >= 0; i--) {
            currentLayerObjectIndex = ((PresentationStateGraphicLayer) listPresentationStateGraphicLayer.elementAt(i)).containsLayerObject(point);
            if (currentLayerObjectIndex != -1) {
                currentLayerIndex = i;
                return;
            }
        }
    }
    
    
    /**
     * Returns a String representing this object.
     *
     * @return A String representing this object
     * @since 30.04.1999
     */
    public String getInfo() {
        String returnString;
        returnString = new String( "PresentationStateGraphicHandler" + "\n" );
        for(int i = 0; i <listPresentationStateGraphicLayer.size(); i++) {
            returnString= returnString.concat(new String("    Index: " +i +"\n"));
            returnString=returnString.concat(((PresentationStateGraphicLayer) listPresentationStateGraphicLayer.elementAt(i)).getInfo());
        }
        return returnString;
    }
    
    /**
     * Sets the new available size for displaying the image. If no zoom value is set the zoom value will be calculate.
     * The size of the available display is important for display realtive annotations
     * (handled in PresentationStateGraphicObject and PresentationStateTextObject). Changing the size
     * resluts in changing all display relative annotations.
     *
     * @param screenSize New available size for displaying the image.
     * @since 30.04.1999
     */
    public void setScreenSize(Dimension screenSize) {
        
        this.screenSize = screenSize;
        calculateFirstZoomValue();
        
        //Init size in layer. Calculates dispaly relative annotation.
        for (int i = 0; i < listPresentationStateGraphicLayer.size(); i++) {
            ((PresentationStateGraphicLayer)listPresentationStateGraphicLayer.elementAt(i)).setScreenSize(screenSize);
        }
        
    }
    /**
     * Calculates the zoom value if the image should displayed first time. The zoom value
     * must be caluclated because the presnetations state do not contain this. There are only the
     * top left hand corner and the bottom right hand corner of the displaying part of the image available.
     *
     * @since 30.04.1999
     */
    public void calculateFirstZoomValue() {
        DisplayArea da = getDisplayArea();
        if (ps.getDisplayedAreaPresentationSizeMode() ==jDVPSPresentationSizeMode.DVPSD_scaleToFit) {
            
            zoomValue = calculateZoomValue(da);
        }
        else if (ps.getDisplayedAreaPresentationSizeMode() ==jDVPSPresentationSizeMode.DVPSD_magnify) {
            jDoubleByRef zoom = new jDoubleByRef();
            ps.getDisplayedAreaPresentationPixelMagnificationRatio(zoom);
            zoomValue = zoom.value;
        }
        else if (ps.getDisplayedAreaPresentationSizeMode() ==jDVPSPresentationSizeMode.DVPSD_trueSize) {
            
            zoomValue =1d;
        }
        //zoomValue = (Math.round(zoomValue*100d)/100d);
        
    }
    public void convertDisplayedAreas(DisplayArea imageArea, DisplayArea newArea) {
        float transX = ((float)(imageArea.getBrhcX()-imageArea.getTlhcX()))/((float)(newArea.getBrhcX()-newArea.getTlhcX()));
        float transY = ((float)(imageArea.getBrhcY()-imageArea.getTlhcY()))/((float)(newArea.getBrhcY()-newArea.getTlhcY()));
        
        // The display reletive annotationa must be applyed afte the spatial transformations
        
        if ((getRotation() == 1) || (getRotation() ==3)) {
            float help = transX;
            transX = transY;
            transY = help;
        }
        
        //needed for the future
        float offsetX = 0;
        float offsetY = 0;
        
        for (int i = 0; i < listPresentationStateGraphicLayer.size(); i++) {
            ((PresentationStateGraphicLayer) listPresentationStateGraphicLayer.elementAt(i)).convertDisplayedAreas(offsetX, offsetY, transX, transY);
        }
        
    }
    
    public double calculateZoomValue(DisplayArea da) {
        double zoom;
        if ((getRotation() == 0) ||(getRotation() ==2)) {
            zoom = Math.min(  (double) screenSize.width/((double)(da.getBrhcX()-da.getTlhcX()+1)*getCurrentScalingX()),
            (double)screenSize.height/((double)(da.getBrhcY()-da.getTlhcY()+1)*getCurrentScalingY()));
        }
        else {
            zoom = Math.min(  (double) screenSize.width/((double)(da.getBrhcY()-da.getTlhcY()+1)*getCurrentScalingX()),
            (double)screenSize.height/((double)(da.getBrhcX()-da.getTlhcX()+1)*getCurrentScalingY()));
        }
        return zoom;
        
    }
    /**
     * The Vector of  PresentationStateGraphicLayers.
     *
     * @return The Vector of  PresentationStateGraphicLayers.
     * @since 30.04.1999
     */
    public Vector getListPresentationStateGraphicLayer() {
        return listPresentationStateGraphicLayer;
    }
    
    /**
     * Returns the PresentationStateGraphicLayer with the specified index
     *
     */
    public PresentationStateGraphicLayer getPresentationStateGraphicLayer(int index) {
        return (PresentationStateGraphicLayer)listPresentationStateGraphicLayer.elementAt(index);
    }
    
    
    /**
     * Gets the creators name of the presentation state.
     * The value is stored in the c++ interface
     * and is available in  the jDVPresententationState object ps.
     *
     * @return The creators name of the presentation state.
     * @see J2Ci.jDVPresentationState
     * @since 30.04.1999
     */
    public String getPresentationCreatorsName() {
        return ps.getPresentationCreatorsName();
    }
    
    /**
     * Gets the label of the presentation state.
     * The value is stored in the c++ interface
     * and is available in  the jDVPresententationState object ps.
     *
     * @return The label of the presentation state.
     * @see J2Ci.jDVPresentationState
     * @since 30.04.1999
     */
    public String getPresentationLabel() {
        return ps.getPresentationLabel();
    }
    
    /**
     * Gets the description of the presentation state.
     * The value is stored in the c++ interface
     * and is available in  the jDVPresententationState object ps.
     *
     * @return The description of the presentation state.
     * @see J2Ci.jDVPresentationState
     * @since 30.04.1999
     */
    public String getPresentationDescription() {
        return ps.getPresentationDescription();
    }
    
    
    
    
    /**
     * Gets the the displayed area of the presentation state.
     * The value is stored in the c++ interface
     * and is available in  the jDVPresententationState object ps.
     *
     * @return The top left hand corner of the displayed area of the presentation state.
     * @see J2Ci.jDVPresentationState
     * @since 30.04.1999
     */
    public DisplayArea getDisplayArea() {
        jIntByRef tlhc_xValue = new jIntByRef();
        jIntByRef tlhc_yValue = new jIntByRef();
        jIntByRef brhc_yValue= new jIntByRef();
        jIntByRef brhc_xValue= new jIntByRef();
        
        ps.getImageRelativeDisplayedArea(tlhc_xValue, tlhc_yValue, brhc_xValue, brhc_yValue);
        return new DisplayArea(tlhc_xValue.value, tlhc_yValue.value, brhc_xValue.value, brhc_yValue.value);
        
    }
    /**
    
     */
    public DisplayArea getStandardDisplayedArea() {
        jIntByRef tlhc_xValue = new jIntByRef();
        jIntByRef tlhc_yValue = new jIntByRef();
        jIntByRef brhc_yValue= new jIntByRef();
        jIntByRef brhc_xValue= new jIntByRef();
        
        ps.getStandardDisplayedArea(tlhc_xValue, tlhc_yValue, brhc_xValue, brhc_yValue);
     //  System.err.println("DDDD" +new DisplayArea(tlhc_xValue.value, tlhc_yValue.value, brhc_xValue.value, brhc_yValue.value));
           return new DisplayArea(tlhc_xValue.value, tlhc_yValue.value, brhc_xValue.value, brhc_yValue.value);
        
    }
    
    
    
    
    
    /**
     * Checks if a VOI window is active.
     * The value is stored in the c++ interface
     * and is available in  the jDVPresententationState object ps.
     *
     * @return True if a VOI window is active.
     * @see J2Ci.jDVPresentationState
     * @since 30.04.1999
     */
    public boolean haveActiveVOIWindow() {
        return ps.haveActiveVOIWindow();
    }
    
    
    /**
     * Checks if a VOI LUT is active.
     * The value is stored in the c++ interface
     * and is available in  the jDVPresententationState object ps.
     *
     * @return True if a VOI LUT is active.
     * @see J2Ci.jDVPresentationState
     * @since 30.04.1999
     */
    
    public boolean haveActiveVOILUT() {
        return ps.haveActiveVOILUT();
    }
    
    /**
     * Returns the description of the currently active VOI window.
     * The value is stored in the c++ interface
     * and is available in  the jDVPresententationState object ps.
     *
     * @return The description of the currently active VOI window.
     * @see J2Ci.jDVPresentationState
     * @since 30.04.1999
     */
    public String getCurrentVOIDescription() {
        return ps.getCurrentVOIDescription();
    }
    
    
    /**
     * Returns the center of the currently active VOI window.
     * The value is stored in the c++ interface
     * and is available in  the jDVPresententationState object ps.
     *
     * @return The center of the currently active VOI window.
     * @see J2Ci.jDVPresentationState
     * @since 30.04.1999
     */
    public int getCurrentWindowCenter() {
        jDoubleByRef help = new jDoubleByRef();
        ps.getCurrentWindowCenter(help);
        return (int) help.value;
    }
    
    /**
     * Returns the width of the currently active VOI window.
     * The value is stored in the c++ interface
     * and is available in  the jDVPresententationState object ps.
     *
     * @return The width of the currently active VOI window.
     * @see J2Ci.jDVPresentationState
     * @since 30.04.1999
     */
    public int getCurrentWindowWidth() {
        jDoubleByRef help = new jDoubleByRef();
        ps.getCurrentWindowWidth(help);
        return (int) help.value;
    }
    
    /**
     * Sets rotation status of the presentation state.
     * The value is stored in the c++ interface
     * and is available in  the jDVPresententationState object ps.
     *
     * @param rotation the rotation to be set (from jDVPSRotationType).
     * @see J2Ci.jDVPresentationState
     * @see J2Ci.jDVPSRotationType
     * @since 30.04.1999
     */
    
    public void setRotation(int rot) {
        ps.setRotation(rot);
        
    }
    /**
     * Rotate 90 degree.
     * The value is stored in the c++ interface
     * and is available in  the jDVPresententationState object ps.
     *
     * @param rotation the rotation to be set (from jDVPSRotationType).
     * @see J2Ci.jDVPresentationState
     * @see J2Ci.jDVPSRotationType
     * @since 30.04.1999
     */
    public void setRotation() {
        if (ps.getRotation() <=2) ps.setRotation(ps.getRotation()+1);
        else ps.setRotation(0);
        for (int i = 0; i < listPresentationStateGraphicLayer.size(); i++) {
            ((PresentationStateGraphicLayer) listPresentationStateGraphicLayer.elementAt(i)).setRotation();
        }
        
    }
    
    
    /**
     * Gets the rotation status of the presentation state.
     * The value is stored in the c++ interface
     * and is available in  the jDVPresententationState object ps.
     *
     * @return The rotation of the presentation state.
     * @see J2Ci.jDVPresentationState
     * @see J2Ci.jDVPSRotationType
     * @since 30.04.1999
     */
    
    public int getRotation() {
        return  ps.getRotation();
        
    }
    
    
    
    /**
     * Flips the image of the presentation state.
     * The value is stored in the c++ interface
     * and is available in  the jDVPresententationState object ps.
     *
     * @see J2Ci.jDVPresentationState
     * @see J2Ci.jDVPSRotationType
     * @since 30.04.1999
     */
    public void setFlip() {
        DisplayArea da =getDisplayArea();
        ps.setFlip(!ps.getFlip());
        
        for (int i = 0; i < listPresentationStateGraphicLayer.size(); i++) {
            ((PresentationStateGraphicLayer) listPresentationStateGraphicLayer.elementAt(i)).setFlip(getRotation());
        }
        
    }
    
    
    /**
     * Inserts a PaintObject as a new PresentationStateGraphicObject in
     * the currently active PresentationStateGraphicLayer or as a new shutter
     * in the ShutterList.
     *
     * @param newPaintObject Contains the inserting PaintObject.
     * @param aff Contains the current transfomations.
     * @see ShutterList
     * @see PresentationStateGraphicObject
     */
    public void newPaintObject(PaintObject newPaintObject,AffineTransform aff,int applyTo) {
        //Inserts Lines an polylines
        if ((newPaintObject instanceof Line2DObject)||
        (newPaintObject instanceof PolylineObject)) {
            
            float thePoints[] = new float[newPaintObject.getObjectPoints().size()*2];
            
            //Converts points
            for (int i = 0; i< newPaintObject.getObjectPoints().size(); i++) {
                thePoints[2*i] = newPaintObject.getCopyPointAt(i).x+1f;
                thePoints[2*i+1] = newPaintObject.getCopyPointAt(i).y+1f;
            }
            //Insert shutter / polyline
            if (!newPaintObject.isShutter)getCurrentLayer().add(new PresentationStateGraphicObject(ps.addGraphicObject(currentLayerIndex,applyTo),0,jDVPSGraphicType.DVPST_polyline,thePoints,newPaintObject.getType(),newPaintObject.filled,screenSize,aff));
            else {
                shutterList.addPolygonalShutter(thePoints,newPaintObject.getObjectPoints().size());
            }
        }
        if ((newPaintObject instanceof Point2DObject)) {
            
            float thePoints[] = new float[newPaintObject.getObjectPoints().size()*2];
            
            //Converts points
            for (int i = 0; i< newPaintObject.getObjectPoints().size(); i++) {
                thePoints[2*i] = newPaintObject.getCopyPointAt(i).x+1f;
                thePoints[2*i+1] = newPaintObject.getCopyPointAt(i).y+1f;
            }
            
            getCurrentLayer().add(new PresentationStateGraphicObject(ps.addGraphicObject(currentLayerIndex,applyTo),0,jDVPSGraphicType.DVPST_point,thePoints,newPaintObject.getType(),newPaintObject.filled,screenSize,aff));
        }
        //Rectangle
        else if (newPaintObject instanceof Rectangle2DObject) {
            Point2D.Float point1 = newPaintObject.getPoint(0);
            Point2D.Float point2 = newPaintObject.getPoint(1);
            if (point1.x >point2.x) {
                float change = point1.x;
                point1.x = point2.x;
                point2.x = change;
            }
            if (point1.y >point2.y) {
                float change = point1.y;
                point1.y = point2.y;
                point2.y = change;
            }
            
            //Converts points
            float x = point1.x;
            float y = point1.y;
            float w = point2.x-x;
            float h = point2.y-y;
            float thePoints[] = new float[10];
            thePoints[0] = x+1f;
            thePoints[1] = y+1f;
            
            thePoints[2] = x+w+1f;
            thePoints[3] = y+1f;
            
            thePoints[4] = x+w+1f;
            thePoints[5] = y+h+1f;
            
            thePoints[6] = x+1f;
            thePoints[7] = y+h+1f;
            
            thePoints[8] = x+1f;
            thePoints[9] = y+1f;
            
            //Insert polyline/ shutter
            if (!newPaintObject.isShutter)getCurrentLayer().add(new PresentationStateGraphicObject(ps.addGraphicObject(currentLayerIndex,applyTo),0,jDVPSGraphicType.DVPST_polyline,thePoints,newPaintObject.getType(),newPaintObject.filled,screenSize,aff));
            else {
                
                shutterList.addRectangularShutter((int)(point1.x+1f),(int)(point2.x+1f),(int)(point1.y+1f),(int)(point2.y+1f));
            }
            
        }
        //Ellipse
        else if (newPaintObject instanceof Ellipse2DObject) {
            //System.out.println("###");
            float[] thePoints = new float[8];
            Point2D.Float point1 = newPaintObject.getPoint(0);
            Point2D.Float point2 = newPaintObject.getPoint(1);
            if (point1.x >point2.x) {
                float change = point1.x;
                point1.x = point2.x;
                point2.x = change;
            }
            if (point1.y >point2.y) {
                float change = point1.y;
                point1.y = point2.y;
                point2.y = change;
            }
            
            float x = point1.x;
            float y = point1.y;
            float w = point2.x-x;
            float h = point2.y-y;
            //The first two points must be  the end points oft the major axis.
            int index=0;
            if (w<h) {
               index=4;
            }
            thePoints[index++] = x+w+1f;
           // System.err.println("index : "+(index-1) +", " +thePoints[index-1]);
            thePoints[index++] = y+1f+h/2f;
         // System.err.println("index : "+(index-1) +", " +thePoints[index-1]);
            
            thePoints[index++] = x+1f;
          //  System.err.println("index : "+(index-1) +", " +thePoints[index-1]);
            thePoints[index++] = y+1f+h/2f;
          //   System.err.println("index : "+(index-1) +", " +thePoints[index-1]);
            if (w<h) {
               index=0;
            }
            
            thePoints[index++] = x+w/2+1f;
             //System.err.println("index : "+(index-1) +", " +thePoints[index-1]);
            thePoints[index++] = y+1f;
            //System.err.println("index : "+(index-1) +", " +thePoints[index-1]);
            
            thePoints[index++] = x+w/2+1f;
            // System.err.println("index : "+(index-1) +", " +thePoints[index-1]);
            thePoints[index++] = y+h+1f;
             //System.err.println("index : "+(index-1) +", " +thePoints[index-1]);
             
            
            getCurrentLayer().add(new PresentationStateGraphicObject(ps.addGraphicObject(currentLayerIndex,applyTo),0,jDVPSGraphicType.DVPST_ellipse,thePoints,newPaintObject.getType(),newPaintObject.filled,screenSize,aff));
        }
        //Circle
        else if (newPaintObject instanceof Circle2DObject) {
            
            Point2D.Float point1 = newPaintObject.getPoint(0);
            Point2D.Float point2 = newPaintObject.getPoint(1);
            
            if (!newPaintObject.isShutter) {
                float[] thePoints = new float[4];
                
                
                thePoints[0] = point1.x+1f;
                thePoints[1] = point1.y+1f;
                
                thePoints[2] = point2.x+1f;
                thePoints[3] = point2.y+1f;
                getCurrentLayer().add(new PresentationStateGraphicObject(ps.addGraphicObject(currentLayerIndex,applyTo),0,jDVPSGraphicType.DVPST_circle,thePoints,newPaintObject.getType(),newPaintObject.filled,screenSize,aff));
            }
            else {
                
                shutterList.addCircularShutter((int)(point1.x+1f),(int)(point1.y+1f),(int)point1.distance(point2));
            }
        }
        //Interpolated.
        else if (newPaintObject instanceof InterpolatedObject) {
            float thePoints[] = new float[newPaintObject.getObjectPoints().size()*2];
            
            for (int i = 0; i< newPaintObject.getObjectPoints().size(); i++) {
                thePoints[2*i] = newPaintObject.getCopyPointAt(i).x+1f;
                thePoints[2*i+1] = newPaintObject.getCopyPointAt(i).y+1f;
            }
            
            getCurrentLayer().add(new PresentationStateGraphicObject(ps.addGraphicObject(currentLayerIndex,applyTo),0,jDVPSGraphicType.DVPST_interpolated,thePoints,newPaintObject.getType(),newPaintObject.filled,screenSize,aff));
        }
        
        
    }
    
    
    /**
     * Deletes all objects of the PresentationStateGraphicsHandler and
     * rebuilds them. Should be called if a new jDVPresentationState object is
     * available in the jDVInterface.
     *
     * @param screenSize The size of the available size for drawing the image.
     * @since 30.04.1999
     */
    
    public void setNewPresentationState(Dimension screenSize) {
        if (havePresentationState) deleteAll();
        //int status = dvi.resetPresentationState();
        buildPresentationState();
        setScreenSize(screenSize);
    }
    
    /**
     * Resets the presentation state to the last saved status.
     *
     * @since 30.04.1999
     */
    public void resetPresentationState() {
        deleteAll();
        int status = dvi.resetPresentationState();
        buildPresentationState();
        setScreenSize(screenSize);
    }
    
    
    /**
     * Deletes all objects of the PresentationStateGraphicsHandler.
     *
     * @since 30.04.1999
     */
    public void deleteAll() {
        
        for(int i = 0; i <listPresentationStateGraphicLayer.size(); i++) {
            ((PresentationStateGraphicLayer) listPresentationStateGraphicLayer.elementAt(0)).deleteAll();
            listPresentationStateGraphicLayer.removeElementAt(0);
        }
        
        
        currentLayerIndex = -1;
        currentLayerObjectIndex = -1;
        listPresentationStateGraphicLayer = null;
        shutterList.deleteAll();
        shutterList = null;
        zoomValue=-1;
        ps = null;
        System.gc();
        
    }
    /**
     * Deletes the currently active PresentationStateLayerObject,
     * specified with the currentLayerObjectIndex.
     *
     * @since 30.04.1999
     */
    
    public void deleteCurrentObject() {
        if (currentLayerObjectIndex != -1)deleteObjectAtIndex(currentLayerObjectIndex);
    }
    
    /**
     * Deletes the specified PresentationStateLayerObject in
     * the currently active PresentationStateGraphicLayer.
     *
     * @param index The index of the deleting PresentationStateLayerObject in
     * the currently active PresentationStateGraphicLayer.
     *
     * @since 30.04.1999
     */
    public void deleteObjectAtIndex(int index) {
        ((PresentationStateGraphicLayer) listPresentationStateGraphicLayer.elementAt(currentLayerIndex)).deleteObjectAt(index);
        currentLayerObjectIndex = -1;
    }
    
    /**
     * Deletes the  PresentationStateGraphicLayer with the specified index
     *
     * @param index The index of the deleting PresentationStateGraphicLayer
     * @since 30.04.1999
     */
    public void deleteLayer(int index) {
        //Delete layer
        ((PresentationStateGraphicLayer) listPresentationStateGraphicLayer.elementAt(index)).deleteAll();
        listPresentationStateGraphicLayer.removeElementAt(index);
        ps.removeGraphicLayer(index);
        
        //Sets new indices for all PresentationStateGraphicLayer greater than the specified index
        for (int i = index; i < listPresentationStateGraphicLayer.size(); i++) {
            ((PresentationStateGraphicLayer) listPresentationStateGraphicLayer.elementAt(i)).index = ((PresentationStateGraphicLayer) listPresentationStateGraphicLayer.elementAt(i)).index-1;
        }
        
        //Select new  active listPresentationStateGraphicLayer
        if (listPresentationStateGraphicLayer.size() >0 ) currentLayerIndex = 0;
        else currentLayerIndex = -1;
        
    }
    
    /**
     * Insert a new PresentationStateGraphicLayer with a RGB -Color.
     *
     * @param label Label of the layer.
     * @param description Decription of the layer.
     * @param r Red color value.
     * @param g Green color value.
     * @param b Blue color value.
     * @see PresentationStateGraphicLayer
     * @since 30.04.1999
     */
    public void insertNewColorLayer(String label, String description, int r,int g, int b) {
        listPresentationStateGraphicLayer.add(new PresentationStateGraphicLayer(listPresentationStateGraphicLayer.size(),ps,label, description, r,g,b,overlayList));
    }
    
    
    /**
     * Insert a new PresentationStateGraphicLayer with a Gray -Color.
     *
     * @param label Label of the layer.
     * @param description Decription of the layer.
     * @param monochome Gray color value.
     * @see PresentationStateGraphicLayer
     * @since 30.04.1999
     */
    public void insertNewMonochromeLayer(String label, String description, float monochome) {
        listPresentationStateGraphicLayer.add(new PresentationStateGraphicLayer(listPresentationStateGraphicLayer.size(),ps,label, description, monochome,overlayList));
        
    }
    
    /**
     * Gets the  PresentationStateGraphicLayer with the specified index.
     * Returns null if no object with the specified index exist.
     *
     * @param index Specifies the PresentationStateGraphicLayer.
     * @return The  PresentationStateGraphicLayer with the specified index.
     * @see PresentationStateGraphicLayer
     * @since 30.04.1999
     */
    public PresentationStateGraphicLayer getLayerAtIndex(int index) {
        if (index == -1) return null;
        else return ((PresentationStateGraphicLayer) listPresentationStateGraphicLayer.elementAt(index));
    }
    
    /**
     * Gets the  Color of the currently active PresentationStateGraphicLayer.
     * Returns null if no PresentationStateGraphicLayer is active.
     *
     * @return The color of the currently active PresentationStateGraphicLayer.
     * @see PresentationStateGraphicLayer
     * @since 30.04.1999
     */
    public Color getCurrentColor() {
        if (getCurrentLayer() == null) return Color.white;
        else return getCurrentLayer().getColor(8);
    }
    
    /**
     * Sets a new PresentationStateTextObject to the currently active
     * PresentationStateGraphicLayer.
     *
     * @param text The text of the new PresentationStateTextObject.
     * @param isBoundingBox If true the PresentationStateTextObject has a bounding box.
     * @param isAnchor If ture the PresentationStateTextObject has an anchor point.
     * @param isboth If true the PresentationStateTextObject has an anchor point and a bounding box.
     * @param isAnchorDisplay If true the exitsting anchor point is display relative.
     * @param isBoxDisplay If true the existing bounding box is display realtive.
     * @param isAnchorVisible If true the existing anchor point have a visible anchor point.
     * @since 30.04.1999
     * @see PresentationStateGraphicLayer
     * @see PresentationStateTextObject
     */
    public void setNewText( String text,
    boolean isBoundingBox,
    boolean isAnchor,
    boolean isAnchorDisplay,
    boolean isBoxDisplay,
    boolean isAnchorVisible,
    int just,
    int rot,
    int applyTo) {
        jDVPSTextObject newText =ps.addTextObject(currentLayerIndex,applyTo);
        
        newText.setText(text);
        
        getCurrentLayer().add(new PresentationStateTextObject(newText,0,screenSize,isAnchor,isBoundingBox,isAnchorDisplay,isBoxDisplay,isAnchorVisible, just, rot));
        currentLayerObjectIndex = getCurrentLayer().listPresentationStateObject.size()-1;
        
    }
    
    
    /**
     * Moves a PresentationStateLayerObject form one
     * PresentationStateGraphicLayer to an other.
     *
     * @param newLayer The index of the new PresentationStateGraphicLayer.
     * @param oldLayer The index of the current PresentationStateGraphicLayer.
     * @param objectIndex The index of the moving PresentationStateLayerObject in the current PresentationStateGraphicLayer.
     * @see PresentationStateGraphicLayer
     * @see PresentationStateTextObject
     * @since 30.04.1999
     */
    public void moveToLayer(    int newLayer,
    int oldLayer,
    int objectIndex, int applyTo) {
        PresentationStateGraphicLayer oldPresentationStateGraphicLayer =((PresentationStateGraphicLayer) listPresentationStateGraphicLayer.elementAt(oldLayer));
        PresentationStateGraphicLayer newPresentationStateGraphicLayer =((PresentationStateGraphicLayer) listPresentationStateGraphicLayer.elementAt(newLayer));
        PresentationStateLayerObject moveObject = oldPresentationStateGraphicLayer.layerObjectAt(objectIndex);
        
        //move in c++
        if (moveObject instanceof PresentationStateTextObject) {
            ps.moveGraphicObject( oldLayer, moveObject.getIndex(),  newLayer, applyTo);
        }
        if (moveObject instanceof PresentationStateGraphicObject) {
            ps.moveGraphicObject( oldLayer, moveObject.getIndex(),  newLayer,applyTo);
        }
        
        //move in java
        oldPresentationStateGraphicLayer.removeObjectAt(objectIndex);
        newPresentationStateGraphicLayer.add(moveObject);
    }
    
    /**
     * Returns the zoom value.
     *
     * @return The zoom value.
     * @since 30.04.1999
     */
    public double getZoomValue() {
        return zoomValue;
    }
    
    /**
     * Sets the zoom value.
     *
     * @param  zoomValue The new zoom value.
     * @since 30.04.1999
     */
    public void setZoomValue(double zoomValue) {
        this.zoomValue = zoomValue;
    }
    
    
    /**
     * Updates the PresentationStateGraphicsHandler without changing the enablePresentationState status.
     *
     * @since 30.04.1999
     */
    public void updatePresentationState() {
        deleteAll();
        boolean temp = enablePresentationState; // enable... wird in build..() berschrieben
        buildPresentationState();
        enablePresentationState = temp;
        setScreenSize(screenSize);
    }
    
    public void getPrintImageBuffer(TransformationConverter transConv) {
        
        //Loads Size of the bitmap
        jIntByRef h = new jIntByRef();
        jIntByRef w = new jIntByRef();
        if ( ps.getPrintBitmapWidthHeight(w,h)!= jE_Condition.EC_Normal) {
            JOptionPane.showMessageDialog(null,"Error: Can not determine bitmap size");
            return ;
        }
        
        //Init shortArray
        short[] pix = new short[(int)ps.getPrintBitmapSize()/2];
        
        //Loads the print bitmap
        if (ps.getPrintBitmap(pix, ps.getPrintBitmapSize()) == jE_Condition.EC_Normal) {
            
            BufferedImage printImage;
            WritableRaster wr;
            DataBufferUShort dbs;;
            int bandOffsets[] = {0};
            
            //Aspect ration
            double asRatio = ps.getPrintBitmapPixelAspectRatio();
            
            
            //Init BurreredImage with print bitmap
            dbs = new DataBufferUShort( pix,pix.length);
            wr = Raster.createInterleavedRaster(dbs,w.value,h.value,w.value,1, bandOffsets,null);
            printImage = new BufferedImage(GrayColorIndex.getGray12BitColorModel(), wr, GrayColorIndex.getGray12BitColorModel().isAlphaPremultiplied(), null);
            DisplayArea da = getDisplayArea();
            
            
            //Saves the current screenSize
            Dimension saveScreen = screenSize;
            
            
            int width = da.getBrhcX()-da.getTlhcX();
            int height = da.getBrhcY()-da.getTlhcY();
            
            double        printZoom ;
            int rot = ps.getRotation();
            
            if ((rot ==1) ||(rot== 3)) {
                printZoom= (double)(h.value /width);
                setScreenSize(new Dimension(h.value, w.value));
            }
            else {
                
                printZoom= (double)(w.value /width);
                setScreenSize(new Dimension(w.value, h.value));
            }
            System.err.println("printZoom: "+printZoom);
            
            //Loads transformations
            AffineTransform aff = transConv.getPrintTransformation(printZoom);
            AffineTransform overlayAff = transConv.getPrintOverlayTransformation();
            
            Graphics2D g2 = (Graphics2D) printImage.createGraphics();
            
            shutterList.drawShutter(g2, aff, w.value,h.value, 12);
            
            
            drawPaintStructure(printImage,aff,overlayAff,true,12, true);
            //drawPaintStructure(printImage,aff,new AffineTransform(),true,12, true);
            
            
            setScreenSize(saveScreen);
            
            if (dvi.saveGrayscaleHardcopyImage(pix, w.value, h.value, asRatio)== jE_Condition.EC_Normal) {
                Controller.instance().firePrint(new PrintEvent(this,PrintEvent.ID_HC));
            }
            else {
                JOptionPane.showMessageDialog(null,"Error: Can not print image");
                
            }
            
        }
        else {
            JOptionPane.showMessageDialog(null,"Error: Can not create print display");
        }
        
        pix = null;
        System.gc();
        System.err.println("end-print");
    }
    
    /**
     * Selects one frame of a multiframe image. This affects the image bitmap
     * that is rendered, the overlay bitmaps and the visibility of graphic and text
     * objects. This method may only be called when an image is attached to the
     * presentation state.
     * @param idx frame number in the range [1..getImageNumberOfFrames()]
     */
    public void selectImageFrameNumber(int idx) {
        if ((idx >= 1)&&(idx <= getImageNumberOfFrames()))
            ps.selectImageFrameNumber(idx);
    }
    
    
    /**
     * Selects the next frame of a multiframe image. This affects the image bitmap
     * that is rendered, the overlay bitmaps and the visibility of graphic and text
     * objects. This method may only be called when an image is attached to the
     * presentation state.
     * @param idx frame number in the range [1..getImageNumberOfFrames()]
     */
    public  int getSelectedImageFrameNumber() {
        return ps.getSelectedImageFrameNumber();
        
    }
    
    /**
     *  Selects the next frame of a multiframe image.
     *  @return Number of the selected frame
     */
    public  int selectNextFrame() {
        
        if (getSelectedImageFrameNumber() < getImageNumberOfFrames()) {
            selectImageFrameNumber(getSelectedImageFrameNumber()+1);
            return getSelectedImageFrameNumber()+1;
        }
        else return getSelectedImageFrameNumber();
        
    }
    /**
     *  Selects the next frame of a multiframe image.
     *  @return Number of the selected frame
     */
    public  int selectPreviousFrame() {
        if (getSelectedImageFrameNumber() > 1) {
            selectImageFrameNumber(getSelectedImageFrameNumber()-1);
            return getSelectedImageFrameNumber()-1;
        }
        else return 1;
        
    }
    
    /**
     * Returns the number of frames of the current image.
     */
    public int getImageNumberOfFrames() {
        jIntByRef num = new jIntByRef();
        ps.getImageNumberOfFrames(num);
        return num.value;
    }
    
    
    
    
    
    
    /**
     * Gets the number of image references contained in the current presentation state.
     *  @return number of image references, 0 if an error occurred.
     */
    public int getNumberOfImages() {
        return dvi.getNumberOfImageReferences();
    }
    /**
     *  Loads an image which referenced by the current presentation
     *  state and needs to be contained in the database.
     *  This method acquires a database lock which must be explicitly freed by the user.
     *  @param idx index of the image to be loaded getNumberOfImage();
     */
    public  void selectImageNumber(int idx) {
        if ((idx >= 1)&&(idx <= getNumberOfImages())) {
            dvi.loadReferencedImage(idx-1,true);
            dvi.releaseDatabase();
            Controller.instance().fireEvent(new UpdateBrowserEvent(this));
            selectedImageNumber = idx;
            Controller.instance().fireStatus(new SignedStatusEvent(this,
            SignedStatusEvent.LOADIMAGE,
            dvi.getCurrentSignatureStatus(jDVPSObjectType.DVPSS_image),
            dvi.getCurrentSignatureStatus(jDVPSObjectType.DVPSS_structuredReport)));
            
        }
    }
    
    /**
     *  Gets the index of the currently selected image.
     *  @return index of the currently selected image, 0 if an error occurred
     */
    public int getSelectedImageNumber() {
        return selectedImageNumber;
    }
    
    
    
    /**
     *  Selects the next frame of a multiframe image.
     *  @return Number of the selected frame
     */
    public  int selectNextImage() {
        
        if (getSelectedImageNumber() < getNumberOfImages()) {
            selectImageNumber(getSelectedImageNumber()+1);
            return getSelectedImageNumber()+1;
        }
        else return getNumberOfImages();
        
    }
    /**
     *  Selects the next frame of a multiframe image.
     *  @return Number of the selected frame
     */
    public  int selectPreviousImage() {
        if (getSelectedImageNumber() > 1) {
            selectImageNumber(getSelectedImageNumber()-1);
            return getSelectedImageNumber()-1;
        }
        else return 1;
        
    }
    
    
}

/*
 *  CVS Log
 *  $Log: PresentationStateGraphicsHandler.java,v $
 *  Revision 1.2  2003/09/08 10:17:26  kleber
 *  Bugfix: The Displayed Area is defined after the spatial transformation.
 *
 *  Revision 1.1.1.1  2001/06/06 10:32:30  kleber
 *  Init commit for DICOMscope 3.5
 *  Create new CVS
 *
 */