?? labelclass.java~86~
字號:
package myfirstmap;
import com.mapinfo.mapj.MapJ;
import com.mapinfo.unit.LinearUnit;
import com.mapinfo.util.DoublePoint;
import com.mapinfo.util.DoubleRect;
import com.mapinfo.mapj.Layers;
import com.mapinfo.mapj.DataLayer;
import com.mapinfo.mapj.FeatureLayer;
import com.mapinfo.mapdefcontainer.MapDefContainer;
import com.mapinfo.mapdefcontainer.FileMapDefContainer;
import com.mapinfo.mapxtreme.client.MapXtremeImageRenderer;
import com.mapinfo.xmlprot.mxtj.ImageRequestComposer;
import java.awt.Color;
import com.mapinfo.mapj.LabelProperties;
import com.mapinfo.graphics.Rendition;
import com.mapinfo.graphics.RenditionImpl;
import com.mapinfo.labeltheme.OverrideLabelTheme;
import com.mapinfo.mapj.BaseLabelProperties;
import com.mapinfo.unit.Distance;
public class LabelClass
{
private String mdfFilePath="e:\\MapInfo\\study\\mapmdf";
private String mdfFileName="states.mdf";
private String mapxtremeURL="http://901-32:8083/mapxtreme47/mapxtreme";
private MapDefContainer mapDefContainer;
public LabelClass()
{
}
public void studyLabelProperty()
{
MapJ mapj=new MapJ();
DoublePoint centerPoint=new DoublePoint(-108.8,42.5);
LinearUnit unit=LinearUnit.kilometer ;
mapj.setDistanceUnits(unit) ;
mapj.setDeviceBounds(new DoubleRect(0,0,800,600)) ;
Layers layers=null;
DataLayer dataLayer=null;
DoubleRect rect;
FeatureLayer featureLayer=null;
String layerName;
LabelProperties labelProperties;
//DoubleRect bound=new DoubleRect(centerPoint,
try
{
mapj.setCenter(centerPoint);
// mapDefContainer=new FileMapDefContainer(mdfFilePath);
// mapj.loadMapDefinition(mapDefContainer,mdfFileName);
mapj.loadMapDefinition(mdfFilePath+"\\"+mdfFileName) ;
layers=mapj.getLayers() ;
dataLayer=(DataLayer)layers.get(0) ;
rect=dataLayer.getBounds() ;
double width=(rect.xmax -rect.xmin )/8;
double height=(rect.ymax -rect.ymin )/8;
mapj.setBounds(new DoubleRect(centerPoint,width,height)) ;
//
featureLayer=(FeatureLayer)mapj.getLayers() .get(0) ;
layerName=featureLayer.getName() ;
// labelProperties=new LabelProperties();
labelProperties=featureLayer.getLabelProperties() ;
//set the label offset and alignments to their default position
labelProperties.setOffset(new DoublePoint(1d,1d) ) ;//不用(0,0)可以讓標簽在區域里面,當然最好的辦法是
//求出每個區域的中心,然后用它作為偏移值,不過再做此之前還應該多一道判斷手續,那就是該幾何體是不是區域
labelProperties.setHorizontalAlignment(LabelProperties.HORIZ_ALIGN_DEFAULT ) ;
labelProperties.setVerticalAlignment(LabelProperties.VERT_ALIGN_DEFAULT ) ;
// Turn the multiline option on and set the columns to be used for labeling.
labelProperties.setMultiLineTextMode(LabelProperties.MULTILINE_TEXT_ON ) ;
//labelProperties.setLabelExpression("\"NAME:[\"+State_Name+\"]\\nABBREV:[\"+State+\"]\"");//("\"name:[\"+State_Name+\"]\nabbrev:[\"+State+\"]") ;
labelProperties.setLabelExpression("\"name:[\"+State+\"]") ;
//use geometory compute mode
labelProperties.setGeometryCalculationMode(LabelProperties.GEOMETRY_COMPUTED );//這條語句在這兒設置沒有用,我不知道為什么
//// Set LabelFollowingPath to true to enable label splining
boolean bFollowPath=true;
// labelProperties.setLabelFollowingPath(bFollowPath);//我不知道這條語句有什么用
boolean bfollow=labelProperties.isLabelFollowingPath() ;
// Set a Rendition for the Labels and create an OverrideLabelTheme in
// order to display the options set.
Rendition rend=new RenditionImpl();
/* rend.setValue(Rendition.FONT_FAMILY ,"Arial");
rend.setValue(Rendition.FONT_WEIGHT ,1);
Distance distance=new Distance(60, LinearUnit.kilometer );
rend.setValue(Rendition.FONT_SIZE ,distance);
rend.setValue(Rendition.FONT_STYLE ,Rendition.FontStyle.ITALIC );
rend.setValue(Rendition.SYMBOL_FOREGROUND ,Color.red );
rend.setValue(Rendition.FILTER_EFFECTS ,Rendition.FilterEffects .HALO );
*/
rend.setValue(Rendition.SYMBOL_MODE ,Rendition.SymbolMode .FONT );
rend.setValue(Rendition.FONT_FAMILY ,"Mapinfo Cartographic") ;
rend.setValue(Rendition.FONT_SIZE ,16);
rend.setValue(Rendition.SYMBOL_STRING ,String.valueOf((char)33)) ;
labelProperties.setRendition(rend) ;
// OverrideLabelTheme overLabelTheme =new OverrideLabelTheme(labelProperties,"Theme Name");//1
// featureLayer.getLabelThemeList() .add(overLabelTheme) ;//2
// Get the States Layer, set its LabelProperties object, and turn labeling on.
//我覺得這下面的兩條語句沒有必要要,因為labelProperties已經添加到LabelThemeList中去了,我試驗了一下
//用它們其中一個就可以了,即要么將labelProperties添加到LabelThemeList中去(用1,2),要么用featureLayer來直接設置
//該標簽屬性(用3,4)
BaseLabelProperties baseLabel=new BaseLabelProperties(labelProperties);//3
Distance maxDist=new Distance(100,LinearUnit.kilometer );
Distance minDist=new Distance(70,LinearUnit.kilometer );
baseLabel.setZoomMax(maxDist) ;
baseLabel.setZoomMin(minDist) ;
// featureLayer.setLabelProperties(baseLabel) ;//4
//下面的語句是另外一種標簽設置方式,它首先通過getLabelProperties()方法得到該層的標簽屬性的引用(不是拷貝)
//這樣后面的設置就直接反映到標簽屬性里去了,沒有必要再用 featureLayer.setLabelProperties(baseLabel) 這樣的語句了
/* LabelProperties labelProperties2=featureLayer.getLabelProperties() ;
Rendition rend2=new RenditionImpl();
rend2.setValue(Rendition.FONT_STYLE ,Rendition.FontStyle.NORMAL );
rend2.setValue(Rendition.FONT_FAMILY ,"Arial");
rend2.setValue(Rendition.FONT_WEIGHT ,1);
rend2.setValue(Rendition.FONT_SIZE ,30);
labelProperties2.setRendition(rend2) ;*/
//
featureLayer.setAutoLabel(true) ;//這條語句必須要
//
MapXtremeImageRenderer render=new MapXtremeImageRenderer(mapxtremeURL);
ImageRequestComposer imageRC=ImageRequestComposer.create(mapj,256,Color.white,"image/png");
render.render(imageRC) ;
render.toFile("E:\\MapInfo\\study\\png\\labelProperty4.png") ;
render.dispose() ;
}catch(Exception e)
{
System.out.println("get a "+e.getClass() +"\nwith message "+e.getMessage() ) ;
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -