/*
 * CitiesPanel.java
 *
 * Created on March 25, 2006, 9:20 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package cz.pcland.tsp;
import cz.pcland.ga.TSPIntegerIndividual;
import cz.pcland.gavisualization.*;
import javax.swing.*;
import java.awt.*;
import java.util.*;
import cz.pcland.functions.lexan.*;
import cz.pcland.functions.nodes.*;
import cz.pcland.functions.syntax.*;
import java.awt.geom.*;
import java.awt.font.*;

import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
/**
 *
 * @author petr
 */
public class CitiesPanel extends JPanel implements MouseListener {
    TSPInstance instance = null;
    
    public void setInstance(TSPInstance instance){
        this.instance = instance;
    }
    
    public TSPInstance getInstance() {
        return this.instance;
    }
    
    /** Creates a new instance of CitiesPanel */
    public CitiesPanel() {
        super();
        addMouseListener(this);
    }
    
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if(instance == null) return;
        
        
        Graphics2D g2 = (Graphics2D)g;
        
        
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        
        
        this.setBackground(Color.white);
        
        for(int i=0;i<instance.getCount();i++){
            Point p = instance.getCoordinates(i);
            g2.setColor(Color.blue);
            Ellipse2D.Double point = new Ellipse2D.Double(p.x,p.y,3,3);
            g2.fill(point);
        }
        
        if(instance.info == null) return;
        
        int[] data = ((TSPIntegerIndividual)instance.info.getBestString()).getString();
        if(data.length != 0){
            for(int ii=1;ii<data.length;ii++){                
                g2.setColor(Color.green);
                g2.draw(new Line2D.Float(instance.getCoordinates(data[ii-1]), instance.getCoordinates(data[ii])));
            }
            
            g2.draw(new Line2D.Float(instance.getCoordinates(data[0]), instance.getCoordinates(data[data.length-1])));
        }
    }
    /**
     * Invoked when the mouse button has been clicked (pressed
     * and released) on a component.
     */
    public void mouseClicked(MouseEvent e) {}
    
    /**
     * Invoked when a mouse button has been pressed on a component.
     */
    public void mousePressed(MouseEvent e) {
        if(instance == null)return;
        int x = e.getX();
        int y = e.getY();
        
        instance.addCity(new City(x,y));
        this.repaint();
    }
    
    /**
     * Invoked when a mouse button has been released on a component.
     */
    public void mouseReleased(MouseEvent e) {}
    
    /**
     * Invoked when the mouse enters a component.
     */
    public void mouseEntered(MouseEvent e) {}
    
    /**
     * Invoked when the mouse exits a component.
     */
    public void mouseExited(MouseEvent e) {}
    
    
}
