User Tools

Site Tools


guides:overview:javacode

Sample Code - Java

Documentation | API Overview | Sample Code - Java

This is a code of sample application described in API Overview.

Code written in JAVA with use of NetBeans 8.1 IDE.

Working compiled version is available at: Full source and project of this is sample comes with Trial & Retail versions of TatukGIS Developer Kernel.

package apioverview;
 
import tatukgis.*;
 
public class APIOverview extends javax.swing.JFrame {
 
    public APIOverview() {
        initComponents();
    }
 
    @SuppressWarnings("unchecked")
    // start of autogenerated NetBeans code
    private void initComponents() {
 
        GIS = new tatukgis.jdk.TGIS_ViewerWnd();
        btnOpen = new javax.swing.JButton();
        btnCreateShapes = new javax.swing.JButton();
        btnFind = new javax.swing.JButton();
        btnZoom = new javax.swing.JButton();
        btnDrag = new javax.swing.JButton();
        btnSelect = new javax.swing.JButton();
 
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("DK.Java.Swing");
        setSize(new java.awt.Dimension(900, 600));
 
        GIS.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                GISMousePressed(evt);
            }
        });
 
        btnOpen.setText("Open project");
        btnOpen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOpenActionPerformed(evt);
            }
        });
 
        btnCreateShapes.setText("Create shape");
        btnCreateShapes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCreateShapesActionPerformed(evt);
            }
        });
 
        btnFind.setText("Find shapes in relation with created shape");
        btnFind.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFindActionPerformed(evt);
            }
        });
 
        btnZoom.setText("Zooming");
        btnZoom.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnZoomActionPerformed(evt);
            }
        });
 
        btnDrag.setText("Dragging");
        btnDrag.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDragActionPerformed(evt);
            }
        });
 
        btnSelect.setText("Selecting");
        btnSelect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSelectActionPerformed(evt);
            }
        });
 
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnOpen)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnZoom)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnDrag)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnSelect)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnCreateShapes)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnFind)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addComponent(GIS, javax.swing.GroupLayout.PREFERRED_SIZE, 901, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnOpen)
                    .addComponent(btnCreateShapes)
                    .addComponent(btnFind)
                    .addComponent(btnZoom)
                    .addComponent(btnDrag)
                    .addComponent(btnSelect))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(GIS, javax.swing.GroupLayout.DEFAULT_SIZE, 560, Short.MAX_VALUE))
        );
 
        pack();
    }                      
    // end of autogenerated NetBeans code
 
    private void btnOpenActionPerformed(java.awt.event.ActionEvent evt) {                                        
        //Open a project
        GIS.Open("..\\DATA\\world.shp");
        GIS.setMode(TGIS_ViewerMode.Select);
    }                                       
 
    private void GISMousePressed(java.awt.event.MouseEvent evt) {                                 
        TGIS_Shape shp;
        TGIS_Point ptg;
        TGIS_LayerVector lv;
        double precision;
 
        // ignore clicking if mode is other then select
        if(GIS.getMode() != TGIS_ViewerMode.Select) return ;
 
        // convert screen coordinates to map coordinates
        ptg = GIS.ScreenToMap(new TPoint(evt.getX(), evt.getY()));
 
        //get layer from the viewer
        lv = (TGIS_LayerVector)GIS.getItems().getItems(0);
 
        // calculate precision of location as 5 pixels
        precision = 5/GIS.getZoom() ;
 
        // let's try to locate a selected shape on the map
        shp = (TGIS_Shape) GIS.Locate(ptg, precision) ;
 
        // not found?
        if(shp == null) return ;
 
        //deselect selected shapes
        lv.DeselectAll();
 
        // mark shape as selected
        shp.setIsSelected(!shp.getIsSelected()) ;
 
        // and refresh a map
        GIS.InvalidateWholeMap() ;
    }                                
 
    private void btnCreateShapesActionPerformed(java.awt.event.ActionEvent evt) {                                                
        TGIS_LayerVector ll;
        TGIS_Shape shp;
 
        // lets find if such layer already exists
        ll = (TGIS_LayerVector)GIS.Get( "edit layer" ) ;
        if( ll != null ) return;
 
 
        // create a new layer and add it to the viewer
        ll = new TGIS_LayerVector() ;
        ll.setName( "edit layer" ) ;
        ll.setCS( GIS.getCS() ) ; // same coordinate system as a viewer
 
        // making inside of polygon transparent with blue border
        ll.getParams().getArea().setOutlineColor(TGIS_Color.getBlue());
        ll.getParams().getArea().setPattern(TGIS_BrushStyle.Clear);        
 
        GIS.Add( ll ) ;
 
 
        // create a shape and add it to polygon
        shp = ll.CreateShape( TGIS_ShapeType.Polygon ) ;
 
 
        // we group operation together 
        shp.Lock( TGIS_Lock.Extent ) ;
        shp.AddPart() ;
 
        // add some veritices
        shp.AddPoint( new TGIS_Point( 10, 10 ) );
        shp.AddPoint( new TGIS_Point( 10, 80 ) );
        shp.AddPoint( new TGIS_Point( 80, 90 ) );
        shp.AddPoint( new TGIS_Point( 90, 10 ) );
 
        // unlock operation, close polygon if necessary
        shp.Unlock() ; 
 
        // and now refresh map
        GIS.InvalidateWholeMap();
    }                                               
 
    private void btnFindActionPerformed(java.awt.event.ActionEvent evt) {                                        
        TGIS_LayerVector ll;
        TGIS_LayerVector lv;
        TGIS_Shape selShp, tmpShp;
 
        //get layer from the viewer
        ll = (TGIS_LayerVector) GIS.Get( "edit layer" );
        if (ll == null) return ;
 
        // lets get a layer with world shape
        // names are constructed based on layer name
        lv = (TGIS_LayerVector)GIS.Get("world");
 
        //deselect selected shapes
        lv.DeselectAll();
 
        // and we need a created shape, with we want   
        // to use as selection shape
        selShp = ll.GetShape(1) ;  // just a first shape form the layer
 
        // for file based layer we should pin shape to memory
        // otherwise it should be discarded 
        selShp = selShp.MakeEditable();
 
        // Group all future screen updates into one drawing operatiiom
        GIS.Lock();
 
        // so now we search for all shapes with DE9-IM relationship
        // which labels starts with 's' (with use of SQL syntax)
        // in this case we find “T*****FF*” contains relationship
        // which means that we will find only shapes inside the polygon
        for( Object tmp : lv.Loop( selShp.getExtent(), "label LIKE 's%'", selShp, "T*****FF*" )){
            tmpShp = (TGIS_Shape)tmp ;  
            tmpShp.setIsSelected(true) ;
          }
 
        //unlock updating of the viewer
        GIS.Unlock();
 
        // and now refresh map
        GIS.InvalidateWholeMap();
    }                                       
 
    private void btnZoomActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // if the viewer is empty then do nothing
        if(GIS.getIsEmpty()) return;
 
        // if the viewer is not empty set it mode to Zoom mode
        GIS.setMode(TGIS_ViewerMode.Zoom);
    }                                       
 
    private void btnDragActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // if the viewer is empty then do nothing
        if(GIS.getIsEmpty()) return;
 
        // if the viewer is not empty set it mode to Drag mode
        GIS.setMode(TGIS_ViewerMode.Drag);
    }    
 
    private void btnSelectActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // if the viewer is empty then do nothing
        if(GIS.getIsEmpty()) return;
 
        // if the viewer is not empty set it mode to Select mode
        GIS.setMode(TGIS_ViewerMode.Select);
    }                                         
 
    public static void main(String args[]) {
 
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new APIOverview().setVisible(true);
            }
        });
    }
 
    private tatukgis.jdk.TGIS_ViewerWnd GIS;
    private javax.swing.JButton btnCreateShapes;
    private javax.swing.JButton btnDrag;
    private javax.swing.JButton btnFind;
    private javax.swing.JButton btnOpen;
    private javax.swing.JButton btnSelect;
    private javax.swing.JButton btnZoom;
}
2017/03/03 12:41