Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.29 KB | None | 0 0
  1. COLOR
  2.  
  3.  
  4. public class Color implements Comparable
  5.     {
  6. final static int BYTE_MASK=255;
  7. private long red,green,blue;
  8. protected long color;
  9. public void rgb2color()
  10. {
  11.     color=(red<<16)|(green<<8)|(blue);
  12. }
  13. public void color2rgb()
  14. {
  15.     red=color>>16;
  16.     green=(color>>8)&BYTE_MASK;
  17.     blue=color&BYTE_MASK;
  18.  
  19. }
  20. public Color()
  21. {
  22.    
  23. }
  24. public Color(long c)
  25.     {
  26.     this.color=c;
  27.     color2rgb();
  28.     }
  29. public long getRed()
  30.     {
  31.     return red;
  32.     }
  33. public void setRed(long r)
  34.     {
  35.     red=r;
  36.     rgb2color();
  37.     }
  38. public long getGreen()
  39. {
  40. return green;
  41. }
  42. public void setGreen(long v)
  43. {
  44.     green=v;
  45. rgb2color();
  46. }
  47. public long getBlue()
  48. {
  49. return blue;
  50. }
  51. public void setBlue(long m)
  52. {
  53.     blue=m;
  54. rgb2color();
  55. }
  56. public String toString()
  57. {
  58.     return "Red: "+red+",Green:"+green+ ", Blue:" +blue+", Color:"+color;
  59. }
  60.     public boolean equals(Object r){
  61.         return this.color==((Color)r).color;
  62.     }
  63.     public int compareTo(Object c)
  64.     {
  65.         if (this.color<((Color)c).color)
  66.             return -1;
  67.         if (this.color>((Color)c).color)
  68.             return 1;
  69.         return 0;
  70.     }
  71.    
  72.     }
  73.  
  74.  
  75. COLOR RECTANGLE
  76. public class ColorRectangle extends Color implements Comparable
  77. {private int iX1,iY1,iX2,iY2;
  78. public ColorRectangle(){}
  79. public ColorRectangle(int x1,int y1,int x2,int y2,long c){
  80.     super(c);
  81.     iX1=x1;
  82.     this.iY1=y1;
  83.     this.iX2=x2;
  84.     this.iY2=y2;
  85. }
  86. public int getIX1(){
  87.     return iX1;
  88. }
  89. public int getIY1(){
  90.     return iY1;
  91. }
  92. public int getIx2(){
  93.     return iX2;
  94. }
  95. public int getIY2(){
  96.     return iY2;
  97. }
  98. public int calcArea(){
  99.     return Math.abs((iX2-iX1)*(iY2-iY1));
  100. }
  101. public int compareTo(Object r){
  102.     if(this.calcArea()<((ColorRectangle)r).calcArea())
  103.         return -1;
  104.     if(this.calcArea()>((ColorRectangle)r).calcArea())
  105.         return 1;
  106.     return 0;
  107. }
  108. public String toString(){
  109.     return "x1:"+iX1+",Y1:"+iY1+",x2:"+iX2+",Y2:"+iY2+"Color:"+super.toString();
  110. }
  111.  
  112. public boolean equals(ColorRectangle r){
  113.     return(this.calcArea()==r.calcArea()&&this.equals(r));
  114. }
  115. public void translateX(int iPoints){
  116.     iX1=iX1+iPoints;
  117.     iX2+=iPoints;
  118. }
  119. public void translateY(int iPoints){
  120.     iY1=iY1+iPoints;
  121.     iY2+=iPoints;
  122. }
  123. public void translateXY(int iPoints){
  124.     translateX(iPoints);
  125.     translateY(iPoints);
  126. }
  127. public boolean isInside(int pX,int pY){
  128.     if(iX1<pX && iX2>pX && iY1<pY && iY2>pY){
  129.         return true;
  130.     }
  131.     else{
  132.         return false;
  133.     }
  134. }
  135. public ColorRectangle unionRect(ColorRectangle r){
  136.     int x1=Math.min(iX1,r.iX1);
  137.     int y1=Math.min(iY1,r.iY1);
  138.     int x2=Math.max(iX2,r.iX2);
  139.     int y2=Math.max(iY2,r.iY2);
  140.     long c=this.color&r.color;
  141.     r=new ColorRectangle(x1,y1,x2,y2,c);
  142.    
  143.     ColorRectangle current=new ColorRectangle(x1,y1,x2,y2,c);
  144.     return current;
  145. }
  146. public ColorRectangle unionRect2(ColorRectangle r){
  147.     int x1=Math.max(iX1,r.iX1);
  148.     int y1=Math.max(iY1,r.iY1);
  149.     int x2=Math.min(iX2,r.iX2);
  150.     int y2=Math.min(iY2,r.iY2);
  151.     long c=this.color&r.color;
  152.     ColorRectangle current=new ColorRectangle(x1,y1,x2,y2,c);
  153.     return current;
  154. }
  155. public static void main(String[]args){
  156.     ColorRectangle a1=new ColorRectangle(4,4,10,10,14789);
  157.     ColorRectangle a2=new ColorRectangle(2,2,5,5,12548);
  158.     System.out.println("a1:"+a1.toString());
  159.     System.out.println("a2:"+a2);
  160.     if(a1.equals(a2)){
  161.         System.out.println("Areas are equal");
  162.     }
  163.     else{
  164.         System.out.println("Areas are NOT equal");
  165.     }
  166.     ColorRectangle a3=a1.unionRect(a2);
  167.     System.out.println("a3:"+a3);
  168. }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement