java graphic2D 矩形文字居中

要實現在地圖上畫N個方格,有文字並居中,顏色填充根據級別不一致,好久都沒有做出來,請教了老大,終於搞定了,不多說,上代碼:

/*
* 輸出預報格網圖
* width爲圖片寬度,height爲輸出圖片高度,parameter爲參數列表
* @see com.ffds.dispatching.situationAnalysis.service.SituationAnalysisService#getForecastGrid(int, int, java.util.HashMap)
*/
public BufferedImage getForecastGrid(int width,int height,HashMap<String,Object> parameters){
//設定底圖初始範圍
double minX =  MapUtil.convertMercatorX(1.257110322559409E7);
double minY =  MapUtil.convertMercatorY(2807811.819841969);
double maxX =  MapUtil.convertMercatorX(1.3293222258698823E7);
double maxY =  MapUtil.convertMercatorY(3529930.8529467015);
//定義返回值
BufferedImage forecastGrid = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = forecastGrid.createGraphics();
forecastGrid = g2d.getDeviceConfiguration().createCompatibleImage(width,height, Transparency.TRANSLUCENT);
g2d = forecastGrid.createGraphics();

List<HashMap<String,Object>> list = situatioinAnalysisDao.getForecastGrid(parameters);

double x = 0;
double y = 0;
double gridcellwidth = 0.277243;
double gridcellheight = 0.225;
int[] rw = MapUtil.getXYScreen(Double.valueOf(list.get(0).get("LONG_D").toString())-gridcellwidth,Double.valueOf(list.get(0).get("LAT_D").toString())+gridcellheight, maxX, maxY, minX, minY, width,height);
int[] rb = MapUtil.getXYScreen(Double.valueOf(list.get(0).get("LONG_D").toString())+gridcellwidth,Double.valueOf(list.get(0).get("LAT_D").toString())-gridcellheight, maxX, maxY, minX, minY, width,height);
int recWidth = rb[0]-rw[0];
int recHeight = -rw[1]+rb[1];
FontMetrics fm = null;
int stringWidth=0;
int stringAscent = 0;
int stringDescent = 0;
for (int i = 0; i < list.size(); i++) {
x = Double.valueOf(list.get(i).get("LONG_D").toString());
y = Double.valueOf(list.get(i).get("LAT_D").toString());
int[] screenXY = MapUtil.getXYScreen(x-gridcellwidth,y+gridcellheight, maxX, maxY, minX, minY, width,height);
//繪製邊框
Rectangle2D r = new Rectangle2D.Double(screenXY[0],screenXY[1],recWidth,recHeight);
g2d.setColor(Color.BLACK);
g2d.draw(r);

//繪製文字,填充顏色
if(list.get(i).get("RN").equals("")){
g2d.drawString("0", screenXY[0]+recWidth/2, screenXY[1]+recHeight/2);
}else{
double rn = Double.valueOf(list.get(i).get("RN").toString());
Color color = null;
if (rn>0 && rn<5) {
color = new Color(169,237,150);
} else if(rn > 5 && rn <= 10){
color = new Color(58,188,52);
}else if(rn > 10 && rn <= 20){
color = new Color(65,161,212);
}else if(rn > 20 && rn <= 30){
color = new Color(57,189,221);
}else if(rn > 30 && rn <= 50){
color = new Color(6,0,254);
}else{
color = new Color(255,255,255);
}

g2d.setColor(color);
AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f);
        g2d.setComposite(ac);
g2d.fillRect(screenXY[0]+1,screenXY[1]+1,recWidth-2,recHeight-2);
g2d.setColor(Color.BLACK);
fm = g2d.getFontMetrics();
stringWidth = fm.stringWidth(String.valueOf(rn)); 
stringAscent = fm.getAscent(); 
stringDescent = fm.getDescent (); 
g2d.drawString(String.valueOf(rn), screenXY[0]+recWidth/2-stringWidth / 2, screenXY[1]+recHeight/2+ (stringAscent - stringDescent) / 2);
}
}
return forecastGrid;
}

效果圖如下:



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章