Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.83 KB | None | 0 0
  1. // http://sit.tu-varna.bg/wp-content/uploads/2018/04/OOP2/project02.pdf
  2. // I)
  3. package color;
  4.  
  5. public class color implements Comparable<Object>
  6. {
  7. private static long BYTE_MASK = 255; //konstanti s glavni bukvi
  8. private long red,green,blue,rgb;
  9. // 24 bitovo chislo (xxxxxxxxR xxxxxxxxG xxxxxxxxB ---- 256*256*R + 256*G + 1*B
  10. // R,G,B e [0,255]       rgb e [0 ~ 2(na 24-1)]
  11.  
  12. public void rgb2color()
  13. {             // 16 bita na lqvo << 8 bita na lqvo
  14.     rgb = red << 16 | green << 8 | << blue;
  15.     // ||-logicheski operacii  |-po bitovi operacii
  16. }   // &&-logicheski operacii  &-po bitovi operacii
  17.  
  18. public void color2rgb()
  19. {
  20.     red = rgb >> 16;
  21.     green = (rgb >> 8) & BYTE_MASK;
  22.     blue = rgb & BYTE_MASK;
  23. }
  24.  
  25. // 1)
  26. public color() {}
  27.  
  28. // 2)
  29. public color(long c)
  30. {
  31.     this.rgb=c;
  32.     color2rgb();
  33. }
  34.  
  35. // 4) nqma 3 po uslovie
  36. public long getRed()
  37. {
  38.     return red;
  39. }
  40.  
  41. public void setRed(long r)
  42. {
  43.     red = r;
  44.     rgb2color();
  45. }
  46.  
  47. public long getGreen()
  48. {
  49.     return green;
  50. }
  51.  
  52. public void setGreen(long g)
  53. {
  54.     green = g;
  55.     rgb2color();
  56. }
  57.  
  58. public long getBlue()
  59. {
  60.     return blue;
  61. }
  62.  
  63. public void setBlue(long b)
  64. {
  65.     blue = b;
  66.     rgb2color();
  67. }
  68.  
  69. // 5)
  70. public String toString()
  71. {
  72.     return "Red: " + red + ", Green: " + green + ", Blue: " + blue + ", RGB: " + rgb;
  73. }
  74.  
  75. // 6)
  76. public boolean equals(Object r)
  77. {
  78.     return this.rgb == ((color)r).rgb;
  79. }
  80.  
  81. // 7)
  82. public int compareTo (Object c)
  83. {
  84.     if (this.rgb < ((color)c).rgb) return -1;
  85.     if (this.rgb > ((color)c).rgb) return 1;
  86.     return 0; // <- sukratena forma na else.  //else return 0;
  87. }
  88. }
  89. -----------------------------------------------------------------------------------------------------
  90. -----------------------------------------------------------------------------------------------------
  91. -----------------------------------------------------------------------------------------------------
  92. // http://sit.tu-varna.bg/wp-content/uploads/2018/04/OOP2/project02.pdf
  93. // II) Izpolzvame Source koda ot uprajnenie 1 i pravim malki promeni po nego
  94. package color;
  95.  
  96. public class ColorRec extends color implements Comparable<Object>
  97. {
  98.     // http://sit.tu-varna.bg/wp-content/uploads/2018/04/OOP2/project01.pdf
  99.     private int ix1, iy1, ix2, iy2;
  100.     // 1)
  101.     public ColorRec() {}
  102.      
  103.     // 2)                                       //dobavqme long c
  104.     public ColorRec (int x1, int y1, int x2, int y2, long c)
  105.     {
  106.         // dobavqme super(c);
  107.         super(c);
  108.         ix1 = x1;
  109.         this.iy1 = y1;
  110.         this.ix2=x2;
  111.         this.iy2=y2;
  112.     }
  113.     // dobavqme void set-ove
  114.     public void setix1(int x)
  115.     {
  116.     ix1=x; 
  117.     }
  118.    
  119.     public void setix2(int x)
  120.     {
  121.     ix2=x; 
  122.     }
  123.    
  124.     public void setiy1(int y)
  125.     {
  126.     ix1=y; 
  127.     }
  128.    
  129.     public void setiy2(int y)
  130.     {
  131.     ix2=y; 
  132.     }
  133.    
  134.     // 3)
  135.     public int getix1()
  136.     {
  137.         return ix1;
  138.     }
  139.      
  140.     public int getiy1()
  141.     {
  142.         return iy1;
  143.     }
  144.      
  145.     public int getix2()
  146.     {
  147.         return ix2;
  148.     }
  149.      
  150.     public int getiy2()
  151.     {
  152.         return iy2;
  153.     }
  154.      
  155.     public int calcArea()
  156.     {
  157.         return Math.abs((ix2-ix1)*(iy2-iy1));
  158.     }
  159.      
  160.     public int compareTo(Object r)
  161.     {
  162.         if (this.calcArea() > ((ColorRec)r).calcArea()) return 1;
  163.         if (this.calcArea() < ((ColorRec)r).calcArea()) return -1;
  164.         return 0; // if(this.calcArea() = ((ColorRec)r).calcArea()) return 0;
  165.     }
  166.      
  167.     public String toString()
  168.     {  // 7) dobavqme super.toString();
  169.         return "ix1: " + ix1 + ", iy1: " + iy1 + " ,ix2: " + ix2 + " , iy2: " + iy2 + super.toString();
  170.     }
  171.     // 8)
  172.     public boolean equals(ColorRec r)
  173.     {
  174.         return this.calcArea() == r.calcArea() && super.equals(r);
  175.     }
  176.    
  177.     public void translatex(int iPoints)
  178.     {
  179.         ix1 = ix1 + iPoints;
  180.         // same as above -> ix2 += iPoints;
  181.     }
  182.      
  183.     public void translatey(int iPoints)
  184.     {
  185.         iy1 = iy1 + iPoints;
  186.         // same as above -> iy2 += iPoints;
  187.     }
  188.      
  189.     public void translatexy(int iPoints)
  190.     {
  191.         translatex(iPoints);
  192.         translatey(iPoints);
  193.     }
  194.      
  195.     public boolean isInside(int ptx, int pty)
  196.     {
  197.         return (ix1 < ptx && ix2 > ptx && iy1 < pty && iy2 > pty);
  198.     }
  199.      
  200.     // Dolen lqv ugul -> po-malki stoinosti // Goren desen ugul -> po-golemi stoinosti
  201.     public ColorRec unionRect(ColorRec r)
  202.     { // dobavqme long c, i v return-a dobavqme c
  203.         int x1 = Math.min(ix1, r.ix1);
  204.         int y1 = Math.min(iy1, r.iy1);
  205.         int x2 = Math.max(ix2, r.ix2);
  206.         int y2 = Math.max(iy2, r.iy2);
  207.         long c = this.getRgb() | r.getRgb();
  208.         return new ColorRec(x1,y1,x2,y2,c);
  209.      
  210.         // int x1 = ix1 < r.ix1 ? ix1 : r.ix1; // Ternalen Zapis ! // uslovie - istina - luja
  211.     }
  212.      
  213.     public ColorRec intersectionRect(ColorRec r)
  214.     { // dobavqme long c, i v return-a dobavqme c
  215.         int x1 = Math.max(ix1, r.ix1);
  216.         int y1 = Math.max(iy1, r.iy1);
  217.         int x2 = Math.min(ix2, r.ix2);
  218.         int y2 = Math.min(iy2, r.iy2);
  219.         long c = this.getRgb() | r.getRgb();
  220.         return new ColorRec(x1,y1,x2,y2,c);
  221.     }
  222.      
  223.     // Main
  224.     public static void main (String[] args)
  225.     {
  226.        
  227.     //  --------------- dobavqme 5-to chislo
  228.         ColorRec r1 = new ColorRec(1,1,6,6,825896);
  229.         System.out.println(r1.toString());
  230.         ColorRec r2 = new ColorRec (5,5,12,12,15);
  231.         System.out.println(r2.toString());
  232.     //  -------------------------------------------
  233.         r1.translatexy(10);
  234.         System.out.println(r1.toString());
  235.     // --------------------------------------------
  236.         if (r1.equals(r2))
  237.         {
  238.             System.out.println("Areas are equal");
  239.         }
  240.        
  241.         else
  242.         {
  243.             System.out.println("Areas are different");
  244.         }
  245.     // ---------------------------------------------
  246.         if(r1.compareTo(r2)==-1)
  247.         {
  248.             System.out.println("r1 < r2");
  249.         }
  250.        
  251.         if(r1.compareTo(r2)==1)
  252.         {
  253.             System.out.println("r1 > r2");
  254.         }
  255.        
  256.         else
  257.         {
  258.             System.out.println("r1 = r2");
  259.         }
  260.     // ----------------------------------------------
  261.         ColorRec r3=r1.unionRect(r2);
  262.         System.out.println(r3.toString());
  263.     }
  264.     // ----------------------------------------------
  265.    
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement