PDA

Click to See Complete Forum and Search --> : [RESOLVED]please help with Vector


jlbovo
Nov 13th, 2006, 02:34 PM
Hello, I am still wokring on the same project and I have come into yet another problem I'd like to learn to fix now.

The desgin should be as follows : I need to pass x-y coordiantes to a Vector, so i can store them. Left clicking would draw a circle and get the coordinates of the point clicked. The Vector is there so if I were to right click I could erase the last stored coordinates.

Here's what I was given to work with...


class ImagePanel extends JPanel {

private BufferedImage currentbufimg; // this is the real image associated with each ImagePanel object.
private int panelID; // identifier for each ImagePanel.
private UIDDemo1 parent; // parent object.
private Vector features; // features to be selected by the users.


As you can see the Vector is named features.... and a paint component will call the drawfeatures object....and then...


public void paintComponent(Graphics g){
super.paintComponent(g);
if(currentbufimg != null)
g.drawImage(currentbufimg,0,0,this);
if(panelID == 1 || panelID == 2)
drawFeatures(g);
if(panelID == 3) {
drawDisplacement(g);
}
}


...i'm with left this little bit of code to figure out how i would be able to get those variables passed to the Vector.

public void drawFeatures(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
g2d.setColor(Color.white);

// draw circles and save to Vector.




I already have a mouse event handler to capture the x-y coordniates when the mouse is clicked. I just need some type of guidence on how i could go abouts doing this...I understand the add etc of the Vector soem what, but will i need another mouse event to get thsi started.

Again any guidence will be very helpful, for i am tryin my best to understand and learn thsi Java. Thansk again - Justin

System_Error
Nov 13th, 2006, 06:32 PM
Add setter methods in the Vector class such as setXPoint(int x); and setYPoint(int y);

If you add setter methods then with a declaration like this: private Vector features; you can call features.setXPoint(3) or whatever.

jlbovo
Nov 14th, 2006, 10:27 AM
So, I think I have to add each coordinate seperate right ? I was thinkin that i'd be able to add them together as a pair. SO say...


features.add(cordX)
features.add(cordY)


thats what i came up with,I'm not sure if you have the time to explain to me exaclty what your tryin to say here...add setter methods in the Vector class such as setXPoint(int x); and setYPoint(int y)....is that basicly what I just asked up a few lines ? I'm still fairly new to java, and doing thing i haven't done before make me nervous. Please help, share your knowledge thanks - justin

jlbovo
Nov 16th, 2006, 08:56 AM
ok well, in my contining effort for this i think i have made progress.

i forgot to mention i hade a mouse clikc action for the left click that add features.


if(panelID == 1) {
if(e.getButton() == MouseEvent.BUTTON1) { // left button, add features.
Point p = new Point(e.getPoint());
// add the point to the features.
features.add(p);
repaint();
}
if(e.getButton() == MouseEvent.BUTTON3) { // right button, remove features.
// implement a removeLastFeature() method.
repaint();
}



...but for some reason i get an error when i try to compile. Am i adding the features right or am i missing something ??? Does point get both x,y and make it one item..? please some guidience thanks a million - justin

System_Error
Nov 17th, 2006, 05:02 PM
Crap, I think I totally miss-understood everything you said to begin with. -- Sorry about that.



It looks okay from what I can see....


but for some reason i get an error when i try to compile.


What's the error message?



oes point get both x,y and make it one item..?


Yes, it gives both x and y coordinates.


Let me know what error message you are getting and what it's not doing that it should and I can help you out more.

jlbovo
Nov 20th, 2006, 11:08 AM
hello sorry i took so long to reply.

But i have made some progress in the program, but now I have encounter new problems.
I am now able to pass to coordinates as a Point to the vector...which in turn will draw the circles. Here's the code for that...


if(panelID == 1) {
if(e.getButton() == MouseEvent.BUTTON1) {
Point p = new Point(e.getPoint());
features.add(p);
repaint();
}

and..... int nfts = features.size();
if(nfts == 0)
return;

for(int i = 0; i < nfts; i++){
Point p = (Point)features.get(i);
if(p == null)
continue;
g2d.setStroke(new BasicStroke(2.0f));
g2d.drawOval(p.x-5, p.y-5, 10, 10);
}

....so that there draws the white circles only in Panel 1. Now from my limited abililty in Java i came up with this so i can draw White rectangles in Panel 2.

...here i get the points.

if(panelID == 2) {

if(e.getButton() == MouseEvent.BUTTON1) {
Point p8 = new Point(e.getPoint());
features.add(p8);
repaint();
}
...and to draw it
for(int i = 0; i < nfts; i++){
Point p8 = (Point)features.get(i);
if(p8 == null)
continue;
g2d.setStroke(new BasicStroke(2.0f));
g2d.drawRect(p8.x-5, p8.y-5, 10, 10);
}

but now i draw White squares in both panel 1 and Panel 2. But what i really Want is to be able to draw White circles in Panel1 and White squares in Panel2. Please alert me if I am making a mistake, So i can correct it and learn from it. Also why do i draw the squares in Panel 2 and no Circles anymore in Panel 1 ?? Help would be greatly apperciated. - thanks Justin.