Paint EVERY rectangle drawn on a canvas using vector

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Nov 2007
Posts: 28
Reputation: Cleo123 is an unknown quantity at this point 
Solved Threads: 0
Cleo123 Cleo123 is offline Offline
Light Poster

Paint EVERY rectangle drawn on a canvas using vector

 
0
  #1
Nov 29th, 2008
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:

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

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

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

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

Thanks in advance,
Cleo
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,205
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 486
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: Paint EVERY rectangle drawn on a canvas using vector

 
0
  #2
Nov 29th, 2008
Why not have class like this
  1. public class Points
  2. {
  3. private int xTopLeft;
  4. private int yTopLeft;
  5. private int xBottRight;
  6. private int yBottRight;
  7.  
  8. public Points(){}
  9.  
  10. public Points(int a, int b, int c, int d)
  11. {
  12. setXTopLeft(a);
  13. setYTopLeft(b);
  14. setXBottRight(c);
  15. setYBottRight(d);
  16. }
  17.  
  18. private void setXTopLeft(int i){xTopLeft = i;}
  19. public int getXTopLeft(){return xTopLeft;}
  20.  
  21. private void setYTopLeft(int i){yTopLeft = i;}
  22. public int getYTopLeft() { return yTopLeft;}
  23.  
  24. private void setXBottRight(int i) {xBottRight = i;}
  25. public int getXBottRight(){return xBottRight;}
  26.  
  27. private void setYBottRight(int i) {yBottRight = i;}
  28. public int getYBottRight() { return yBottRight;}
  29. }

with vector declaration as Vector<Points> vec = new Vector<Points>(); and
  1. //add new element to vector
  2. vec.add(new Pointer(X_TOP_LEFT, Y_TOP_LEFT, X_BOTTOM_RIGHT, Y_BOTTOM_RIGHT));
  3.  
  4. //retrieve rectangle points
  5. for(Vector<Pointers>point:vec)
  6. {
  7. //DO WHAT EVER YOU WANT WITH RETRIEVED DATA
  8. System.out.println("Shape coordinates: "+point.getXTopLeft()+"-"+point.getYTopLeft()+"-"+point.getXBottRight()+"-"+point.getYBottRight());
  9. }
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 28
Reputation: Cleo123 is an unknown quantity at this point 
Solved Threads: 0
Cleo123 Cleo123 is offline Offline
Light Poster

Re: Paint EVERY rectangle drawn on a canvas using vector

 
0
  #3
Nov 29th, 2008
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:

  1. for(i = 0; i < vector.size(); i++){
  2. Point a = (Point) vector.get(i);
  3.  
  4. //Get the top left corner
  5. if (a.x < topLeftPoint.x && a.y < topLeftPoint.y)
  6. topLeftPoint = a;
  7. System.out.println("Top Left" + topLeftPoint);
  8.  
  9. for(j = 0; j < vector.size(); j++)
  10. {
  11. Point b = (Point) vector.get(j);
  12. //Get the bottom right corner
  13. if (b.x > bottomRightPoint.x && b.y > bottomRightPoint.y) {
  14. bottomRightPoint = b;
  15. System.out.println("Bottom right" + bottomRightPoint);
  16. }
  17. }
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
Last edited by Cleo123; Nov 29th, 2008 at 2:48 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,205
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 486
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: Paint EVERY rectangle drawn on a canvas using vector

 
0
  #4
Nov 29th, 2008
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
  1. for(i = 0; i < vector.size()-1; i++)
  2. {
  3. Point a = (Point) vector.get(i);
  4. Point b = (Point) vector.get(i+1);
  5. if(a.x < b.x && a.y < b.y)
  6. {
  7. System.out.println("Got my coordinates");
  8. i++; // need to move one extra position so loop doesn't use "b" Point
  9. }
  10. else
  11. {
  12. System.out.println("A point has bigger values then B");
  13. //for loop moves "b" values to "a" and be get new set
  14. }
  15. }
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC