Package interfaz.utilidades

Source Code of interfaz.utilidades.Pizarra

package interfaz.utilidades;

import baseconocimiento.utilidades.Mapeo;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import javax.swing.JFrame;
import modelo.Barrio;
import modelo.Localidad;
import modelo.Mapa;


/**
*
* @author UNL
*/
public class Pizarra extends Canvas {

    private Mapa mapa;
    private boolean init = true;
    private int factorEscala = 3;
    ArrayList<Localidad> localidades;
    public Point[] extraerCoordenadasLocalidades() {
        localidades = this.mapa.getLocalidades();
        System.out.println("Num. Localidades: " + localidades.size());
        Point[] listaPuntos = new Point[localidades.size()];

        for (int i = 0; i < listaPuntos.length; i++) {
            int x = (int) localidades.get(i).getBarrio().getCoordenadaX();
            int y = (int) localidades.get(i).getBarrio().getCoordenadaY();
            Point punto = null;
            try {
                punto = new Point(x / this.factorEscala, y / this.factorEscala);
            } catch (Exception e) {
                System.out.println("Division por cero");
            }
            listaPuntos[i] = punto;
        }

        return listaPuntos;
    }

    public void graficarPuntos(Graphics2D g, Point[] points) {
        int cont = 0;
       
        for (Point point : points) {
            //recorremos por cada localidad sus vecinos
            ArrayList<Barrio> barrios = this.localidades.get(cont).getVecinos();
            System.out.println("-------------------------------");
            System.out.println("Tengo "+barrios.size());
            for (Barrio barrio : barrios) {
                System.out.println(barrio);
                graficarCamino(g, point, barrio.getPoint(this.factorEscala));
            }
            System.out.println("-------------------------------");
           
           
            g.setColor(Color.YELLOW);
            g.fillOval((int) point.getX()-2 , (int) point.getY()-2, 5, 5);
            g.setColor(Color.red);
            g.drawOval((int) point.getX() - 5, (int) point.getY()- 5, 10, 10);
           
            g.setColor(Color.WHITE);
            //g.drawString(this.localidades.get(cont).getBarrio().getDenominacion()+ "(" + point.getX() + "," + point.getY() + ")",
            //        (float) point.getX(), (float) point.getY());
            Localidad localidad= localidades.get(cont);
            g.drawString("  P" + (cont+1) + "(" + localidad.getBarrio().getCoordenadaX() + "," + localidad.getBarrio().getCoordenadaY() + ")", (float) point.getX(), (float) point.getY());
            cont++;
           
        }
    }

    public void graficarCamino(Graphics2D g, Point point1, Point point2) {
        g.setColor(Color.blue);
        g.drawLine((int) point1.getX(), (int) point1.getY(), (int) point2.getX(), (int) point2.getY());
    }

    /**
     *
     * @param g
     * @param point1 es un punto de origen que describe las reglas de la base del conocimiento
     * @param point2 es el punto de destino basado en las reglas del conocimiento     */
    public void trazarlinea(Graphics2D g, Point point1, Point point2) {
        g.drawLine(point1.x, point1.y, point2.x, point2.y);
    }
    private PizarraListener zoomAndPanListener;

    public Pizarra(Mapa mapaDatos) {
        this.zoomAndPanListener = new PizarraListener(this);
        this.addMouseListener(zoomAndPanListener);
        this.addMouseMotionListener(zoomAndPanListener);
        this.addMouseWheelListener(zoomAndPanListener);
        this.mapa = mapaDatos;
    }

    public Pizarra(int minZoomLevel, int maxZoomLevel, double zoomMultiplicationFactor) {
        this.zoomAndPanListener = new PizarraListener(this, minZoomLevel, maxZoomLevel, zoomMultiplicationFactor);
        this.addMouseListener(zoomAndPanListener);
        this.addMouseMotionListener(zoomAndPanListener);
        this.addMouseWheelListener(zoomAndPanListener);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(700, 500);
    }

    @Override
    public void paint(Graphics g1) {
        Dimension d = getSize();
        BufferedImage imagen = new BufferedImage(d.width, d.height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = (Graphics2D) imagen.getGraphics();
        imagen.getGraphics().setColor(Color.BLUE);
        //imagen.getGraphics().fillRect(0, 0, d.width, d.height);

        if (init) {
            // Initialize the viewport by moving the origin to the center of the window,
            // and inverting the y-axis to point upwards.
            init = false;

             // int xc = d.width / 2;
            System.out.println(" ancho"+d.width/2);
             //  int yc = d.height / 2;
            System.out.println("alto"+d.height/2);

            int xc = 0;
            int yc = d.height;

            g2.translate(xc, yc);

            g2.scale(1, -1);

            // Save the viewport to be updated by the ZoomAndPanListener
            zoomAndPanListener.setCoordTransform(g2.getTransform());

        } else {
            // Restore the viewport after it was updated by the ZoomAndPanListener
            g2.setTransform(zoomAndPanListener.getCoordTransform());
        }

        g2.setColor(Color.red);
        Font font = g2.getFont();
        AffineTransform affineTransform = new AffineTransform();
        affineTransform.scale(1,-1);
        g2.setFont(font.deriveFont(affineTransform));
        g2.setColor(Color.BLUE);
        graficarPuntos(g2, extraerCoordenadasLocalidades());
        g1.drawImage(imagen, 0, 0, this);
    }

    @Override
    public void update(Graphics g) {
        paint(g);
    }

    public static void main(String[] args) {

        JFrame frame = new JFrame("Pizarra");

        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        Pizarra chart = new Pizarra(new Mapa(1, "Loja", new Mapeo().mapearLocalidades()));

        frame.add(chart, BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);
        chart.createBufferStrategy(2);

    }
}
TOP

Related Classes of interfaz.utilidades.Pizarra

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.