javagraphic2D矩形⽂字居中
要实现在地图上画N个⽅格,有⽂字并居中,颜⾊填充根据级别不⼀致,好久都没有做出来,请教了⽼⼤,终于搞定了,不多说,上代码:
/*
* 输出预报格⽹图
* width为图⽚宽度,height为输出图⽚⾼度,parameter为参数列表
* @e com.ffds.dispatching.situationAnalysis.rvice.SituationAnalysisService#getForecastGrid(int, int, java.util.HashMap)
*/
public BufferedImage getForecastGrid(int width,int height,HashMap<String,Object> parameters){
//设定底图初始范围
新手上路驾驶技巧double minX = vertMercatorX(1.257110322559409E7);
double minY = vertMercatorY(2807811.819841969);
double maxX = vertMercatorX(1.3293222258698823E7);
北京鼎石国际学校double maxY = vertMercatorY(3529930.852*******);
//定义返回值
BufferedImage forecastGrid = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = ateGraphics();
forecastGrid = DeviceConfiguration().createCompatibleImage(width,height, Transparency.TRANSLUCENT);
g2d = ateGraphics();
List<HashMap<String,Object>> list = ForecastGrid(parameters);
pulloff
double x = 0;
fuchs
double y = 0;
double gridcellwidth = 0.277243;
double gridcellheight = 0.225;
int[] rw = XYScreen(Double.(0).get("LONG_D").toString())-
gridcellwidth,Double.(0).get("LAT_D").toString())+gridcellheight, maxX, maxY, minX, minY, width,height);
int[] rb =
int recWidth = rb[0]-rw[0];
int recHeight = -rw[1]+rb[1];
FontMetrics fm = null;
int stringWidth=0;
int stringAscent = 0;
int stringDescent = 0;
true grit苏木精for (int i = 0; i < list.size(); i++) {
x = Double.(i).get("LONG_D").toString());
y = Double.(i).get("LAT_D").toString());
int[] screenXY = XYScreen(x-gridcellwidth,y+gridcellheight, maxX, maxY, minX, minY, width,height);
//绘制边框
Rectangle2D r = new Rectangle2D.Double(screenXY[0],screenXY[1],recWidth,recHeight);
g2d.tColor(Color.BLACK);
g2d.draw(r);
//绘制⽂字,填充颜⾊
(i).get("RN").equals("")){
g2d.drawString("0", screenXY[0]+recWidth/2, screenXY[1]+recHeight/2);
}el{
double rn = Double.(i).get("RN").toString());
Color color = null;
lost and foundif (rn>0 && rn<5) {
color = new Color(169,237,150);
} el if(rn > 5 && rn <= 10){
} el if(rn > 5 && rn <= 10){
color = new Color(58,188,52);
}el if(rn > 10 && rn <= 20){
color = new Color(65,161,212);
}el if(rn > 20 && rn <= 30){
color = new Color(57,189,221);
}el if(rn > 30 && rn <= 50){
color = new Color(6,0,254);
}el{
color = new Color(255,255,255);
}
g2d.tColor(color);
AlphaComposite ac = Instance(AlphaComposite.SRC_OVER, 0.5f);
g2d.tComposite(ac);
一对一网站
g2d.fillRect(screenXY[0]+1,screenXY[1]+1,recWidth-2,recHeight-2);
g2d.tColor(Color.BLACK);
fm = FontMetrics();
西安考研培训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);
worldenergy
}
}
return forecastGrid;
}
效果图如下: