SuperMap iObjects Java 11i 知识库
地图排版打印  

7     使用组件产品如何实现

7.1     示范程序介绍

根据第6章介绍的地图打印配置流程,我们了解了利用SuperMap组件产品实现地图排版打印的基本步骤。下面就给出使用SuperMap组件产品实现地图排版打印的示范程序。

该示范程序实现的功能如下:

1.     布局操作:保存布局、加载布局、保存布局为模板、加载布局模板、清空布局窗口;

2.     绘制布局元素:添加带复杂边框的地图对象、添加地图标题、添加方向标、添加比例尺、添加图例;

3.     编辑功能:添加点状元素、添加线状元素及面状填充;

4.     输出为图片:将布局导出为PDFEMFEPSBMPJPGPNG格式;

5.     布局浏览:布局的放大、缩小、平移、全幅显示,以及锁定地图;

6.     布局预览及布局打印。

7.     该示范程序,所使用的示范数据路径为:SuperMap组件产品安装目录\SampleData\China

7.2     初始化相关变量并打开布局窗口

初始化窗体,添加布局窗口,并连接工作空间,效果如图 7‑1所示:

    private void initialize() {

           try {

 

                  // 打开工作空间

                  WorkspaceConnectionInfo conInfo = new WorkspaceConnectionInfo(

                                "../../SampleData/China/China400.smwu");

                  conInfo.setType(WorkspaceType.SMWU);

                  m_workspace.open(conInfo);

                  m_mapLayoutControl.getMapLayout().setWorkspace(m_workspace);

 

                  // 打开布局窗口水平、垂直滚动条

                  m_mapLayoutControl.setVerticalScrollbarVisible(true);

                  m_mapLayoutControl.setHorizontalScrollbarVisible(true);

 

           } catch (Exception ex) {

                  System.out.println(ex.getMessage());

           }

    }

 

71 界面初始化示意图

7.3     布局操作相关功能实现

对布局的保存、加载、清空以及将布局保存为模板、加载布局模板等功能在“布局操作”菜单以及“清空布局窗口”按钮中,如图 7‑2所示:

72 示范程序的“布局操作”相关菜单

保存布局:

    public void saveLayout() {

           //获取可用的布局名称、布局的 XML 描述信息

           String layoutname = m_workspace.getLayouts().getAvailableLayoutName("layout");

           String xml = m_mapLayoutControl.getMapLayout().toXML();

          

        //将布局添加到布局集合中,并保存工作空间

           m_workspace.getLayouts().add(layoutname, xml);

           m_workspace.save();

    }

加载布局,由于示范数据中本身并无布局,因此该功能仅在“保存布局”后可用:

    public void loadLayout() {

           Layouts layouts = m_workspace.getLayouts();

           String layoutName = layouts.get(0);

 

           MapLayout mapLayout = m_mapLayoutControl.getMapLayout();

           mapLayout.setWorkspace(m_workspace);

           mapLayout.open(layoutName);

    }

布局保存为模板

    public void saveAsTemplate() {

           String layoutTemplateName = "../../SampleData/China/template.ult";            m_mapLayoutControl.getMapLayout().saveAsTemplate(layoutTemplateName);

    }

加载布局模板,由于示范数据中本身并无布局模板,因此该功能仅在“保存为模板”后可用:

    public void loadTemplate() {

           String layoutTemplateName = "../../SampleData/China/template.ult";

           m_mapLayoutControl.getMapLayout().loadTemplate(layoutTemplateName);

    }

清空布局窗口,布局清空后,应将锁定地图、打印预览设置为非选中状态:

    public void clearLayout() {

           m_mapLayoutControl.getMapLayout().getElements().deleteAll();

           m_mapLayoutControl.getMapLayout().zoomToPaper();

           m_mapLayoutControl.getMapLayout().refresh();

    }

7.4     布局元素相关功能实现

添加各种布局元素的功能集成在“绘制布局元素”菜单中,如图 7‑3所示:

73 示范程序“绘制布局元素”菜单

添加带复杂边框地图对象:

    public void addGeoMap() {

           LayoutElements elements = m_mapLayoutControl.getMapLayout().getElements();

           GeoMap geoMap = new GeoMap();

           geoMap.setMapName("2000年各省人口数分段专题图");

 

           // 设置Geomap对象的外切矩形

           Rectangle2D rect = new Rectangle2D(new Point2D(850, 1250), new Size2D(1500, 1500));

           GeoRectangle geoRect = new GeoRectangle(rect, 0); geoMap.setShape(geoRect);

 

           // 设置Geomap对象的边框

           GeoMapBorder geoMapBorder = new GeoMapBorder();

           geoMapBorder.setBorderType(GeoMapBorderType.COMPLEX);

 

           //内框

           geoMapBorder.setInFrameColor(java.awt.Color.WHITE);

           geoMapBorder.setInFrameWidth(0.5);

           geoMapBorder.setInFrameInterval(2);

 

           // 内线

           geoMapBorder.setInLineColor(new Color(115, 115, 115));

           geoMapBorder.setInLineWidth(0.5);

           geoMapBorder.setInLineInterval(5);

 

           // 外线

           geoMapBorder.setOutLineColor(java.awt.Color.WHITE);

           geoMapBorder.setOutLineWidth(0.5);

           geoMapBorder.setOutLineInterval(30);

 

           // 外框

           geoMapBorder.setOutFrameColor(new Color(115, 115, 115));

           geoMapBorder.setOutFrameWidth(1);

           geoMapBorder.setOutFrameInterval(2);

 

           // 设置填充文本的风格

           TextStyle fillTextStyle = new TextStyle();

           fillTextStyle.setShadow(true);

           fillTextStyle.setAlignment(TextAlignment.TOPCENTER);

           fillTextStyle.setBackColor(java.awt.Color.WHITE);

           fillTextStyle.setForeColor(new Color(135, 171, 217));

           fillTextStyle.setBackOpaque(true);

           fillTextStyle.setBold(false);

           fillTextStyle.setFontName("宋体");

           fillTextStyle.setFontHeight(10.0);

           fillTextStyle.setFontWidth(10.0);

           fillTextStyle.setSizeFixed(false);

           fillTextStyle.setWeight(100);

          

           // 设置填充文本

           geoMapBorder.setFillType(GeoMapBorderFillType.TEXT);

           geoMapBorder.setFillDirection(FillDirectionType.INNER);

           geoMapBorder.setFillText(" * ");

           geoMapBorder.setFillTextStyle(fillTextStyle);            

 

           // 设置转角图片

           geoMapBorder.setCornerFillType(GeoMapBorderFillType.IMAGE);

           geoMapBorder.setCornerFillImageFile("../../SampleData/China/WeatherReport/a18.bmp");

           geoMapBorder.setCornerFillStartMode(CornerFillStartMode.LEFTBOTTOM);

 

           geoMap.setMapBorder(geoMapBorder);

           geoMap.setBorderVisible(true);

           elements.addNew(geoMap);

    }

添加标题:

    public void addTitle() {

           TextStyle textStyle = new TextStyle();

           textStyle.setShadow(true);

           textStyle.setAlignment(TextAlignment.TOPCENTER);

           textStyle.setBackColor(new Color(65, 65, 65));

           textStyle.setForeColor(new Color(174, 241, 176));

           textStyle.setBackOpaque(false);

           textStyle.setBold(true);

           textStyle.setFontName("楷体");

           textStyle.setFontHeight(50.0);

           textStyle.setFontWidth(30.0);

           textStyle.setSizeFixed(false);

           textStyle.setItalic(true);

           textStyle.setOutline(true);

           textStyle.setWeight(500);

 

           TextPart textPart = new TextPart("2000年各省人口数分段专题图", new Point2D(850, 2150),0);

 

           GeoText geoText = new GeoText(textPart, textStyle);

           m_mapLayoutControl.getMapLayout().getElements().addNew(geoText);

    }

在布局窗口中,由于添加地图比例尺、地图图例、地图指南针以及锁定地图操作均是针对当前选中的某一地图而言,因此在实现这些功能时,需要首先返回当前被选中的地图的ID

返回选中地图对象的ID,用于返回锁定地图、添加图例、比例尺、指南针等功能所对应的地图对象:

    public int getMapID() {

           int mapID = -1;

           int count = 0;

           LayoutSelection selection = m_mapLayoutControl.getMapLayout().getSelection();

           LayoutElements elements = m_mapLayoutControl.getMapLayout().getElements();

           elements.refresh();

 

           for (int i = 0; i < selection.getCount(); i++) {

                  int id = selection.get(i);

                  elements.seekID(id);

                  Geometry geometry = elements.getGeometry();

                  if (geometry.getType() == GeometryType.GEOMAP) {

                         mapID = id;

                         count++;

                  }

 

                  if (count > 1) {

                         mapID = -1;

                  }

           }

           return mapID;

    }

添加指南针,该功能仅在某一地图对象处于选中状态才可用:

    public void addNorthArrow() {

           Point2D northArrowCenter = new Point2D(200, 1900);

           Size2D northArrowSize = new Size2D(150, 150);

 

           Rectangle2D northArrowBounds = new Rectangle2D(northArrowCenter,northArrowSize);

           GeoNorthArrow northArrow = new GeoNorthArrow(

                         NorthArrowStyleType.FOURDIRECTION, northArrowBounds, 0);

 

           northArrow.setBindingGeoMapID(getMapID());

           m_mapLayoutControl.getMapLayout().getElements().addNew(northArrow);

    }

添加比例尺,该功能仅在某一地图对象处于选中状态才可用:

    public void addScale() {

           GeoMapScale scale = new GeoMapScale(getMapID(), new Point2D(950, 515), 550, 18);

           scale.setScaleUnit(Unit.KILOMETER);

           scale.setLeftDivisionCount(1);

           scale.setSegmentCount(2);         

           scale.setScaleType(GeoMapScaleType.RAILWAY);

 

           m_mapLayoutControl.getMapLayout().getElements().addNew(scale);

    }

添加图例,该功能仅在某一地图对象处于选中状态才可用:

    public void addGeoLegend() {

           LayoutElements elements = m_mapLayoutControl.getMapLayout().getElements();

           elements.seekID(getMapID());

           GeoMap geoMap = (GeoMap) elements.getGeometry();

 

           GeoLegend geoLegend = new GeoLegend(geoMap.getMapName(), m_workspace);

           geoLegend.setHeight(175);

           geoLegend.setWidth(750);

           geoLegend.setCenter(new Point2D(475, 590));

 

           GeoStyle legendStyle = new GeoStyle();

           legendStyle.setFillForeColor(new Color(255, 235, 175));

           legendStyle.setFillOpaqueRate(30);

           legendStyle.setLineWidth(0.5);

           legendStyle.setLineColor(new Color(65,65,65));

           geoLegend.setBackGroundStyle(legendStyle);

           geoLegend.setColumnCount(3);

 

           //设置图例项和图例子项的说明文本的风格

           TextStyle legendTextStyle = new TextStyle();

           legendTextStyle.setBackColor(java.awt.Color.YELLOW);

           legendTextStyle.setForeColor(java.awt.Color.BLUE);

           legendTextStyle.setFontName("宋体");

           legendTextStyle.setFontHeight(20.0);

           legendTextStyle.setFontWidth(12.0);

           legendTextStyle.setSizeFixed(false);

 

           geoLegend.setItemTextStyle(legendTextStyle);

           geoLegend.setSubItemTextStyle(legendTextStyle);

 

           //设置图例标题风格

           TextStyle titleTextStyle = new TextStyle();

           titleTextStyle.setBackColor(java.awt.Color.YELLOW);

           titleTextStyle.setForeColor(java.awt.Color.BLUE);

           titleTextStyle.setFontName("宋体");

           titleTextStyle.setFontHeight(40.0);

           titleTextStyle.setFontWidth(25.0);

           titleTextStyle.setItalic(true);

           titleTextStyle.setBold(true);

           titleTextStyle.setSizeFixed(false);

           titleTextStyle.setWeight(500);

          

           geoLegend.setTitle("图例");

           geoLegend.setTitleStyle(titleTextStyle);

           //将图例添加到布局图层,而非屏幕图层

           geoLegend.load(false);

 

           m_mapLayoutControl.getMapLayout().getElements().addNew(geoLegend);

    }

通过上述功能,实现了地图对象、比例尺、图例、指南针、地图标题的添加,效果如图 7‑4所示:

74 布局排版效果图

7.5     布局编辑相关功能实现

添加点状符号、线状符号、面状填充等功能集成在“编辑”菜单中,如图 7‑5所示:

75 示范程序“编辑”菜单

点状符号:

    public void createPoint() {

           m_mapLayoutControl.setLayoutAction(Action.CREATEPOINT);

    }

线状符号:

    public void createLine() {

           m_mapLayoutControl.setLayoutAction(Action.CREATEPOLYLINE);

    }

面状填充:

    public void createPolygon() {

           m_mapLayoutControl.setLayoutAction(Action.CREATEPOLYGON);

    }

7.6     输出为图片相关功能实现

输出为PDF等文件相关功能都集成在"输出为图片"菜单中,如图 7‑6所示:

76 示范程序“输出为图片”菜单

输出为PDF

    public void exportToPDF() {

           // 设置PDF文件输出选项

           PDFOptions pdfOptions = new PDFOptions();

           pdfOptions.setEntire(true);

           pdfOptions.setLineStyleRetained(true);

           pdfOptions.setPointStyleRetained(true);

           pdfOptions.setRegionStyleRetained(true);

           pdfOptions.setVector(true);

 

           // 输出布局为PDF文件

           JFileChooser m_fileChooser = new JFileChooser();

           FileNameExtensionFilter filter = new FileNameExtensionFilter(

                         "导出为PDF(*.pdf)", "pdf");

           m_fileChooser.addChoosableFileFilter(filter);

 

           m_fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);

 

           int value = m_fileChooser.showSaveDialog(null);

           if (value == 1) {

                  return;

           }

           File selectedFile = m_fileChooser.getSelectedFile();

           if (selectedFile == null) {

                  return;

           }

           String filePath = selectedFile.getPath();

           m_mapLayoutControl.getMapLayout().outputLayoutToPDF(filePath,

                         pdfOptions);

    }

输出为EMF

    public void exportToEMF() {       

           String emfName = "../../SampleData/China/maplayout.emf";

           m_mapLayoutControl.getMapLayout().outputLayoutToEMF(emfName);

    }

输出为EPS

    public void exportToEPS() {

           String epsName = "../../SampleData/China/maplayout.eps";

           m_mapLayoutControl.getMapLayout().outputLayoutToEPS(epsName);

    }

输出为PNG

    public void exportToPNG() {

           String pngName = "../../SampleData/China/maplayout.png";

           m_mapLayoutControl.getMapLayout().outputLayoutToPNG(pngName, true);

    }

输出为BMP

    public void exportToBMP() {

           int bmpDPI = 500;

           String bmpName = "../../SampleData/China/maplayout.bmp";

           m_mapLayoutControl.getMapLayout().outputLayoutToBMP(bmpName, bmpDPI);

    }

输出为JPG

    public void exportToJPG() {

           String jpgName = "../../SampleData/China/maplayout.jpg";

           m_mapLayoutControl.getMapLayout().outputLayoutToJPG(jpgName);

    }

7.7     布局浏览相关功能及实现

锁定地图等布局浏览功能在菜单中的位置,如图 7‑7所示:

77 示范程序布局浏览功能

由于放大、缩小、平移、全幅显示功能,在是否“锁定地图”时,作用对象不同,当地图没有锁定时,作用对象为整个布局,当地图锁定时,作用对象为锁定的地图。此外,“锁定地图”功能,需在某一地图处于选中状态才可用。

锁定地图,该功能仅在某一地图对象处于选中状态才可用,代码实现如下:

    public void lockMap(boolean isLock) {

           int mapID = -1;

        //isLock 表示地图是否锁定

           if (isLock) {

                  mapID = getMapID();

           }

           m_mapLayoutControl.setActiveGeoMapID(mapID);

    }

放大功能实现代码:

    public void zoomin(boolean isLock) {

        //isLock 表示地图是否锁定

           if (isLock) {

                  m_mapLayoutControl.setMapAction(Action.ZOOMIN);

           } else {

                  m_mapLayoutControl.setLayoutAction(Action.ZOOMIN);

           }

    }

缩小功能实现代码:

    public void zoomout(boolean isLock) {

        //isLock 表示地图是否锁定

           if (isLock) {

                  m_mapLayoutControl.setMapAction(Action.ZOOMOUT);

           } else {

                  m_mapLayoutControl.setLayoutAction(Action.ZOOMOUT);

           }

    }

平移功能实现代码:

    public void pan(boolean isLock) {

           //isLock 表示地图是否锁定

           if (isLock) {

                  m_mapLayoutControl.setMapAction(Action.PAN);

           } else {

                  m_mapLayoutControl.setLayoutAction(Action.PAN);

           }

    }

全幅显示功能实现代码:

    public void viewEntire(boolean isLock) {

        //isLock 表示地图是否锁定

           if (isLock) {

                  m_mapLayoutControl.getActiveMap().viewEntire();

                  m_mapLayoutControl.getMapLayout().refresh();

           } else {

                  m_mapLayoutControl.getMapLayout().zoomToPaper();

           }

    }

7.8     打印相关功能及实现

布局预览及布局打印功能在菜单中的位置,如图 7‑8所示:

78 示范程序布局打印功能

打印预览功能实现代码:

    m_PrintPreviewCheckBox.addChangeListener(new javax.swing.event.ChangeListener() {

           public void stateChanged(javax.swing.event.ChangeEvent e) {

                  boolean printPreview = m_PrintPreviewCheckBox.isSelected();

                  if (printPreview) {

                         m_mapLayoutControl.getMapLayout().setPrintPreview(true);

                         m_mapLayoutControl.getMapLayout().refresh();

                  } else {

                         m_mapLayoutControl.getMapLayout().setPrintPreview(false);

                         m_mapLayoutControl.getMapLayout().refresh();

                  }

           }

    });

打印布局功能实现代码:

    public void printLayout() {

           Printer printer = m_mapLayoutControl.getMapLayout().getPrinter();

           printer.setPaperSize(PaperSize.A4);

           printer.setOrientation(PaperOrientation.LANDSCAPE);

 

           // 设置纸面的页边距

           PaperMargin paperMargin = new PaperMargin(70);

           paperMargin.setLeft(100);

           paperMargin.setRight(100);

           printer.setMargin(paperMargin);

 

           // 打印机设置

           printer.setDeviceDPI(72);

           printer.setVectorPrint(true);

           printer.setPrinterName(打印机名称);

 

           // 打印布局

           if (printer.isValidPrinter()) {

                  JOptionPane.showMessageDialog(null, "可以打印");

           } else {

                  JOptionPane

                                .showMessageDialog(null, "打印机不合法!");

           }

    }

 


  版权所有© 北京超图软件股份有限公司。保留所有权利。