Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- COLOR
- public class Color implements Comparable
- {
- final static int BYTE_MASK=255;
- private long red,green,blue;
- protected long color;
- public void rgb2color()
- {
- color=(red<<16)|(green<<8)|(blue);
- }
- public void color2rgb()
- {
- red=color>>16;
- green=(color>>8)&BYTE_MASK;
- blue=color&BYTE_MASK;
- }
- public Color()
- {
- }
- public Color(long c)
- {
- this.color=c;
- color2rgb();
- }
- public long getRed()
- {
- return red;
- }
- public void setRed(long r)
- {
- red=r;
- rgb2color();
- }
- public long getGreen()
- {
- return green;
- }
- public void setGreen(long v)
- {
- green=v;
- rgb2color();
- }
- public long getBlue()
- {
- return blue;
- }
- public void setBlue(long m)
- {
- blue=m;
- rgb2color();
- }
- public String toString()
- {
- return "Red: "+red+",Green:"+green+ ", Blue:" +blue+", Color:"+color;
- }
- public boolean equals(Object r){
- return this.color==((Color)r).color;
- }
- public int compareTo(Object c)
- {
- if (this.color<((Color)c).color)
- return -1;
- if (this.color>((Color)c).color)
- return 1;
- return 0;
- }
- }
- COLOR RECTANGLE
- public class ColorRectangle extends Color implements Comparable
- {private int iX1,iY1,iX2,iY2;
- public ColorRectangle(){}
- public ColorRectangle(int x1,int y1,int x2,int y2,long c){
- super(c);
- iX1=x1;
- this.iY1=y1;
- this.iX2=x2;
- this.iY2=y2;
- }
- public int getIX1(){
- return iX1;
- }
- public int getIY1(){
- return iY1;
- }
- public int getIx2(){
- return iX2;
- }
- public int getIY2(){
- return iY2;
- }
- public int calcArea(){
- return Math.abs((iX2-iX1)*(iY2-iY1));
- }
- public int compareTo(Object r){
- if(this.calcArea()<((ColorRectangle)r).calcArea())
- return -1;
- if(this.calcArea()>((ColorRectangle)r).calcArea())
- return 1;
- return 0;
- }
- public String toString(){
- return "x1:"+iX1+",Y1:"+iY1+",x2:"+iX2+",Y2:"+iY2+"Color:"+super.toString();
- }
- public boolean equals(ColorRectangle r){
- return(this.calcArea()==r.calcArea()&&this.equals(r));
- }
- public void translateX(int iPoints){
- iX1=iX1+iPoints;
- iX2+=iPoints;
- }
- public void translateY(int iPoints){
- iY1=iY1+iPoints;
- iY2+=iPoints;
- }
- public void translateXY(int iPoints){
- translateX(iPoints);
- translateY(iPoints);
- }
- public boolean isInside(int pX,int pY){
- if(iX1<pX && iX2>pX && iY1<pY && iY2>pY){
- return true;
- }
- else{
- return false;
- }
- }
- public ColorRectangle unionRect(ColorRectangle r){
- int x1=Math.min(iX1,r.iX1);
- int y1=Math.min(iY1,r.iY1);
- int x2=Math.max(iX2,r.iX2);
- int y2=Math.max(iY2,r.iY2);
- long c=this.color&r.color;
- r=new ColorRectangle(x1,y1,x2,y2,c);
- ColorRectangle current=new ColorRectangle(x1,y1,x2,y2,c);
- return current;
- }
- public ColorRectangle unionRect2(ColorRectangle r){
- int x1=Math.max(iX1,r.iX1);
- int y1=Math.max(iY1,r.iY1);
- int x2=Math.min(iX2,r.iX2);
- int y2=Math.min(iY2,r.iY2);
- long c=this.color&r.color;
- ColorRectangle current=new ColorRectangle(x1,y1,x2,y2,c);
- return current;
- }
- public static void main(String[]args){
- ColorRectangle a1=new ColorRectangle(4,4,10,10,14789);
- ColorRectangle a2=new ColorRectangle(2,2,5,5,12548);
- System.out.println("a1:"+a1.toString());
- System.out.println("a2:"+a2);
- if(a1.equals(a2)){
- System.out.println("Areas are equal");
- }
- else{
- System.out.println("Areas are NOT equal");
- }
- ColorRectangle a3=a1.unionRect(a2);
- System.out.println("a3:"+a3);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement