📄 sectors3dpanel.java
字号:
// Disegna i settori di saturazione
g2.setColor(sectorcolor);
g2.fillPolygon(xxp,yyp,sector.CountPoint());
if (numstat!=1){
g2.setColor(Color.BLACK);
g2.draw(new Polygon(xxp,yyp,sector.CountPoint()));
}
// Adds this sector to labels
double[] centre = sector.getCentre();
labels.add(new Caption(sector.getName(), centre[0], centre[1], sector.getType()));
}//per ogni settore
// Now draws captions and link them to the corresponding sector
Iterator i = labels.iterator();
// Stores prevoius y value to avoid collision
float previousy = Float.NEGATIVE_INFINITY;
// Value where label should be placed
float yvalue;
boolean skipSingle = false, skipDouble = false;
// Skips single and double sector labels if they are too many
Caption first = (Caption) labels.first();
float textHeight = (float)g2.getFontMetrics().getStringBounds(first.getLabel(), g2).getHeight();
if (first.getY() * height + textHeight * labels.size() > getHeight()) {
skipSingle = true;
// now checks if double have to be skipped too
int count = 0;
while (i.hasNext())
if (((Caption)i.next()).getStatN() > 1)
count ++;
// resets iterator for next cycle
i = labels.iterator();
// Decides if doubles have to be skipped too
if (first.getY() * height + textHeight * count > getHeight())
skipDouble = true;
}
while (i.hasNext()) {
Caption c = (Caption) i.next();
// Performs skipping if needed
if (skipSingle && c.getStatN() == 1)
continue;
else if (skipDouble && c.getStatN() == 2)
continue;
// Select color basing on number of station in this sector
if (c.getStatN() == 1)
g2.setColor(LINESINGLE);
else if (c.getStatN() == 2)
g2.setColor(LINEDOUBLE);
else
g2.setColor(LINEMORE);
yvalue = (float)c.getY()*height;
if (yvalue < previousy)
// collision detected
yvalue = previousy;
// Writes label and draw its line
g2.setStroke(LABEL_LINE);
g2.draw(new Line2D.Double(height*(rad3+.1)+10 -4, -yvalue, c.getX()*height,-c.getY()*height));
g2.setStroke(LINES);
g2.setColor(BLACK);
g2.drawString(c.getLabel(),(float)(height*(rad3+.1)+10),-yvalue+(float)g2.getFontMetrics().getStringBounds(c.getLabel(), g2).getHeight() / 2 - 4);
// updates previousy adding height of this label
previousy = yvalue + (float)g2.getFontMetrics().getStringBounds(c.getLabel(), g2).getHeight();
}
// Disegna gli assi
g2.setStroke(LINES);
sectorcolor = BLACK;
g2.setColor(sectorcolor);
Line2D class3 = new Line2D.Double(rad3d2*height,-1*height,rad3d2*height,-1.1*height);
Line2D class2 = new Line2D.Double(rad3*height,0,rad3*height +.1*height,.05*height);
Line2D class1 = new Line2D.Double(0,0,-.1*height,.05*height);
g2.draw(class3);
g2.draw(class2);
g2.draw(class1);
// Arrow on class 3 axis
Line2D fclass31 = new Line2D.Double(rad3d2*height,-1.1*height,rad3d2*height -0.05*height,-1.1*height +0.05*height);
Line2D fclass32 = new Line2D.Double(rad3d2*height,-1.1*height,rad3d2*height +0.05*height,-1.1*height +0.05*height);
g2.draw(fclass31);
g2.draw(fclass32);
// Arrow on class 2 axis
Line2D fclass21 = new Line2D.Double(rad3*height +.1*height,.05*height,rad3*height +.1*height -.06*height,.05*height +.03*height);
Line2D fclass22 = new Line2D.Double(rad3*height +.1*height,.05*height,rad3*height +.1*height -.03*height,.05*height -.06*height);
g2.draw(fclass21);
g2.draw(fclass22);
// Arrow on class 1 axis
Line2D fclass11 = new Line2D.Double(-.1*height,.05*height,-.1*height +.06*height,.05*height +0.03*height);
Line2D fclass12 = new Line2D.Double(-.1*height,.05*height,-.1*height +.03*height,.05*height -0.06*height);
g2.draw(fclass11);
g2.draw(fclass12);
// Axis labels
g2.setColor(GREY);
g2.drawString(classNames[2] +" %",(float)(height*rad3d2-g2.getFontMetrics().getStringBounds(classNames[2]+" %", g2).getWidth()),-height);
g2.drawString(classNames[1] +" %",(float)(height*rad3-g2.getFontMetrics().getStringBounds(classNames[1]+" %", g2).getWidth()),15);
g2.drawString(classNames[0] +" %",0,15);
// Graph is shown
isShown = true;
// Disegna il perimetro del triangolo
g2.setColor(Color.BLACK);
g2.draw(new Polygon(xt,yt,3));
}
}
/**
* This method will calculate mouse position in beta coordinates
* @param x mouse position x coordinate
* @param y mouse position y coordinate
* @return string with mouse position in beta coordinates or null if mouse is out of
* graph
*/
protected String getCoordinates(int x, int y) {
double b1 = rad3d3*((double)x-traslationX)/height-0.5*(traslationY-y)/height;
double b2 = (traslationY - y)/height;
double b0 = 1 - b1 - b2;
if (b0 >= 0 && b0 <= 1 &&
b1 >= 0 && b1 <= 1 &&
b2 >= 0 && b2 <= 1)
return classNames[0] + ": " + formatter.format(b0) + "% " +
classNames[1] +": " + formatter.format(b1) + "% " +
classNames[2] +": " + formatter.format(b2) + "%";
else
return null;
}
/**
* Inner class used to sort sector captions
* <br> Author: Bertoli Marco
*/
protected class Caption implements Comparable{
private double x,y;
private String label;
private int statN;
/**
* Builds a new Caption object
* @param label label of the caption
* @param x value of x coordinate
* @param y value of y coordinate
* @param statN number of stations
*/
public Caption(String label, double x, double y, int statN) {
this.label = label;
this.x = x;
this.y = y;
this.statN = statN;
}
/**
* Gets value of x
* @return value of x
*/
public double getX() {
return x;
}
/**
* value of y
* @return value of y
*/
public double getY() {
return y;
}
/**
* Gets label caption
* @return label caption
*/
public String getLabel() {
return label;
}
/**
* Gets station number in this sector
* @return station number in this sector
*/
public int getStatN() {
return statN;
}
/**
* <p>Compares this object with the specified object for order. Returns a
* negative integer, zero, or a positive integer as this object is less
* than, equal to, or greater than the specified object.</p>
* <p>Compares y values and if they are equals compares x values.</p>
*
* @param o the Object to be compared.
* @return a negative integer, zero, or a positive integer as this object
* is less than, equal to, or greater than the specified object.
* @throws ClassCastException if the specified object's type prevents it
* from being compared to this Object.
*/
public int compareTo(Object o) {
Caption c = (Caption) o;
// Compares y values and if they are equals, compares x
if (y < c.y)
return -1;
else if (y > c.y)
return 1;
else {
if (x < c.x)
return -1;
else if (x > c.x)
return 1;
else
return 0;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -