int iRange = iFinal-iStart+1; // width uncut by image border
int iWidth = iTail-iHead+1; // width possibly cut by the image
int jHeight = jTail-jHead+1;
CategoryWeight[] twgts;
CategoryWeight twgt = null;
int idx, twgts_length;
float tot_wgt, height_per_wgt;
int iLevel, iDelta, iCenter;
int jLevel, jDelta, jCenter;
int jDeltaTotal;
boolean isInclusive;
jDeltaTotal = 0;
twgts = shade.arrayOfCategoryWeights();
twgts_length = twgts.length;
if ( DisplayType == CUMULATIVE_INCLUSION_ID
|| DisplayType == CUMULATIVE_EXCLUSION_ID
|| DisplayType == CUMULATIVE_EXCLUSION_BASE_ID ) {
isInclusive = ( DisplayType == CUMULATIVE_INCLUSION_ID );
if ( isInclusive ) {
Arrays.sort( twgts, CategoryWeight.INCL_RATIO_ORDER );
// Compute the pixel height per unit weight
tot_wgt = 0.0f;
for ( idx = 0; idx < twgts_length; idx++ ) {
twgt = twgts[ idx ];
if ( twgt.getCategory().isVisible() )
tot_wgt += twgt.getRatio( isInclusive );
}
height_per_wgt = (float) jHeight / tot_wgt;
}
else {
Arrays.sort( twgts, CategoryWeight.EXCL_RATIO_ORDER );
height_per_wgt = jHeight;
}
// set sub-rectangles' height from the bottom, ie. jHead+jTail
jLevel = jHead + jHeight; // jLevel = jTail + 1
for ( idx = twgts_length-1; idx >= 0; idx-- ) {
twgt = twgts[ idx ];
if ( twgt.getCategory().isVisible() ) {
jDelta = (int) ( height_per_wgt
* twgt.getRatio( isInclusive ) + 0.5f );
if ( jDelta > 0 ) {
if ( jLevel > jHead ) {
if ( jLevel-jDelta >= jHead ) {
jLevel -= jDelta;
twgt.setPixelHeight( jDelta );
}
else {
twgt.setPixelHeight( jLevel - jHead );
jLevel = jHead;
}
}
else
twgt.setPixelHeight( 0 );
}
else
twgt.setPixelHeight( 0 );
}
else
twgt.setPixelHeight( 0 );
jDeltaTotal += twgt.getPixelHeight();
}
shade.setTotalPixelHeight( jDeltaTotal ); // for isPixelIn()
}
else if ( DisplayType == OVERLAP_INCLUSION_ID
|| DisplayType == OVERLAP_EXCLUSION_ID ) {
isInclusive = ( DisplayType == OVERLAP_INCLUSION_ID );
if ( isInclusive )
Arrays.sort( twgts, CategoryWeight.INCL_RATIO_ORDER );
else
Arrays.sort( twgts, CategoryWeight.EXCL_RATIO_ORDER );
jLevel = Integer.MAX_VALUE; // JLevel should be named as JDelta_prev
iDelta = iRange;
for ( idx = twgts_length-1; idx >= 0; idx-- ) {
twgt = twgts[ idx ];
if ( twgt.getCategory().isVisible() ) {
jDelta = (int) ( twgt.getRatio( isInclusive ) * jHeight
+ 0.5f );
twgt.setPixelHeight( jDelta );
if ( jDelta >= jLevel )
iDelta -= MinCategorySeparation;
twgt.setPixelWidth( iDelta );
jLevel = jDelta;
}
else
twgt.setPixelHeight( 0 );
}
}
else { // if ( DisplayType == FIT_MOST_LEGENDS_ID )
Arrays.sort( twgts, CategoryWeight.INCL_RATIO_ORDER );
int num_visible_twgts = 0;
for ( idx = 0; idx < twgts_length; idx++ ) {
if ( twgts[ idx ].getCategory().isVisible() )
num_visible_twgts++;
}
jDelta = (int) ( (float) jHeight / num_visible_twgts );
if ( jDelta < MinCategoryHeight )
jDelta = MinCategoryHeight;
// set sub-rectangles' height from the bottom, ie. jHead+jTail
jLevel = jHead + jHeight; // jLevel = jTail + 1
for ( idx = twgts_length-1; idx >= 0; idx-- ) {
twgt = twgts[ idx ];
if ( twgt.getCategory().isVisible() ) {
if ( jLevel > jHead ) {
if ( jLevel-jDelta >= jHead ) {
jLevel -= jDelta;
twgt.setPixelHeight( jDelta );
}
else {
twgt.setPixelHeight( jLevel - jHead );
jLevel = jHead;
}
}
else
twgt.setPixelHeight( 0 );
}
else
twgt.setPixelHeight( 0 );
}
}
// Fill the color of the sub-rectangles from the bottom, ie. jHead+jTail
int num_sub_rects_drawn = 0;
if ( DisplayType == OVERLAP_INCLUSION_ID
|| DisplayType == OVERLAP_EXCLUSION_ID ) {
// iBoxXXXX : variables that twgt isn't cut by image border
int iBoxHead, iBoxTail, iBoxWidth;
jCenter = jHead + jHeight / 2; // i.e. jCenter % jHead & jTail
iCenter = iStart + iRange / 2; // i.e. iCenter % iStart & iFinal
for ( idx = twgts_length-1; idx >= 0; idx-- ) {
twgt = twgts[ idx ];
jDelta = twgt.getPixelHeight();
iBoxWidth = twgt.getPixelWidth();
if ( jDelta > 0 && iBoxWidth > 0 ) {
iBoxHead = iCenter - iBoxWidth / 2;
iBoxTail = iBoxHead + iBoxWidth;
iLevel = ( iBoxHead >= 0 ? iBoxHead : 0 );
iDelta = ( iBoxTail < iImageWidth ?
iBoxTail : iImageWidth ) - iLevel;
g.setColor( twgt.getCategory().getColor() );
g.fillRect( iLevel, jCenter-jDelta/2, iDelta, jDelta );
num_sub_rects_drawn++;
}
}
}
else {
/*
if ( DisplayType == FIT_MOST_LEGENDS_ID )
|| DisplayType == CUMULATIVE_INCLUSION_ID
|| DisplayType == CUMULATIVE_EXCLUSION_ID
|| DisplayType == CUMULATIVE_EXCLUSION_BASE_ID )
*/
jLevel = jHead + jHeight; // jLevel = jTail + 1
if ( DisplayType == CUMULATIVE_EXCLUSION_ID )
jLevel -= ( jHeight - jDeltaTotal ) / 2;
for ( idx = twgts_length-1; idx >= 0; idx-- ) {
twgt = twgts[ idx ];
jDelta = twgt.getPixelHeight();
if ( jDelta > 0 ) {
jLevel -= jDelta;
g.setColor( twgt.getCategory().getColor() );
g.fillRect( iHead, jLevel, iWidth, jDelta );
num_sub_rects_drawn++;
}
}
}