📄 kmeansinfoclustering.java
字号:
int i;
angle[0]=0;
for(i=1;i<numClust;i++) angle[i]=angle[i-1]+(int)((360) * prc[i-1]);
for(;i<angle.length;i++) angle[i]=360;
final JLabel l = new JLabel();
final pieChartIcon p = new pieChartIcon(numClust);
l.addMouseMotionListener(new MouseMotionAdapter(){
public void mouseMoved(MouseEvent e)
{
int pos;
pos=p.getPosXY(e.getX(),e.getY());
if(pos!=-1) l.setToolTipText(tableClusters.getValueAt(pos,2).toString());
else l.setToolTipText("");
}
});
p.setAngle(angle);
l.setIcon(p);
return l;
}
private JLabel setPie2(int var)
{
int[] angle=new int[numClust];
int i;
angle[0]=0;
for(i=1;i<numClust;i++){
angle[i]=angle[i-1]+(int)((360) * info.infoCluster[i].percVar[var]);
}
for(;i<angle.length;i++){
angle[i]=360;
}
final JLabel l = new JLabel();
pieChartVars = new pieChartIcon(numClust);
l.addMouseMotionListener(new MouseMotionAdapter(){
public void mouseMoved(MouseEvent e)
{
int pos;
pos=pieChartVars.getPosXY(e.getX(),e.getY());
if(pos!=-1) l.setToolTipText(tableVars.getValueAt(pos,1).toString());
else l.setToolTipText("");
}
});
pieChartVars.setAngle(angle);
l.setIcon(pieChartVars);
return l;
}
private class pieChartIcon implements Icon
{
private final int DIAMETRO=100;
private int[] angle;
private double centreX,centreY;
public pieChartIcon (int numSpicchi)
{
centreX=0;
centreY=0;
angle=new int[numSpicchi+1];
}
public void setAngle(int[] posAngoli)
{
int i;
for(i=0;i<angle.length-1;i++) angle[i]=posAngoli[i];
angle[i]=360;
}
public int getIconWidth()
{
return DIAMETRO;
}
public int getIconHeight()
{
return DIAMETRO;
}
public void paintIcon(Component c, Graphics g, int x, int y)
{
float len=angle.length;
centreX=x+(DIAMETRO/2);
centreY=y+(DIAMETRO/2);
for(int i=0;i<len-1;i++)
{
g.setColor(JavaWatColor.getColor(i));
g.fillArc(x,y,DIAMETRO,DIAMETRO,angle[i],angle[i+1]-angle[i]);
g.setColor(Color.BLACK);
g.drawArc(x,y,DIAMETRO,DIAMETRO,angle[i],angle[i+1]-angle[i]);
}
}
public int getPosXY(int x,int y)
{
int ret=1;
double angl;
if(Math.sqrt(Math.pow((x-centreX),2)+Math.pow((y-centreY),2))>DIAMETRO/2) return -1;
//if(x-centreX==0) return 0;
angl= Math.toDegrees(Math.atan((centreY-y)/(x-centreX)));
if(x<centreX) angl=180 + angl;
if(angl<0) angl=360+angl;
//System.out.println("ANGOLO= " + angl);
for(;ret<angle.length;ret++) if(angl<angle[ret]) break;
return (ret-1);
}
}
/** MODEL PER TABELLA CLUSTER DETAILS **/
private class clustDetModel extends AbstractTableModel{
private String[] colHeader={"Cluster","Num. observations","Percentage",""};
private int[] Elem = null;
private double[] Perc = null;
DinamicFormat f = new DinamicFormat("###.###%",0); //????? CONTROLLARE A COSA E SE SERVE
public clustDetModel(int[] el,double perc[]){
Elem = el;
Perc = perc;
}
public int getRowCount() {
return Elem.length;
}
public int getColumnCount() {
return colHeader.length;
}
public Object getValueAt(int rowIndex, int columnIndex) {
if(rowIndex >= 0){
if(columnIndex == 0) {
return "Cluster " + (rowIndex+1);
}
if(columnIndex == 1) {
return Integer.toString(Elem[rowIndex]);
}
if(columnIndex == 2) {
return f.format(Perc[rowIndex]);
}
if(columnIndex == 3) {
return JavaWatColor.getColor(rowIndex);
}
}
return null;
}
/**
* Returns name for each column (given its index) to be displayed inside
* table header
*/
public String getColumnName(int columnIndex) {
if (columnIndex < colHeader.length)
return colHeader[columnIndex];
else
return null;
}
/**
* Tells wether data contained in a specific cell(given row and column
* index) is editable or not. In this case distribution column is not
* editable, as editing functionality is implemented via edit button
*/
public boolean isCellEditable(int rowIndex, int columnIndex) {
return false;
}
}
/** MODEL PER TABELLA VARIABLES DETAILS **/
private class varsDetModel extends AbstractTableModel{
private String[] colHeader={"Cluster","Perc",""};
private double[] Perc = null;
DinamicFormat f = new DinamicFormat("###.###%",0); //????? CONTROLLARE A COSA E SE SERVE
public varsDetModel(double perc[]){
Perc = perc;
}
public void setNewPerc(double[] p){
Perc = p;
}
public int getRowCount() {
return Perc.length;
}
public int getColumnCount() {
return colHeader.length;
}
public Object getValueAt(int rowIndex, int columnIndex) {
if(rowIndex >= 0){
if(columnIndex == 0) {
return "Cluster " + (rowIndex+1);
}
if(columnIndex == 1) {
return f.format(Perc[rowIndex]);
}
if(columnIndex == 2){
return JavaWatColor.getColor(rowIndex);
}
}
return null;
}
/**
* Returns name for each column (given its index) to be displayed inside
* table header
*/
public String getColumnName(int columnIndex) {
if (columnIndex < colHeader.length)
return colHeader[columnIndex];
else
return null;
}
/**
* Tells wether data contained in a specific cell(given row and column
* index) is editable or not. In this case distribution column is not
* editable, as editing functionality is implemented via edit button
*/
public boolean isCellEditable(int rowIndex, int columnIndex) {
return false;
}
}
private class ColorRenderer extends JLabel implements TableCellRenderer {
public ColorRenderer(){
setOpaque(true); //MUST do this for background to show up.
setBorder(BorderFactory.createMatteBorder(2,5,2,5,Color.WHITE));
}
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column) {
setBackground((Color)value);
return this;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -