[RESOLVED]please help with Vector
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...
Code:
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...
Code:
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.
Code:
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
Re: Passing coordinates to a Vector...help
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.
Re: Passing coordinates to a Vector...help
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...
Code:
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...
Quote:
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
Re: Passing coordinates to a Vector...help
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.
Code:
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
Re: Passing coordinates to a Vector...help
Crap, I think I totally miss-understood everything you said to begin with. -- Sorry about that.
It looks okay from what I can see....
Quote:
but for some reason i get an error when i try to compile.
What's the error message?
Quote:
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.
Re: Passing coordinates to a Vector...help
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...
Code:
if(panelID == 1) {
if(e.getButton() == MouseEvent.BUTTON1) {
Point p = new Point(e.getPoint());
features.add(p);
repaint();
}
and.....
Code:
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.
Code:
if(panelID == 2) {
if(e.getButton() == MouseEvent.BUTTON1) {
Point p8 = new Point(e.getPoint());
features.add(p8);
repaint();
}
...and to draw it
Code:
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.