View Single Post
Old 02-17-2005, 08:02 AM   #4 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,175
Belisarius is on a distinguished road
Vector is just an old class that was jury-rigged into the collections framework. ArrayList is basically the same thing, only built from the ground up to be part of Collections. Vector has been kept around for backwards compatability, but it's a little bit heavy for what most people want to use it for.

Now, what I would do is write yor own Pixel object.

Code:
public class Pixel extends java.awt.Point implements Comparable{
  private int red;
  private int green;
  private int blue;
  
  public Pixel() {
    super();
    init(0,0,0,0,0);
  }
  
  public Pixel(int red, int green, int blue){
    super();
    init(0, 0, red, green, blue);
  }
  
  public Pixel(int x, int y, int red, int green, int blue){
    super();
    init(x, y, red, green, blue);
  }
  
  private void init(int x, int y, int red, int green, int blue){
    super.setLocation(x, y);
    this.red = red;
    this.green = green;
    this.blue = blue;    
  }
  
  public boolean compareTo(Pixel input){
    // Write your compareTo method here
  }
  
  public String toString(){
    // Write your toString method here    
  }
  
  public boolean equals(Pixel input){
    // Write your equals method here
  }
  
  public int hashCode(){
    // Write your hashCode method if you plan on using something that requires
    // a hash.
  }

  public int getRed() {
    return red;
  }

  public void setRed(int red) {
    this.red=red;
  }

  public int getGreen() {
    return green;
  }

  public void setGreen(int green) {
    this.green=green;
  }

  public int getBlue() {
    return blue;
  }

  public void setBlue(int blue) {
    this.blue=blue;
  }
}
Be sure to write the compareTo() and equals() methods. You can then add them to a Set (say . . . TreeSet). This will store only unique Pixels. If you want, you can write your own Comparator and pass it to the TreeSet constructor should you wish to consider *only* colors and not position, while not tainting the default method of comparison for Pixel. In other words, pixels with the same RGB values but different XY values would be, by default, different. If you only wanted to consider similar RGB values, you could write an anonymous Comparator, and pass it to the TreeSet when you create it. It would then include only unique RGB values, regardless of XY position.
__________________
GitS
Belisarius is offline   Reply With Quote