/*
 * BasicGraphRenderer.java
 *
 * Created on February 15, 2008, 1:38 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package medusa.display;

import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.event.MouseEvent;
import java.util.Iterator;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import medusa.MedusaSettings;
import medusa.graph.Edge;
import medusa.graph.Graph;
import medusa.graph.Node;

/**
 *
 * @author Sean
 */
public class BasicGraphRenderer extends JPanel {
    
    /** Creates a new instance of BasicGraphRenderer */
    public BasicGraphRenderer() {
    }
    
    public boolean alpha = true;
    
    public boolean arrow = true; // show arrow or not
    
    
    
    /**
     * The default color of edges
     */
    public Color basicEdgeColor = Color.gray;
    
    
    // Node size, was final, but we decided to make it settable
    public int nodeSize = 10;
    
    // correction for node size
    /**
     * The correction for nodes, i.e if the node size
     * is changed, the node will still center on its location
     */
    protected int correct = (int) (nodeSize / 2.);
    
    /**
     * The default font color
     */
    public Color fontColor = Color.black;
    
    /**
     * The current font size
     */
    public int fontSize = 10;
    
    
    
    // the graph object
    public Graph graph;
    
    public boolean hideWhenMove = true;
    
    public boolean label = true;
    
    
    // the initial font
    public Font nodeFont = new Font("TimesNewRoman", Font.PLAIN, 10);
    
    
    
    
    public boolean showAnnotation = false;
    
    public boolean showConfidence = true;
    
    
    // the attributes holder class
    private MedusaSettings stringSettings;
    
    
    // popup menu for editing
    //PopupMenu popup;
    
    
    
    /**
     * Clear graphics
     * @param g Graphics g
     */
    public void clear(Graphics g) {
        super.paintComponent(g);
    }
    
    

    
    /**
     * Returns the font color
     * @return Font color
     */
    public Color getFontColor(){
        return fontColor;
    }
    
    public Graph getGraph(){
        return graph;
        
    }
    
    
    /**
     * Returns number of nodes and edges
     * @return returns number of [nodes, edges, unique edges]
     */
    public String[] getGraphData(){
        String[] result = new String[3];
        result[0] = (new Integer(graph.getNodeSize())).toString();
        result[1] = (new Integer(graph.getEdgeSize())).toString();
        result[2] = (new Integer(graph.getUniqueEdgeSize())).toString();
        return result;
    }
    
     // the applications dimensions
    
    final int defaultSize=600;
    protected int panelWidth=defaultSize;
    protected int panelHeight=defaultSize;
    
        // Set new dimension   
    public Dimension dims=new Dimension(defaultSize,defaultSize);
    
    public Dimension getMinimumSize(){
        return dims;
        
    }
    
    
    public int getPanelHeight() {
        return panelHeight;
    }
    
    
    public int getPanelWidth() {
        return panelWidth;
    }
    
    
    /**
     * Apparently these functions must be overridden, according to
     * http://www.javaworld.com/javaworld/jw-09-2000/jw-0922-javatraps.html
     */
    public Dimension getPreferredSize(){
        return dims;
    }
    
    
    protected BasicStroke getStroke(float conf){
        float[] dashPattern1 = { 3, 2, 3, 2 };
        float[] dashPattern2 = { 1, 4, 1, 4 };
        if (conf>0.67)
            return new BasicStroke();
        if (conf>0.33)
            return new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10, dashPattern1, 0);
        return new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10, dashPattern2, 0);
    }
    
    

    

        public void setPanelWidth(int panelWidth) {
        this.panelWidth = panelWidth;
    }
    
    public void setPanelHeight(int panelHeight) {
        this.panelHeight = panelHeight;
    }
    
    
    /* Composite is used for fading effects
     */
    /**
     * Get the composite ranging from 1.0f (opaque) to 0.0f (invisible)
     * @param alpha Float 0.0 to 1.0
     * @return The composite
     */
    protected AlphaComposite makeComposite(float alpha) {
        int type = AlphaComposite.SRC_OVER;
        return (AlphaComposite.getInstance(type, alpha));
    }
    
    /*
     * Paint the panel. is called with repaint.
     * Try without synch
     */
    
    /**
     * Paints the component
     * @param g current graphics
     */
    public synchronized void paintComponent(Graphics g) {
        Graphics2D g2d = prePaint(g);
        //setSize(dims); 
        
        paintNet(g2d);
        
    }
    
  

    protected Graphics2D prePaint(final Graphics g) {
        
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setStroke(new BasicStroke(1));
        
        
        clear(g2d);
        return g2d;
    }
    


    public void paintEdge(Graphics2D g, Edge e){
        //if (!e.visible)
        //    return;
        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();
        
        // handle confidencei
        if (showConfidence){
            if (alpha)
                g.setComposite(makeComposite(e.getConf()));
            else
                g.setStroke(getStroke(e.getConf()) );
            
        }
        
        g.setPaint(basicEdgeColor);
        //if (e.visible)
        //g.setPaint(Color.red);
        if (pretty){
            Integer iii= new Integer(e.getType());
            //System.out.println(this.getStringSettings());
            //System.out.println(this.getStringSettings().getColor(iii));
            Color c = getStringSettings().getColor(iii) ;
            g.setPaint( c );
            //System.out.println(stringSettings.getLabel(e.color));
            
            PaintTools.paintPath(g, x1, y1, x2, y2, e.getOrientation(), BasicGraphPanel.offset, arrow);
        }  else{
            g.drawLine(x1,y1,x2,y2);
        }
    }
    
    // paint simple or more "pretty"
        public boolean pretty=false;
        
    /* The main paint method. Paints edges and nodes
     */
    
    /**
     * Paints nodes and edges
     * @param g2d Graphics2D g2d
     */
    public void paintNet(Graphics2D g2d){
        
        for (Iterator edges = graph.edgesIterator(); edges.hasNext() ; ) {
            Edge e = (Edge) edges.next();
            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);
        }
    }
    
       private int currentNodeColorSet=1;
    public void setNodeColorSet(int i){
        currentNodeColorSet=i;
    }
        public int getNodeColorSet(){
        return currentNodeColorSet;
    }
    
    /**
     * Paints out Node n
     * @param g Graphics2D
     * @param n Node n
     */
    public void paintNode(Graphics2D g, Node n){
        g.setStroke(new BasicStroke(1));
        g.setPaint(n.getColor(currentNodeColorSet));
        int x = (int) n.getX()- correct;
        int y = (int) n.getY() - correct;
        
        Shape shape = PaintTools.getShape(n.getShape(),x,y,nodeSize);
        g.fill(shape);
        g.setPaint(Color.black);
        if (n.isFixed())
            g.setPaint(Color.yellow);
        g.draw(shape);
        g.setPaint(fontColor);
        g.setFont(nodeFont);
        if (label)
            if (showAnnotation){
            
            g.drawString(n.getAnnotation(),x-2,y-2);
            } else
                g.drawString(n.getLabel(),x-2,y-2);
        
    }

    public int getCurrentNodeColorSet() {
        return currentNodeColorSet;
    }

    public void setCurrentNodeColorSet(int currentNodeColorSet) {
        this.currentNodeColorSet = currentNodeColorSet;
    }

    public MedusaSettings getStringSettings() {
        return stringSettings;
    }

    public void setStringSettings(MedusaSettings stringSettings) {
        this.stringSettings = stringSettings;
    }
    
    

    
}

 

Ads help maintain this website.