DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

1. Registered User
Join Date
Oct 2002
Posts
66

## Vector Problem

Hi. I was wondering if anybody could help me with a little problem with vectors that I can't seem to overcome?

I have a coursework due in on Monday which I haven't started till yesterday for various reasons. I have to write a program where you can draw 4 seperate, roughly straight lines and then my program will turn it into a box. I'll do this by getting the average of the line's co-ordinates and re-drawing them to create a full box. But that's irrelevant for the moment.

What I've done so far is created a vector which contains Point arrays of size 2. Each Point array stores a line. The first Point in the array stores a start co-ordinate and the second stores the end. For some reason when I add another Point array to the vector it overwrites all the original ones.

Here is my code:

Code:
```import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class BoxDrawer extends Frame {

private Canvas canvas;

public BoxDrawer() {
setTitle("Box Drawer");
setSize(500, 500);
canvas = new Canvas();
}

public static void main (String [] args) {
BoxDrawer f = new BoxDrawer();
MouseWatcher mw = new MouseWatcher();
WindowWatcher ww = new WindowWatcher();
f.show();
}
}

class MouseWatcher extends MouseAdapter implements MouseMotionListener {

private Point p, oldp;	//Store the points for drawing
private Vector points = new Vector();
private Point[] coords = new Point[2];

public void mousePressed(MouseEvent e) {
oldp = e.getPoint(); //Get the current point
coords[0] = oldp;
}

public void mouseReleased(MouseEvent e) {
coords[1] = p;
System.out.println(points.size());
outputPoints();

Enumeration enumerate = points.elements();
while (enumerate.hasMoreElements()) {
Point[] temp = new Point[2];
temp = (Point[]) enumerate.nextElement();
if ((temp[0].x - temp[1].x <= 10) | (temp[1].x - temp[0].x <= 10)) {
System.out.println("horizontal");
straightenHorizontal(e, temp);
} else if ((temp[0].y - temp[1].y <= 10) | (temp[1].y - temp[0].y <= 10)) {
System.out.println("vertical");
straightenVertical(e, temp);
} else {
System.out.println("neither");
}
}
}

public void mouseDragged (MouseEvent e) {
p = e.getPoint(); //Get the current point
Graphics g = ((Canvas)e.getSource()).getGraphics(); //Create a graphic on the canvas
g.drawLine(oldp.x, oldp.y, p.x, p.y); //Draw on the canvas
oldp = p; //Remember where the mouse currently is
g.dispose(); //Free resources
}

public void mouseMoved (MouseEvent e) {
//Do nothing
}

public void clearCanvas(MouseEvent e) {
Graphics g = ((Canvas)e.getSource()).getGraphics(); //Create a graphic on the canvas
g.clearRect(0,0,500,500); //Clear a space the size of the canvas
g.dispose(); //Free resources
}

public void outputPoints() {
Enumeration enumerate = points.elements();
while (enumerate.hasMoreElements()) {
Point[] temp = new Point[2];
temp = (Point[]) enumerate.nextElement();
System.out.println("Start = " + temp[0].x + "," + temp[0].y + " End = " + temp[1].x + "," + temp[1].y);
}
}

public void drawLine(MouseEvent e, Point start, Point end) {
Graphics g = ((Canvas)e.getSource()).getGraphics(); //Create a graphic on the canvas
g.drawLine(start.x, start.y, end.x, end.y); //Draw on the canvas
g.dispose(); //Free resources
}

public void straightenHorizontal(MouseEvent e, Point[] temp) {
clearCanvas(e);

//Get average of both points

drawLine(e, temp[0], temp[1]);
}

public void straightenVertical(MouseEvent e, Point[] temp) {
clearCanvas(e);

//Get average of both points

drawLine(e, temp[0], temp[1]);
}
}

class WindowWatcher implements WindowListener {

public void windowClosing(WindowEvent e) {
System.exit(0); //Exit the program
}

public void windowDeactivated(WindowEvent e) {
//Do nothing
}

public void windowActivated(WindowEvent e) {
//Do nothing
}

public void windowClosed(WindowEvent e) {
//Do nothing
}

public void windowIconified(WindowEvent e) {
//Do nothing
}

public void windowOpened(WindowEvent e) {
//Do nothing
}

public void windowDeiconified(WindowEvent e) {
//Do nothing
}
}```
Any help greatly appreciated! Thanks!

2. Registered User
Join Date
Oct 2002
Posts
66
No worries - I've found hte solution now. I sent it to one of the guys I worked with in Sony. It's basically what I thought (but neglected to mention here) - I've still not exactly got my head about objects and how they point to one another etc. but the error was happening because they were all pointing to the same point and when that changed then so would all the others.

So the solution was to put this:

Code:
```public void mouseReleased(MouseEvent e) {
coords[1] = p;
System.out.println(points.size());
coords = new Point[2];

clearCanvas(e);
Enumeration enumerate = points.elements();
while (enumerate.hasMoreElements()) {
Point[] temp = new Point[2];
temp = (Point[]) enumerate.nextElement();
if (temp[1].x - temp[0].x > temp[1].y - temp[0].y) {
System.out.println("vertical");
straightenVertical(e, temp);
} else if (temp[1].y - temp[0].y > temp[1].x - temp[0].x) {
System.out.println("horizontal");
straightenHorizontal(e, temp);
}
}
}```
...that way it they will be their own objects, and not just pointing to another Thought I'd leave that in case anybody else coems across a similar problem!

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 FAQ Latest Articles Java .NET XML Database Enterprise