如何在形状上绘制轮廓矩形?
我有很多形状,我想用轮廓矩形包围它们?
我先画了fill()
然后draw()
public void paint(Graphics g){
Graphics2D g2d = (Graphics2D) g;
Rectangle2D R = getRect();
g2d.setColor(Color.blue);
g2d.fill(path);
g2d.setColor(Color.red);
g2d.draw(R);
}
getRect()
方法:
public Rectangle2D getRect(){
return new Rectangle2D.Double(MinX(),MinY(),MaxX(),MaxY());
}
MaxX()
和MaxY()
方法是相同的,除了更改Y
而不是X
:
public double MaxX(){
double max = Double.MIN_VALUE;//check the smallest double value
double maxPathEntry=0.0;
Paths01 p=new Paths01();
for ( int i = 0 ; i < PathsList.size() ; i++ ) {
maxPathEntry = PathsList.get(i).MaxX();
if (maxPathEntry > max ){
max = maxPathEntry;
}
else if (p.MoveToX > maxPathEntry){
max = p.MoveToX;
}
}
return max;
}
MinX()
和MinY()
方法是:
public double MinX(){
double min = Double.MAX_VALUE;//check the biggest double value
double minPathEntry=0.0;
Paths01 p=new Paths01();
for ( int i = 0 ; i < PathsList.size() ; i++ ) {
minPathEntry = PathsList.get(i).MinX();
if (minPathEntry < min ){
min = minPathEntry;
}
else if (p.MoveToX < minPathEntry){
min= p.MoveToX;
}
}
return min;
}
但是当它工作时,它没有在形状上显示精确的轮廓矩形,它在形状上显示不合适的轮廓矩形