| | |
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:
Solved Threads: 0
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:
This probably relates to these points which are used prior to calculating the width and height:
A similar for loop is probably required; even psuedocode would be very helpful.
Thanks in advance,
Cleo
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)
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:
Java Syntax (Toggle Plain Text)
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
with vector declaration as
Java Syntax (Toggle Plain Text)
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 Java Syntax (Toggle Plain Text)
//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()); }
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
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
•
•
Join Date: Nov 2007
Posts: 28
Reputation:
Solved Threads: 0
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:
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
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)
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); } }
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.
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
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)
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 } }
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
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
![]() |
Other Threads in the Java Forum
- Previous Thread: File I/O
- Next Thread: how to make attempts in pin numbers
| Thread Tools | Search this Thread |
-xlint add android api applet application array arrays automation bi binary blackberry bluetooth chat class classes client code compile compiler component converter database digit eclipse equation error event exception fractal freeze functiontesting game gameprogramming givemetehcodez graphics gui health html hyper ide idea image input int integer j2me java javame javaprojects jetbrains jni jpanel jtable julia learningresources linux list login loop main map method methods mobile myregfun netbeans newbie nonstatic notdisplaying page pearl print problem program programming project qt recursion scanner screen scrollbar server set size sms sort spamblocker sql string superclass swing system thread threads time tree variablebinding windows xor






