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:
Any help greatly appreciated! Thanks!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(); add(canvas, "Center"); } public static void main (String [] args) { BoxDrawer f = new BoxDrawer(); MouseWatcher mw = new MouseWatcher(); f.canvas.addMouseListener(mw); f.canvas.addMouseMotionListener(mw); WindowWatcher ww = new WindowWatcher(); f.addWindowListener(ww); 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()); points.add(coords); 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 } }


Reply With Quote
Thought I'd leave that in case anybody else coems across a similar problem!



Bookmarks