这个问题继续GPS coordinates to pixel。 我需要绘制几个多边形。我可以单独绘制每个多边形,但不能将所有多边形放在正确的位置。
我从此文件加载有关多边形的信息: GPS
我有Kraj类,代表每个多边形。
public class Kraj {
String name;
Point2D.Double points[];
Point2D.Double transPoints[];
Point2D.Double max;
Point2D.Double min;
// polygon
Path2D.Double polygon;
ArrayList<Kraj> kraje;
public Kraj(String name, Point2D.Double body[])
{
this.name = name;
this.body = Arrays.copyOf(body, body.length);
// calculate a bounding box
zjistiLimity();
this.transPoints = new Point2D.Double[points.length];
}
private void transformToWindow(int width, int height)
{
// convert to window
double convertX = width / (max.x - min.x);
double convertY = height / (max.y - min.y);
// calculate polygon to fit in window with right aspect ratio
double convert = convertX > convertY ? convertY : convertX;
// min = 0, convert to interval <0: infinity> and multiply by convert,
for (int j = 0; j < points.length; j++) {
double transX = (points[j].x - min.x) * convert;
double transY = height - (points[j].y - min.y) * convert;
transPoints[j] = new Point2D.Double(transX, transY);
}
this.polygon = new Path2D.Double();
this.polygon.moveTo(transBody[0].x, transBody[0].y);
for (int i = 1; i < body.length; i++)
this.polygon.lineTo(transPoints[i].x, transPoints[i].y);
this.polygon.closePath();
}
private void drawKraj(Graphics2D g2, int width, int height) {
g2.setStroke(new BasicStroke(2));
g2.fill(polygon);
// vykreslime obrys
g2.setColor(Color.black);
g2.draw(polygon);
}
public void draw(Graphics2D g2,
int contextWidth, int contextHeight)
{
// fit to window size
int sirkaSOdsazenim = contextWidth;
int vyskaSOdsazenim = contextHeight;
this.transformujToWindow(sirkaSOdsazenim, vyskaSOdsazenim);
this.drawKraj(g2, sirkaSOdsazenim, vyskaSOdsazenim);
}
/**
* Set min and max
*/
private void zjistiLimity() {
max = new Point2D.Double(-Double.MAX_VALUE, -Double.MAX_VALUE);
min = new Point2D.Double(Double.MAX_VALUE, Double.MAX_VALUE);
for(int j = 0; j < 10; j++)
{
for (int i = 0; i < body.length; i++)
{
if (points[i].getX() < min.getX()) min.x = points[i].getX();
if (points[i].getY() < min.getY()) min.y = points[i].getY();
if (points[i].getX() > max.getX())max.x = points[i].getX();
if (points[i].getY() > max.getY()) max.y = points[i].getY();
}
}
}
使用此代码,我可以绘制适合窗口的多边形。但我需要绘制所有多边形以适合窗口(计算坐标以创建此贴图):
我需要编辑或添加什么?谢谢你的所有答案。
答案 0 :(得分:2)
您可以使用以下方法翻译整个多边形:
g2.translate(x, y);
g2.draw(polygon);
g2.translate(-x, -y)
确定每个多边形的适当x / y平移是您需要做的事情。
答案 1 :(得分:1)
我需要绘制几个多边形。我可以单独绘制每个多边形,但我无法在正确的位置绘制所有多边形。
如果您可以单独绘制每个多边形,那么您的多边形是正确的。
您需要在Kraj
课程中添加原点。然后,您的draw
方法会将多边形点从多边形原点转换为地图原点。假设您的多边形原点是(10,10)并且需要在(20,30)处绘制特定多边形,那么您将在x中添加10并在绘制之前将20添加到多边形中每个点的y。
您可以在调整每个点的X和Y值之前,在绘制例程中制作多边形的副本。
编辑添加:这是您自己修改的代码以转换原点。我没有测试过这些变化。
private void transformToWindow(Point2D windowOrigin, int width, int height)
{
// convert to window
double convertX = width / (max.x - min.x);
double convertY = height / (max.y - min.y);
// calculate polygon to fit in window with right aspect ratio
double convert = convertX > convertY ? convertY : convertX;
// min = 0, convert to interval <0: infinity> and multiply by convert,
for (int j = 0; j < points.length; j++) {
double transX = (points[j].x - min.x) * convert;
double transY = height - (points[j].y - min.y) * convert;
transPoints[j] = new Point2D.Double(transX, transY);
}
this.polygon = new Path2D.Double();
double xShift = windowOrigin.x - transBody[0].x;
double yShift = windowOrigin.y - transBody[0].y;
this.polygon.moveTo(windowOrigin.x, windowOrigin.y);
for (int i = 1; i < body.length; i++)
this.polygon.lineTo(transPoints[i].x + xShift,
transPoints[i].y + yShift);
this.polygon.closePath();
}