Hi all,

Background:
I am dealing with graphical objects on a canvas in which the user draws four lines and out of these four lines I get the top left corner, bottom right corner and figure out the width and height from these two points.

Problem:
I have completed this but I would also like to use a for loop to get every set of 4 integers in my vector which represent a rectangle and draw it to canvas. Overall printing every rectangle drawn on the canvas. The first rectangle prints fine although the program freezes there after.

Here is the for loop I have tried to use to print of all rectangles drawn on the canvas which currently prints the top left corner point and bottom right corner point out of all rectangles:

for(int z=0; z < 5; z++) {
//for(int i =0 ; i < 5; i++){
rectangle1 = (Point) vector.get(z);			
//Point b  = (Point) vectorRect.get(i);
graf.drawRect(topLeftPoint.x, topLeftPoint.y, width, height);
}
//z = i+1;
//}

This probably relates to these points which are used prior to calculating the width and height:

bottomRightPoint = (Point) vector.get(0);
topLeftPoint = (Point) vector.get(0);

A similar for loop is probably required; even psuedocode would be very helpful.

Thanks in advance,
Cleo

Why not have class like this

public class Points
{
	private int xTopLeft;
	private int yTopLeft;
	private int xBottRight;
	private int yBottRight;
	
	public Points(){}
	
	public Points(int a, int b, int c, int d)
	{
		setXTopLeft(a);
		setYTopLeft(b);
		setXBottRight(c);
		setYBottRight(d);
	}
	
	private void setXTopLeft(int i){xTopLeft = i;}
	public int getXTopLeft(){return xTopLeft;}
	
	private void setYTopLeft(int i){yTopLeft = i;}
	public int getYTopLeft() { return yTopLeft;}
	
	private void setXBottRight(int i) {xBottRight = i;}
	public int getXBottRight(){return xBottRight;}
	
	private void setYBottRight(int i) {yBottRight = i;}
	public int getYBottRight() { return yBottRight;}
}

with vector declaration as Vector<Points> vec = new Vector<Points>(); and

//add new element to vector
vec.add(new Pointer(X_TOP_LEFT, Y_TOP_LEFT, X_BOTTOM_RIGHT, Y_BOTTOM_RIGHT));

//retrieve rectangle points
for(Vector<Pointers>point:vec)
{
    //DO WHAT EVER YOU WANT WITH RETRIEVED DATA
    System.out.println("Shape coordinates: "+point.getXTopLeft()+"-"+point.getYTopLeft()+"-"+point.getXBottRight()+"-"+point.getYBottRight());
}

Thanks for the input, I really appeciate it although I have kept the same vector I already have and used your for vector method which was really beneficial; the canvas now paints more than one rectangle on the canvas!

However all of these rectangles use the same start point...It's to do with my for statement...The answer seems so clear which is why I've replied s late trying to do it sorry but yet I can't pinpoint how to solve it:

for(i = 0; i < vector.size(); i++){
Point a = (Point) vector.get(i);

//Get the top left corner
if (a.x < topLeftPoint.x && a.y < topLeftPoint.y)
topLeftPoint = a;
System.out.println("Top Left" + topLeftPoint);

for(j = 0; j < vector.size(); j++)
{
Point b  = (Point) vector.get(j);
//Get the bottom right corner
if (b.x > bottomRightPoint.x && b.y > bottomRightPoint.y) {
bottomRightPoint = b;
System.out.println("Bottom right" + bottomRightPoint);
}
}

This code is under the construction/calculations of the rectangle.
I have two vectors; the first of which stores all 8 points and the second stores the rectangles.
I need a better way of obtaining the topLeftPoint and bottomRightPoint without looking through the for for the highest and lowest out of all points! I'm going to try and use the same method as I just did with the last method..I think this may work..

Cleo

Sorry for the first one I misread your request, but did not get far off.
As I do not know exactly your storing procedure of Point coordinates I will guess and use little dirty coding

for(i = 0; i < vector.size()-1; i++)
{
    Point a = (Point) vector.get(i);
    Point b = (Point) vector.get(i+1);
    if(a.x < b.x && a.y < b.y)
    {
        System.out.println("Got my coordinates");
        i++; // need to move one extra position so loop doesn't use "b" Point
    }
    else
    {
        System.out.println("A point has bigger values then B");
        //for loop moves "b" values to "a" and be get new set
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.