Sorting Vector of My Own Objects
I've almost made it through a whole semester of Java class without getting totally stumped but it seems ive hit a brick wall this time.
I have an assignment in which i have to store points from a mouse click on a JPanel and display them on the screen and be able to do some other stuff with them. We're required to use a vector to store the points. The actual assignment can be found here.
http://www.csl.mtu.edu/cs1121/www.fall.06/hw.assgn/hw4/desc.html
I have created a new class called myPoint to store the point and its color.
Heres my problem. Some of the tasks require me to go through the vector and display stuff on the screen. In order to do this correctly , i need to sort the vector based on the x value stored in the myPoint objects. I know i cannot directly sort the vector as java doesnt know how to compare my objects. I tried reading about creating my own comparable objects, but i couldnt seem to make heads or tails of it.
Heres the code for my myPoint class
import java.awt.*;
import java.util.*;
publicclass myPoint{
/**
* @author
* --
*
* Stores info about the point
* @param point p = the point
* @param color c = the color of the point and line
*/
private Point p;
private Color c;
//creates the new point object
public myPoint(Point point, Color color){
p = point;
c = color;
}
/**
* return methods
*/
// send the x coordinate of the requested point
publicint getX(){
return((int)p.getX());
}//end of getX method
// send the y coordinate of the requested point
publicint getY(){
return((int)p.getY());
}//end of getY method
// send the color of the requested point- black or red
public Color getCol(){
return (c);
}//end of getCol method
/**
* update methods
*/
//used to change the color of the point for highlighting min/max point
publicvoid updateColor(Color color){
c = color;
}//end of updateColor method
//used to change the x component of the point for evenspace sorting
publicvoid updateX(int newx){
//int x = (int)p.getX();
int y = (int)p.getY();
p =new Point(newx, y);
}
}//end of myPoint class
heres some of the code from the Display class that actually does everything
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
/**
* @author -
* --
* Display points clicked into display area.
*/
publicclass Displayextends JPanelimplements MouseListener{
// properties
private Vector<myPoint> points =new Vector<myPoint>();
privateboolean connectadjline =false;
/**
* Set up display.
*/
public Display(){
setPreferredSize(new Dimension(490,400));
setBackground(Color.gray);
addMouseListener(this);
}// end of constructor
/**
* Draw points.
*
* @param g The Graphics object to draw with.
*/
publicvoid paintComponent(Graphics g){
super.paintComponent(g);
// draw each point
for (myPoint p : points){
g.setColor(p.getCol());
int x = p.getX();
int y = p.getY();
g.fillOval(x-3,y-3,6,6);
g.drawLine(x, y, x, 400);
}
//draw connecting lines if connectadjline = true
if (connectadjline ==true){
sortX();
g.setColor(Color.red);
for (int i = 0; i<points.size()-1; i+=1 ){
myPoint p1 = points.get(i);
myPoint p2 = points.get(i+1);
int sx = p1.getX();
int sy = p1.getY();
int ex = p2.getX();
int ey = p2.getY();
g.drawLine(sx,sy,ex,ey);
}
}
}// end of paintComponent method
// a bunch of other methods for doing stuff
//sort vector according to x
publicvoid sortX(){
//HELP!
}//end of sortX method
}// end of Display class
So i need to know how to sort the points vector based on the x value sent by the myPoint objects
Thanks in advance!>

