/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package medusa.example;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.geom.Rectangle2D;
import java.util.Iterator;
import medusa.MedusaSettings;
import medusa.applet.MedusaAppletPanel;
import medusa.applet.MedusaLite;
import medusa.graph.Edge;
import medusa.graph.Node;

/**
 *
 * @author Sean
 */
public class GeoAppletPanel extends MedusaAppletPanel{

    
    public GeoAppletPanel(MedusaSettings stringSettings, MedusaLite parent,
            String linkStart, String linkEnd){
        
        super(stringSettings,parent,linkStart,linkEnd);
       
        
    }
    
    /**
     * Override paintComponent and the mouseClicked action
     * @param g
     */
    
    @Override
     public synchronized void paintComponent(Graphics g) {
        
       Graphics2D g2d =prePaint(g);

        if (hideWhenMove){
            if (!isRunning()&(getPick()!=null)){
                
                paintVisibleNet(g2d);
            } else {
                paintNet(g2d);
            }
        } else {
            paintNet(g2d);
        }
        
        
        g2d.setStroke(new BasicStroke(1));
        
    }
     
    
     
     public void paintVisibleNet(Graphics2D g2d){
        
        for (Iterator edges = getVisibleGraph().edgesIterator(); edges.hasNext() ;) {
            Edge e = (Edge) edges.next();
            int col=e.getType();            
                paintEdge(g2d, e);
        }
        
        
        //make sure confidence is restored
        g2d.setComposite(makeComposite(1.0f));
        for(Iterator it = graph.nodesIterator(); it.hasNext();) {
            Node node = (Node)it.next();
            paintNode(g2d, node);
        }
        //PaintTools.paintPath(g2d,10);
    }
     
     /**
      * Lets handle double clicks!
      * @param e
      */
         public void mouseClicked(MouseEvent e){
             if(e.getClickCount()==2){
                 Node n = getClosest(e);
                 if (n!=null){
                     n.setColor(Color.pink);
                     System.out.println("You double-clicked "+n.getLabel());
                 }
                 
             }
        //System.out.println("click!");
        
    }
    
     /**
     * Overridden paintEdge method.
     * This version just draws a line. You can try paths, 
     * icons or anything you like.
     * @param g graphics2d context
     * @param e the current edge
     */
    public void paintEdge(Graphics2D g, Edge e) {
        
        g.setPaint(Color.BLACK);
         Node from =graph.getNode(e.getFromName());
        Node to = graph.getNode(e.n2);
        int x1 = (int)from.getX();
        int y1 = (int)from.getY();
        int x2 = (int)to.getX();
        int y2 = (int)to.getY();
        
        // just draw the line
        g.drawLine(x1,y1,x2,y2);
        
        // draw the box halfway
        int centerPointX=(x1+x2)/2;
        int centerPointY=(y1+y2)/2;
        
        // allow some offset on the top left corner
        int offsetE=8;

        // maybe draw a box around it in lightblue
        g.setColor(Color.blue.brighter());
        Rectangle2D box=new Rectangle2D.Double(centerPointX-offsetE, centerPointY-offsetE,2*offsetE, 2*offsetE);
        g.fill(box);
        g.setColor(Color.white);
        // for instance, draw the edge type as an integer
        g.drawString(Integer.toString(e.getType()), centerPointX-offsetE, centerPointY+offsetE);
        
    }
}

 

Ads help maintain this website.