Something wrong with getBound
I am using GeneralPath to draw polygonal lines but for some reason the polyline.getBound() is not giving the corrent returns. I know this because the value set to the GeneralPath are from arrays.
The maximum value for x should have been 300 but it shows 280 and for y it should have been 300 again but it whos 260.
Does any one know what might be the problem?
int x[] ={20,50,200,100,300,50,45};
int y[] ={40,100,40,300,60,80,35};
GeneralPath polyPath =new GeneralPath(GeneralPath.WIND_EVEN_ODD,x.length);
polyPath.moveTo(x[0],y[0]);
for (int index=1; index <x.length; index++)
{
polyPath.lineTo(x[index], y[index]);
System.out.println(x[index] +" -- " + y[index]);
}
Graphics2D g2 = (Graphics2D) g;
g2.draw(polyPath);
//g.drawPolyline(x,y,x.length);
Rectangle r = polyPath.getBounds();
g.drawRect((int)r.getMinX(), (int)r.getMinY(), (int)r.getMaxX(), (int)r.getMaxY());
>
[1501 byte] By [
getusamaa] at [2007-11-26 15:33:20]

# 3
I must have mis-understood what you were asking. There is nothing wrong with your example.
You have to understand, the WIDTH of the bounding box is NOT the same thing as the MAX X and the HEIGHT of the bounding box is NOT the same thing as the MAX Y.
I think you're being confused by the two. Please take a look my code example, and then ask me your question is more detail.
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.GeneralPath;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class BoundsTest extends JFrame
{
private JPanel panel;
public BoundsTest()
{
this.setSize(500,500);
this.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
this.setContentPane(getPanel());
this.setVisible(true);
}
public JPanel getPanel()
{
if(panel == null)
{
panel = new JPanel()
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
int x[] = {20,50,200,100,300,50,45};
int y[] = {40,100,40,300,60,80,35};
GeneralPath polyPath = new GeneralPath(GeneralPath.WIND_EVEN_ODD,x.length);
polyPath.moveTo(x[0],y[0]);
for (int index=1; index <x.length; index++)
{
polyPath.lineTo(x[index], y[index]);
System.out.println(x[index] + " -- " + y[index]);
}
g.setColor(Color.BLACK);
for(int i=0; i><=300; i=i+10)
{
//verical lines
g.drawLine(i, 0, i, 300);
//horizontal lines
g.drawLine(0,i,300,i);
}
g2.setColor(Color.RED);
g2.draw(polyPath);
g2.setColor(Color.GREEN);
Rectangle r = polyPath.getBounds();
System.out.println();
System.out.println();
System.out.println("Bounds min x: " + r.getMinX());
System.out.println("Bounds min y: " + r.getMinY());
System.out.println("Bounds max x: " + r.getMaxX());
System.out.println("Bounds max y: " + r.getMaxY());
System.out.println("Bounds width: " + r.width);
System.out.println("Bounds height: " + r.height);
g.drawRect(r.x, r.y, r.width, r.height);
}
};
}
return panel;
}
public static void main(String args[])
{
new BoundsTest();
}
}
# 5
By the way you are answering / replying, I think your problem is that you don't understand how drawRect works.
You're code:
g.drawRect((int)r.getMinX(), (int)r.getMinY(), (int)r.getMaxX(), (int)r.getMaxY());
My code:
g.drawRect(r.x, r.y, r.width, r.height);
Please note drawRect's parameters: your code is not using width and height as the 3rd and 4th parameters... it's using max X and max Y... which is terribly wrong. Please refer to the API.