📄 sectors3dpanel.java
字号:
/**
* Copyright (C) 2006, Laboratorio di Valutazione delle Prestazioni - Politecnico di Milano
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package jmt.gui.jaba.panels;
import jmt.engine.jaba.Sector3D;
import jmt.engine.jaba.newPoint;
import jmt.engine.jaba.grahamScan;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.AffineTransform;
import java.awt.geom.Line2D;
import java.util.Vector;
import java.util.TreeSet;
import java.util.Iterator;
import java.text.DecimalFormat;
/**
* <p>Title: Sectors 3D Panel</p>
* <p>Description: This panel is used to show saturation sectors on 3-class models.</p>
* <p>This was heavily modified by Bertoli Marco to allow smart label positioning and
* skipping if too many labels must be shown on the image. Now it allows resizing too.</p>
*
* @author Bertoli Marco, Zanzottera Andrea
* Date: 8-feb-2006
* Time: 11.42.37
*/
public class Sectors3DPanel extends JPanel {
// Colors for sectors and lines
private static final Color SINGLE = new Color(255,200,0);
private static final Color DOUBLE = new Color(255,150,75);
private static final Color MORE = new Color(192,0,0);
private static final Color LINESINGLE = Color.gray;
private static final Color LINEDOUBLE = Color.CYAN;
private static final Color LINEMORE = Color.BLUE;
private static final Color BLACK = new Color(0,0,0);
private static final Color GREY = new Color(100,100,100);
// Background
private static final Color BGCOLOR = new Color(255,255,255);
// Stroke styles
private static final BasicStroke LABEL_LINE = new BasicStroke(2);
private static final BasicStroke SECTORS = new BasicStroke(1);
private static final BasicStroke LINES = new BasicStroke(1);
// Constants
private final double rad3 = Math.sqrt(3);
private final double rad3d2 = Math.sqrt(3) / 2;
private final double rad3d3 = Math.sqrt(3) / 3;
// Used to format numbers
private static final DecimalFormat formatter = new DecimalFormat("0.000");
// Resulta data and class names
private Vector s3d;
private String[] classNames;
// Used to immagazine traslation coordinates
private double traslationX, traslationY;
// Tells if graph is shown
private boolean isShown = false;
// Stores height of the graph
private int height;
// Label to show coordinates
private JLabel coordLabel;
/**
* Builds a new Sectors3D Panel to show results of 3-class models
* @param s3d results vector
* @param classNames array with class names
*/
public Sectors3DPanel(Vector s3d, String[] classNames){
super(new BorderLayout());
this.s3d = s3d;
this.classNames = classNames;
this.setBackground(BGCOLOR);
this.setBorder(BorderFactory.createEtchedBorder());
// Label to show coordinates
coordLabel = new JLabel();
coordLabel.setBorder(BorderFactory.createEtchedBorder());
coordLabel.setVisible(false);
coordLabel.setOpaque(true);
// Puts label on south-east corner
JPanel tmp = new JPanel(new BorderLayout());
tmp.add(coordLabel, BorderLayout.EAST);
tmp.setOpaque(false);
this.add(tmp, BorderLayout.SOUTH);
// Adds a mouseListener to show graph coordinates
this.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseMoved(MouseEvent e) {
if (isShown) {
String coord = getCoordinates(e.getX(), e.getY());
if (coord != null) {
coordLabel.setText(coord);
coordLabel.setVisible(true);
Sectors3DPanel.this.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
}
else {
coordLabel.setText("");
coordLabel.setVisible(false);
Sectors3DPanel.this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
}
}
});
}
/**
* Overrides default paint method to draw graph
* @param g graphic object. <b>Must</b> be an instance of Graphics2D
*/
public void paint(Graphics g)
{
isShown = false;
// This is the height of the graph
super.paint(g);
Graphics2D g2;
// If g is not instance of Graphic2D, aborts method
if (g instanceof Graphics2D)
g2 = (Graphics2D) g;
else
return;
Color sectorcolor;
int maxStationNameWidth = (int)g2.getFontMetrics().getStringBounds("StationX", g2).getWidth();
int limitHeight, limitWidth;
limitHeight = (int)(getHeight()*7.5/10.0);
limitWidth = (int)(((getWidth() / rad3) - maxStationNameWidth * 3 - 20)*.95);
height = (limitHeight < limitWidth)? limitHeight: limitWidth;
// If height is too small, aborts paint
if (height < 20)
return;
// Sets traslation coordinates
traslationX = .1 * height + 15;
traslationY = getHeight() - height * 0.1 - 10;
// Now traslates image
AffineTransform aT = g2.getTransform();
aT.setToTranslation(.1 * height + 15, getHeight() - height * 0.1 - 10);
g2.transform(aT);
// Cambia lo Stroke e il colore per evidenziare la linea
g2.setStroke(SECTORS);
// Disegna lo sfondo del triangolo
int[] xt = {0,(int)Math.floor(rad3d2*height),(int)Math.floor(rad3*height)};
int[] yt = {0,-1*height,0};
g2.setColor(SINGLE);
g2.fillPolygon(xt,yt,3);
if (s3d != null) {
// Used to sort label ascendingly by position
TreeSet labels = new TreeSet();
// per ogni settore
for (int i=0;i<s3d.size();i++)
{
Sector3D sector = (Sector3D)s3d.get(i);
//System.out.println("Totale: "+s3d.size()+", Parziale: "+i);
// Sono le coordinate dei punti che saranno passati al metodo fillPolygon
int[] xxp = new int[sector.CountPoint()];
int[] yyp = new int[sector.CountPoint()];
Vector points = new Vector();
// per ogni punto di un settore
for (int j=0;j<sector.CountPoint();j++)
{
double pb11 = sector.getx(j);
double pb12 = sector.gety(j);
int pb1 = (int)Math.floor(pb11*height);
int pb2 = (int)Math.floor(pb12*height);
//Aggiungo il punto al vettore da passare al Grahamscan
newPoint temp = new newPoint(pb1,pb2);
points.addElement(temp);
}
//Coloro il settore in base al numero di stazioni che vi saturano
int numstat = sector.getType();
if (numstat>2)
{
sectorcolor = MORE;
}
else if (numstat==2)
{
sectorcolor = DOUBLE;
}
else sectorcolor = SINGLE;
grahamScan gr = new grahamScan();
Vector ordpoint = gr.doGraham(points);
for (int j=0;j<ordpoint.size();j++)
{
xxp[j]=((newPoint)ordpoint.get(j)).x;
yyp[j]=-((newPoint)ordpoint.get(j)).y;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -