package de.cluster.mvc.view;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.beans.PropertyChangeEvent;
import java.util.ArrayList;
import java.util.HashMap;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import de.cluster.mvc.controller.Controller;
public class DrawAreaPanel extends AbstractView{
/**
*
*/
private static final long serialVersionUID = 1L;
private ArrayList<double []> pointList;
private boolean arrayInitialized = false;
private boolean computeCluster;
private ArrayList<double []> clusterList;
private HashMap<Integer, ArrayList<double[]>> partitionHash;
boolean distances = false;
@SuppressWarnings("unused")
private Controller controller;
private JLabel compactnessLabel;
private JTextArea compactnessArea;
public DrawAreaPanel(Controller controller){
super();
this.controller = controller;
}
public void initComponents() {
setLayout(new FlowLayout());
compactnessLabel = new JLabel("Kompaktheit der Cluster: ");
compactnessArea = new JTextArea();
add(compactnessLabel);
add(compactnessArea);
}
public void paintComponent(Graphics g){
g.setColor(Color.LIGHT_GRAY);
g.fillRect(0,0,300,300);
g.setColor(Color.black);
if (arrayInitialized){
for (int i=0; i<pointList.size();i++){
int x=(int) pointList.get(i)[0];
int y=(int) pointList.get(i)[1];
g.drawLine(x, y, x, y);
}
}
if (computeCluster){
for (int i=0;i<clusterList.size();i++){
int x=(int)(clusterList.get(i)[0]);
int y= (int)(clusterList.get(i)[1]);
g.drawRect(x, y, 20, 20);
}
}
if(distances){
for(int j=0;j<partitionHash.size();j++){
for (int i=0;i<partitionHash.get(j).size();i++){
int x = (int) clusterList.get(j)[0];
int y = (int) clusterList.get(j)[1];
int x2=(int)partitionHash.get(j).get(i)[0];
int y2=(int)partitionHash.get(j).get(i)[1];
g.drawLine(x,y,x2,y2);
}
}
}
}
public void setPointList(ArrayList<double []> pointList){
this.pointList = pointList;
arrayInitialized = true;
}
public void showClusterOvals(ArrayList<double[]> arrayList){
computeCluster=true;
this.clusterList = arrayList;
}
public void setPartitionHash(HashMap<Integer, ArrayList<double []>> partitionHash){
this.partitionHash= partitionHash;
distances = true;
}
@SuppressWarnings("unchecked")
@Override
public void modelPropertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals("Random")) {
ArrayList<double[]> array = (ArrayList<double[]>) evt.getNewValue();
setPointList(array);
repaint();
}
if (evt.getPropertyName().equals("Cluster")) {
showClusterOvals((ArrayList<double[]>) (evt
.getNewValue()));
repaint();
}
if (evt.getPropertyName().equals("PartitionHash")) {
setPartitionHash((HashMap<Integer, ArrayList<double[]>>) (evt
.getNewValue()));
}
if (evt.getPropertyName().equals("Compactness")) {
compactnessArea.setText(""+evt.getNewValue());
// compactnessLabel = new JLabel("Kompaktheit der Cluster:" +evt.getNewValue());
}
}
}