Package grafrajz

Source Code of grafrajz.MyCanvas

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

import grafrajz.utils.DrawableEdge;
import grafrajz.utils.DrawableNode;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.ArrayList;
import javax.swing.JPanel;


/**
*
* @author adam_fejes_dell
* A rajzterület megvalósítása. A paintComponent metódus felülírásával valósítja meg a gráf elemeinek kirajzolását
*/
public class MyCanvas extends JPanel{

    private GraphLogic gLogic;
    private ArrayList<DrawableEdge> edges;
    private ArrayList<DrawableNode> nodes;
   
    final private int NODE_SIZE;
   
   

    public MyCanvas(GraphLogic gLogic, int nodeSize) {
        this.gLogic = gLogic;
        this.edges = gLogic.getDrawableEdges();
        this.nodes = gLogic.getDrawableNodes();
       
        this.NODE_SIZE = nodeSize;
       
        setBackground(Color.WHITE);
    }
   
    /**
     * Gráf elemeinek kirajzolása
     * @param g
     */
    @Override
    public void paintComponent(Graphics g) {
         Graphics2D g2D=(Graphics2D) g;
        
         for(DrawableEdge edge : edges){
             g.setColor(edge.getEdge().getColor());
             g2D.drawLine(edge.getsPoint().x, edge.getsPoint().y, edge.getDestPoint().x, edge.getDestPoint().y);
            
             /**
              * Ha gráf irényított, nyilakat rajzol az élek végére
              */
             if(gLogic.isDirected()){
                 int x1 = edge.getsPoint().x, x2 = edge.getDestPoint().x,  y1 = edge.getsPoint().y, y2 = edge.getDestPoint().y;
                
                 double[] dir = {x2-x1, y2-y1}; //irány vektor gyártása
                 int dist =(int) Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2)); //vektor hossza
                 dir[0] /= dist; dir[1] /= dist; //normalizálom az irány vektort
                
                 int arrowTipx, arrowWing1x, arrowWing2x, arrowTipy, arrowWing1y, arrowWing2y;
                
                 //Nyílhegy pozíciója
                 arrowTipx = (int) (x1 + (dist - NODE_SIZE / 2) * dir[0]);
                 arrowTipy = (int) (y1 + (dist - NODE_SIZE / 2) * dir[1]);
                
                 //Nyíl egyik szárnyvégének a pozíciója
                 arrowWing1x = (int) (x1 + (dist - NODE_SIZE) * dir[0] + dir[1] * 10);
                 arrowWing1y = (int) (y1 + (dist - NODE_SIZE) * dir[1] + dir[0] * 10 * -1);
                
                 //Nyíl másik szárnyvégének a pozíciója
                 arrowWing2x = (int) (x1 + (dist - NODE_SIZE) * dir[0] + dir[1] * 10 * -1);
                 arrowWing2y = (int) (y1 + (dist - NODE_SIZE) * dir[1] + dir[0] * 10);
                


                 int[] xArray = new int[] {arrowWing1x, arrowTipx, arrowWing2x};
                 int[] yArray = new int[] {arrowWing1y, arrowTipy, arrowWing2y};
                 g2D.drawPolyline(xArray, yArray, 3);
             }
            
             /**
              * Ha a gráf súlyozott, berajzolja az élek címkéit
              */
             if(gLogic.isWeightened()){
                 g.setColor(Color.red);
                 g2D.drawString(
                         String.valueOf(edge.getEdge().getWeight()),
                         (edge.getDestPoint().x + edge.getsPoint().x) / 2 ,
                         (edge.getDestPoint().y + edge.getsPoint().y) / 2 );
             }
         }
        
         g.setColor(Color.blue);

         for(DrawableNode node : nodes){
             //Csúcs kirajzolása
             g.setColor(Color.blue);
             g2D.fillOval(node.getPos().x, node.getPos().y, NODE_SIZE, NODE_SIZE);
            
             //Csúcs címke kirajzolása
             g.setColor(Color.white);
             g2D.drawString(node.getNode().getName(), node.getPos().x + (int)NODE_SIZE / 7 * 3, node.getPos().y + (int)NODE_SIZE / 7 * 3);

         }
        
        
         //System.out.println("PAINTED!");
        
    }

   
}
TOP

Related Classes of grafrajz.MyCanvas

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.