SHARE
TWEET

Untitled

a guest Nov 14th, 2017 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package hr.fer.oop.lab3.prob1;
  2.  
  3. import hr.fer.oop.lab3.pic.Picture;
  4.  
  5. /**
  6.  * @author Bingo
  7.  *
  8.  */
  9.  
  10.  
  11. public class Circle implements Drawable {  
  12.    
  13.     /*
  14.      * Inheriting classes may need access to base values to implement  
  15.      * different drawing algorithms, as well as possibly more complex
  16.      * operations.
  17.      */
  18.     protected double radius;
  19.  
  20.     /**
  21.      * Instantiating with a negative radius is not allowed, the  constructor
  22.      * will throw a java.lang.IllegalArgumentException if called with a
  23.      * negative constructor.  
  24.      *
  25.      * @param radius The circle's radius
  26.      */
  27.     public Circle(double radius) {
  28.         if(radius >=0 ) {
  29.             this.radius = radius;
  30.         }
  31.         else
  32.             throw new IllegalArgumentException("Tried instantiating a negative radius circle");
  33.     }
  34.  
  35.     /**
  36.      * Copy constructor won't perform any checks. The original object is
  37.      * assumed to be consistent (the radius should be non-negative).
  38.      *
  39.      * @param original The original Circle object.
  40.      */
  41.     public Circle(Circle original) {
  42.         this.radius = original.radius;
  43.     }
  44.  
  45.     @Override
  46.     public void drawOnPicture(Picture pic) {
  47.         int cY = pic.getHeight() / 2, cX = pic.getWidth() / 2;
  48.         for( int y = 0; y < pic.getHeight(); y++ ) {
  49.             for(int x = 0; x < pic.getWidth(); x++) {
  50.                 if(Math.pow(x - cX, 2) + Math.pow(y - cY,  2) < Math.pow(radius, 2) ){
  51.                     pic.turnPixelOn(x,  y);
  52.                 }
  53.             }
  54.         }
  55.     }
  56.    
  57.     public double getRadius() {
  58.         return this.radius;
  59.     }
  60.  
  61.    
  62.     /*
  63.      * main method used for simple testing
  64.      */
  65.  
  66.     public static void main(String[] args) {
  67.         int[][] tests = {
  68.                 {5, 5, -1}
  69.                 , {5, 5, 0}
  70.                 , {5, 5, 1}
  71.                 , {5, 5, 2}
  72.                 , {5, 5, 3} // D > pic.get[Height|Width]()
  73.                 , {80, 50, 21}
  74.                 , {80, 50, 22}
  75.                 , {80, 50, 23}
  76.                 , {80, 50, 24}
  77.                 , {80, 50, 25} // D >= pic.getWidth()
  78.                 , {80, 50, 26}
  79.                 , {80, 50, 1}
  80.                 , {80, 50, 0}
  81.         };
  82.  
  83.         for(int[] test : tests) {
  84.             System.out.println(new StringBuilder("Picture(w:")
  85.                     .append(test[0])
  86.                     .append(", h:")
  87.                     .append(test[1])
  88.                     .append("), Circle(r:")
  89.                     .append(test[2])
  90.                     .append("):"));
  91.             try {
  92.                 Picture p = new Picture(test[0], test[1]);
  93.                 Circle c = new Circle(test[2]);
  94.                 c.drawOnPicture(p);
  95.                 p.renderImageToStream(System.out);
  96.             }
  97.             catch(Exception e) {
  98.                 e.printStackTrace();
  99.             }
  100.         }
  101.  
  102.     }
  103.  
  104. }
RAW Paste Data
Top