943,551 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 1517
  • Java RSS
Nov 29th, 2008
0

Paint EVERY rectangle drawn on a canvas using vector

Expand Post »
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:

Java Syntax (Toggle Plain Text)
  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:

Java Syntax (Toggle Plain Text)
  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
Reputation Points: 39
Solved Threads: 12
Junior Poster
StephNicolaou is offline Offline
153 posts
since Nov 2007
Nov 29th, 2008
0

Re: Paint EVERY rectangle drawn on a canvas using vector

Why not have class like this
Java Syntax (Toggle Plain Text)
  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
Java Syntax (Toggle Plain Text)
  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. }
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 871
Code tags enforcer
peter_budo is offline Offline
6,653 posts
since Dec 2004
Nov 29th, 2008
0

Re: Paint EVERY rectangle drawn on a canvas using vector

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:

Java Syntax (Toggle Plain Text)
  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 StephNicolaou; Nov 29th, 2008 at 2:48 pm.
Reputation Points: 39
Solved Threads: 12
Junior Poster
StephNicolaou is offline Offline
153 posts
since Nov 2007
Nov 29th, 2008
0

Re: Paint EVERY rectangle drawn on a canvas using vector

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
Java Syntax (Toggle Plain Text)
  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. }
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 871
Code tags enforcer
peter_budo is offline Offline
6,653 posts
since Dec 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: File I/O
Next Thread in Java Forum Timeline: how to make attempts in pin numbers





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC