- 积分
- 229
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2013-10-17
- 最后登录
- 1970-1-1
|
登录后查看更多精彩内容~
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
本帖最后由 ycjtj1dx202ly 于 2013-10-28 22:07 编辑
此次教程是实现在MapLayout上添加中国南海脚图。首先要添加一个MapFrame,并且将它添加到LayersLegend中,接下来为MapFrame添加一个图层,MapLayout中会自动添加一个LayoutMap,最后设置相应的参数将MapView缩放到南海范围。我们先在public FrmMain下添加函数
- this.addMapFrame_ChinaSouthSea();
添加MapFrame及设置相应参数,代码如下:
- private void addMapFrame_ChinaSouthSea() {
- //Add an empty map frame
- MapFrame aMF = new MapFrame();
- aMF.setText("China South Sea");
- this.layersLegend1.addMapFrame(aMF);
- //Add a layer to this map frame
- try {
- String mapDir = "E:\\MeteoInfo\\map\\";
- String aFile = mapDir + "country1.shp";
- MapLayer aLayer = MapDataManage.loadLayer(aFile); aLayer.getLegendScheme().getLegendBreaks().get(0).setColor(new Color(232, 232, 232));
- aMF.addLayer(aLayer);
- //Set layout map property
- LayoutMap aLM = this.mapLayout1.getLayoutMaps().get(1);
- aMF.setDrawGridLabel(false);
- aLM.setLeft(550);
- aLM.setTop(330);
- aLM.setWidth(80);
- aLM.setHeight(100);
- aMF.getMapView().zoomToExtent(105, 120, 0, 20);
- this.layersLegend1.repaint();
- } catch (IOException ex) {
- Logger.getLogger(FrmMain.class.getName()).log(Level.SEVERE, null, ex);
- } catch (Exception ex) {
- Logger.getLogger(FrmMain.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
接下来给MapLayout添加一个文本
首先同样在public FrmMain中添加一个函数
- this.AddTitle();
addTitle函数代码如下:
- private void AddTitle() {this.mapLayo .addText("MeteoInfo Class Library Demo", mapLayout1.getWidth() / 2, 20, 16);
- }
给菜单File添加一个子菜单ExportFigure,并在其Click事件中加入下列代码,实现图像保存为图形文件的功能。
- private void jMenuItem_ExportFigureActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- JFileChooser aDlg = new JFileChooser();
- String[] fileExts = new String[]{"png"};
- GenericFileFilter mapFileFilter = new GenericFileFilter(fileExts, "Png Image (*.png)");
- aDlg.setFileFilter(mapFileFilter);
- fileExts = new String[]{"gif"};
- mapFileFilter = new GenericFileFilter(fileExts, "Gif Image (*.gif)");
- aDlg.addChoosableFileFilter(mapFileFilter);
- fileExts = new String[]{"jpg"};
- mapFileFilter = new GenericFileFilter(fileExts, "Jpeg Image (*.jpg)");
- aDlg.addChoosableFileFilter(mapFileFilter);
- aDlg.setAcceptAllFileFilterUsed(false);
- if (JFileChooser.APPROVE_OPTION == aDlg.showSaveDialog(this)) {
- File aFile = aDlg.getSelectedFile();
- System.setProperty("User.dir", aFile.getParent());
- String extent = ((GenericFileFilter) aDlg.getFileFilter()).getFileExtent();
- String fileName = aFile.getAbsolutePath();
- if (!fileName.substring(fileName.length() - extent.length()).equals(extent)) {
- fileName = fileName + "." + extent;
- }
- if (this.jTabbedPane1.getSelectedIndex() == 0) {
- try {
- this.layersLegend1.getActiveMapFrame().getMapView().exportToPicture(fileName);
- } catch (FileNotFoundException ex) {
- Logger.getLogger(FrmMain.class.getName()).log(Level.SEVERE, null, ex);
- } catch (PrintException ex) {
- Logger.getLogger(FrmMain.class.getName()).log(Level.SEVERE, null, ex);
- } catch (IOException ex) {
- Logger.getLogger(FrmMain.class.getName()).log(Level.SEVERE, null, ex);
- }
- } else if (this.jTabbedPane1.getSelectedIndex() == 1) {
- try {
- this.layersLegend1.getMapLayout().exportToPicture(fileName);
- } catch (FileNotFoundException ex) {
- Logger.getLogger(FrmMain.class.getName()).log(Level.SEVERE, null, ex);
- } catch (PrintException ex) {
- Logger.getLogger(FrmMain.class.getName()).log(Level.SEVERE, null, ex);
- } catch (IOException ex) {
- Logger.getLogger(FrmMain.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
- }
调试结果如下:
|
|