Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 27.98 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.font.*;
  3. import java.awt.geom.*;
  4. import java.awt.image.BufferedImage;
  5. import java.text.*;
  6. import java.util.*;
  7. import java.util.List; // resolves problem with java.awt.List and java.util.List
  8. import java.io.*;
  9.  
  10. /**
  11. * A class that represents a picture. This class inherits from
  12. * SimplePicture and allows the student to add functionality to
  13. * the Picture class.
  14. *
  15. * @author Barbara Ericson ericson@cc.gatech.edu
  16. */
  17. public class Picture extends SimplePicture
  18. {
  19. ///////////////////// constructors //////////////////////////////////
  20.  
  21. /**
  22. * Constructor that takes no arguments
  23. */
  24. public Picture ()
  25. {
  26. /* not needed but use it to show students the implicit call to super()
  27. * child constructors always call a parent constructor
  28. */
  29. super();
  30. }
  31.  
  32. /**
  33. * Constructor that takes a file name and creates the picture
  34. * @param fileName the name of the file to create the picture from
  35. */
  36. public Picture(String fileName)
  37. {
  38. // let the parent class handle this fileName
  39. super(fileName);
  40. }
  41.  
  42. /**
  43. * Constructor that takes the width and height
  44. * @param height the height of the desired picture
  45. * @param width the width of the desired picture
  46. */
  47. public Picture(int height, int width)
  48. {
  49. // let the parent class handle this width and height
  50. super(width,height);
  51. }
  52.  
  53. /**
  54. * Constructor that takes a picture and creates a
  55. * copy of that picture
  56. * @param copyPicture the picture to copy
  57. */
  58. public Picture(Picture copyPicture)
  59. {
  60. // let the parent class do the copy
  61. super(copyPicture);
  62. }
  63.  
  64. /**
  65. * Constructor that takes a buffered image
  66. * @param image the buffered image to use
  67. */
  68. public Picture(BufferedImage image)
  69. {
  70. super(image);
  71. }
  72.  
  73. ////////////////////// methods ///////////////////////////////////////
  74.  
  75. /**
  76. * Method to return a string with information about this picture.
  77. * @return a string with information about the picture such as fileName,
  78. * height and width.
  79. */
  80. public String toString()
  81. {
  82. String output = "Picture, filename " + getFileName() +
  83. " height " + getHeight()
  84. + " width " + getWidth();
  85. return output;
  86.  
  87. }
  88.  
  89. /** Method to set the blue to 0 */
  90. public void zeroBlue()
  91. {
  92. Pixel[][] pixels = this.getPixels2D();
  93. for (Pixel[] rowArray : pixels)
  94. {
  95. for (Pixel pixelObj : rowArray)
  96. {
  97. pixelObj.setBlue(0);
  98. }
  99. }
  100. }
  101.  
  102. public void keepOnlyBlue()
  103. {
  104. Pixel[][] pixels = this.getPixels2D();
  105. for (Pixel[] rowArray : pixels)
  106. {
  107. for (Pixel pixelObj : rowArray)
  108. {
  109. pixelObj.setRed(0);
  110. pixelObj.setGreen(0);
  111. }
  112. }
  113. }
  114.  
  115. public void keepOnlyGreen()
  116. {
  117. Pixel[][] pixels = this.getPixels2D();
  118. for (Pixel[] rowArray : pixels)
  119. {
  120. for (Pixel pixelObj : rowArray)
  121. {
  122. pixelObj.setBlue(0);
  123. pixelObj.setRed(0);
  124. }
  125. }
  126. }
  127.  
  128. public void keepOnlyRed()
  129. {
  130. Pixel[][] pixels = this.getPixels2D();
  131. for (Pixel[] rowArray : pixels)
  132. {
  133. for (Pixel pixelObj : rowArray)
  134. {
  135. pixelObj.setBlue(0);
  136. pixelObj.setGreen(0);
  137. }
  138. }
  139. }
  140.  
  141. public void negate()
  142. {
  143. Pixel[][] pixels = this.getPixels2D();
  144. for (Pixel[] rowArray : pixels)
  145. {
  146. for (Pixel pixelObj : rowArray)
  147. {
  148. pixelObj.setRed(pixelObj.getRed() - 255);
  149. pixelObj.setGreen(pixelObj.getGreen() - 255);
  150. pixelObj.setBlue(pixelObj.getBlue() - 255);
  151. }
  152. }
  153. }
  154.  
  155. public void grayscale()
  156. {
  157. Pixel[][] pixels = this.getPixels2D();
  158. for (Pixel[] rowArray : pixels)
  159. {
  160. for (Pixel pixelObj : rowArray)
  161. {
  162. int avg = (int)((pixelObj.getRed() + pixelObj.getGreen() + pixelObj.getBlue()) / 3);
  163. pixelObj.setRed(avg);
  164. pixelObj.setBlue(avg);
  165. pixelObj.setGreen(avg);
  166. }
  167. }
  168. }
  169.  
  170. public void invert()
  171. {
  172. Pixel[][] pixels = this.getPixels2D();
  173. for (int row = 0; row < pixels.length; row++)
  174. {
  175. for (int col = 0; col < pixels[0].length; col++)
  176. {
  177. int red = 255 - pixels[row][col].getRed();
  178. int green = 255 - pixels[row][col].getGreen();
  179. int blue = 255 - pixels[row][col].getBlue();
  180.  
  181. Color newColor = new Color(red, green, blue);
  182.  
  183. pixels[row][col].setColor(newColor);
  184. }
  185. }
  186. }
  187.  
  188. public void darken(int amount)
  189. {
  190. Pixel[][] pixels = this.getPixels2D();
  191. for (int row = 0; row < pixels.length; row++)
  192. {
  193. for (int col = 0; col < pixels[0].length; col++)
  194. {
  195. int red = pixels[row][col].getRed() - amount;
  196. int green = pixels[row][col].getGreen() - amount;
  197. int blue = pixels[row][col].getBlue() - amount;
  198.  
  199. Color newColor = new Color(red, green, blue);
  200.  
  201. pixels[row][col].setColor(newColor);
  202. }
  203. }
  204. }
  205.  
  206. /**
  207. * Method to fix the fish
  208. * Takes a sample and adjust the image
  209. * to better see the fish
  210. */
  211.  
  212. public void fixUnderwater()
  213. {
  214. Pixel[][] pixels = this.getPixels2D();
  215.  
  216. int redAverage = 0;
  217. int greenAverage = 0;
  218. int blueAverage = 0;
  219. int totalPixels = 0;
  220.  
  221. int maxRed = 0;
  222. int minRed = 255;
  223. int maxGreen = 0;
  224. int minGreen = 255;
  225. int maxBlue = 0;
  226. int minBlue = 255;
  227.  
  228. // takes a sample from a fish and finds the average color value and range of colors
  229. for (int row = 26; row < 36; row++)
  230. {
  231. for (int col = 178; col < 198; col++)
  232. {
  233. totalPixels++;
  234.  
  235. Pixel myPixel = pixels[row][col];
  236.  
  237. redAverage += myPixel.getRed();
  238. greenAverage += myPixel.getGreen();
  239. blueAverage += myPixel.getBlue();
  240.  
  241. if (myPixel.getRed() > maxRed) { maxRed = myPixel.getRed(); }
  242. if (myPixel.getRed() < minRed) { minRed = myPixel.getRed(); }
  243. if (myPixel.getGreen() > maxGreen) { maxGreen = myPixel.getGreen(); }
  244. if (myPixel.getGreen() < minGreen) { minGreen = myPixel.getGreen(); }
  245. if (myPixel.getBlue() > maxBlue) { maxBlue = myPixel.getBlue(); }
  246. if (myPixel.getGreen() < minBlue) { minBlue = myPixel.getBlue(); }
  247.  
  248. }
  249. }
  250.  
  251. redAverage = redAverage / totalPixels;
  252. greenAverage = greenAverage / totalPixels;
  253. blueAverage = blueAverage / totalPixels;
  254.  
  255. Color averageColor = new Color(redAverage, greenAverage, blueAverage);
  256.  
  257. // calculates the range
  258. int redRange = (maxRed - minRed);
  259. int greenRange = (maxGreen - minGreen);
  260. int blueRange = (maxBlue - minBlue);
  261.  
  262. int redDistance = redRange;
  263. int greenDistance = greenRange;
  264. int blueDistance = blueRange;
  265.  
  266. double maxDistance = Math.sqrt(redDistance * redDistance +
  267. greenDistance * greenDistance +
  268. blueDistance * blueDistance);
  269.  
  270. double tolerance = 1.7; // higher tolerance means more pixels will be identified as "fish"
  271.  
  272. // changes the image based on calculated distance from sample value
  273. for (int row = 0; row < pixels.length; row++) // Pixel[] rowArray : pixels)
  274. {
  275. for (int col = 0; col < pixels[0].length; col++) // Pixel pixelObj : rowArray)
  276. {
  277. Pixel myPixel = pixels[row][col]; //
  278.  
  279. boolean closeEnough = myPixel.colorDistance(averageColor) < maxDistance * tolerance; // stopped here, define this***
  280. // System.out.println(myPixel.colorDistance(averageColor));
  281. if (closeEnough)
  282. {
  283. myPixel.setBlue(myPixel.getBlue() + 50);
  284. }
  285. else
  286. {
  287. myPixel.setBlue(myPixel.getBlue() - 50);
  288. }
  289. }
  290. }
  291. }
  292.  
  293. /** Method that mirrors the picture around a
  294. * vertical mirror in the center of the picture
  295. * from left to right */
  296. public void mirrorVertical()
  297. {
  298. Pixel[][] pixels = this.getPixels2D();
  299. Pixel leftPixel = null;
  300. Pixel rightPixel = null;
  301. int width = pixels[0].length;
  302. for (int row = 0; row < pixels.length; row++)
  303. {
  304. for (int col = 0; col < width / 2; col++)
  305. {
  306. leftPixel = pixels[row][col];
  307. rightPixel = pixels[row][width - 1 - col];
  308. rightPixel.setColor(leftPixel.getColor());
  309. }
  310. }
  311. }
  312.  
  313. public void mirrorVerticalRightToLeft()
  314. {
  315. Pixel[][] pixels = this.getPixels2D();
  316. Pixel leftPixel = null;
  317. Pixel rightPixel = null;
  318. int width = pixels[0].length;
  319. for (int row = 0; row < pixels.length; row++)
  320. {
  321. for (int col = 0; col < width / 2; col++)
  322. {
  323. leftPixel = pixels[row][col];
  324. rightPixel = pixels[row][width - 1 - col];
  325. leftPixel.setColor(rightPixel.getColor());
  326. }
  327. }
  328. }
  329.  
  330. public void mirrorHorizontal()
  331. {
  332. Pixel[][] pixels = this.getPixels2D();
  333. Pixel topPixel = null;
  334. Pixel bottomPixel = null;
  335. int height = pixels.length;
  336. for (int row = 0; row < height; row++)
  337. {
  338. for (int col = 0; col < pixels[0].length; col++)
  339. {
  340. topPixel = pixels[row][col];
  341. bottomPixel = pixels[height - 1 - row][col];
  342. bottomPixel.setColor(topPixel.getColor());
  343. }
  344. }
  345. }
  346.  
  347. public void mirrorHorizontalBottomToTop()
  348. {
  349. Pixel[][] pixels = this.getPixels2D();
  350. Pixel topPixel = null;
  351. Pixel bottomPixel = null;
  352. int height = pixels.length;
  353. for (int row = 0; row < height; row++)
  354. {
  355. for (int col = 0; col < pixels[0].length; col++)
  356. {
  357. topPixel = pixels[row][col];
  358. bottomPixel = pixels[height - 1 - row][col];
  359. topPixel.setColor(bottomPixel.getColor());
  360. }
  361. }
  362. }
  363.  
  364. public void mirrorDiagonal() // mirrors from top right to bottom left
  365. {
  366. Pixel[][] pixels = this.getPixels2D();
  367. Pixel topRightPixel = null;
  368. Pixel bottomLeftPixel = null;
  369. int maxLength;
  370. if (pixels.length < pixels[0].length) { maxLength = pixels.length; }
  371. else {maxLength = pixels[0].length; }
  372.  
  373. for (int row = 0; row < maxLength; row++)
  374. {
  375. for (int col = row; col < maxLength; col++)
  376. {
  377. topRightPixel = pixels[row][col];
  378. bottomLeftPixel = pixels[col][row];
  379. bottomLeftPixel.setColor(topRightPixel.getColor());
  380. }
  381. }
  382. }
  383.  
  384. /** Mirror just part of a picture of a temple */
  385. public void mirrorTemple()
  386. {
  387. int mirrorPoint = 276;
  388. Pixel leftPixel = null;
  389. Pixel rightPixel = null;
  390. int count = 0;
  391. Pixel[][] pixels = this.getPixels2D();
  392.  
  393. // loop through the rows
  394. for (int row = 27; row < 97; row++)
  395. {
  396. // loop from 13 to just before the mirror point
  397. for (int col = 13; col < mirrorPoint; col++)
  398. {
  399. count++;
  400. leftPixel = pixels[row][col];
  401. rightPixel = pixels[row]
  402. [mirrorPoint - col + mirrorPoint];
  403. rightPixel.setColor(leftPixel.getColor());
  404. }
  405. }
  406. System.out.println(count);
  407. }
  408.  
  409. /** Mirrors the arms of the snowman */
  410. public void mirrorArms()
  411. {
  412. int mirrorPoint = 193;
  413. Pixel topPixel = null;
  414. Pixel bottomPixel = null;
  415. Pixel[][] pixels = this.getPixels2D();
  416.  
  417. // Left arm
  418. for (int row = 158; row < mirrorPoint; row++)
  419. {
  420. // loop from 13 to just before the mirror point
  421. for (int col = 103; col < 170; col++)
  422. {
  423. topPixel = pixels[row][col];
  424. bottomPixel = pixels[mirrorPoint - row + mirrorPoint][col];
  425. bottomPixel.setColor(topPixel.getColor());
  426. }
  427. }
  428.  
  429. int mirrorPoint2 = 198;
  430. Pixel topPixel2 = null;
  431. Pixel bottomPixel2 = null;
  432.  
  433. // Right arm
  434. for (int row = 171; row < mirrorPoint2; row++)
  435. {
  436. // loop from 13 to just before the mirror point
  437. for (int col = 239; col < 294; col++)
  438. {
  439. topPixel2 = pixels[row][col];
  440. bottomPixel2 = pixels[mirrorPoint2 - row + mirrorPoint2][col];
  441. bottomPixel2.setColor(topPixel2.getColor());
  442. }
  443. }
  444. }
  445.  
  446. public void mirrorGull()
  447. {
  448. int mirrorPoint = 345;
  449. Pixel rightPixel = null;
  450. Pixel leftPixel = null;
  451. Pixel[][] pixels = this.getPixels2D();
  452.  
  453. // Seagull
  454. for (int row = 235; row < 323; row++)
  455. {
  456. for (int col = 238; col < mirrorPoint; col++)
  457. {
  458. rightPixel = pixels[row][col];
  459. leftPixel = pixels[row][mirrorPoint - col + mirrorPoint/3];
  460. leftPixel.setColor(rightPixel.getColor());
  461. }
  462. }
  463. }
  464.  
  465. /** copy from the passed fromPic to the
  466. * specified startRow and startCol in the
  467. * current picture
  468. * @param fromPic the picture to copy from
  469. * @param startRow the start row to copy to
  470. * @param startCol the start col to copy to
  471. */
  472. public void copy(Picture fromPic,
  473. int startRow, int startCol)
  474. {
  475. Pixel fromPixel = null;
  476. Pixel toPixel = null;
  477. Pixel[][] toPixels = this.getPixels2D();
  478. Pixel[][] fromPixels = fromPic.getPixels2D();
  479. for (int fromRow = 0, toRow = startRow;
  480. fromRow < fromPixels.length &&
  481. toRow < toPixels.length;
  482. fromRow++, toRow++)
  483. {
  484. for (int fromCol = 0, toCol = startCol;
  485. fromCol < fromPixels[0].length &&
  486. toCol < toPixels[0].length;
  487. fromCol++, toCol++)
  488. {
  489. fromPixel = fromPixels[fromRow][fromCol];
  490. toPixel = toPixels[toRow][toCol];
  491. toPixel.setColor(fromPixel.getColor());
  492. System.out.println("Fromrow " + fromRow + " toRow " + toRow + " fromCol" + fromCol + " toCol " + toCol);
  493. }
  494. }
  495. }
  496.  
  497. public void copy2(Picture fromPic, int startRow, int endRow, int startCol, int endCol)
  498. {
  499. Pixel fromPixel = null;
  500. Pixel toPixel = null;
  501. Pixel[][] toPixels = this.getPixels2D();
  502. Pixel[][] fromPixels = fromPic.getPixels2D();
  503. for (int fromRow = 0, toRow = startRow;
  504. fromRow < fromPixels.length &&
  505. toRow < endRow;
  506. fromRow++, toRow++)
  507. {
  508. for (int fromCol = 0, toCol = startCol;
  509. fromCol < fromPixels[0].length &&
  510. toCol < endCol;
  511. fromCol++, toCol++)
  512. {
  513. fromPixel = fromPixels[fromRow][fromCol];
  514. toPixel = toPixels[toRow][toCol];
  515. toPixel.setColor(fromPixel.getColor());
  516. //System.out.println("Fromrow " + fromRow + " toRow " + toRow + " fromCol" + fromCol + " toCol " + toCol);
  517. //System.out.println(fromPixels[0].length);
  518. }
  519. }
  520. }
  521.  
  522. /** Method to create a collage of several pictures */
  523. public void createCollage()
  524. {
  525. Picture flower1 = new Picture("flower1.jpg");
  526. Picture flower2 = new Picture("flower2.jpg");
  527.  
  528. //this.copy(flower1,100,0);
  529. this.copy2(flower1,0,100, 0, 100);
  530. //flower1.explore();
  531. //this.copy(flower1,200,0);
  532. // Mirroring
  533. int mirrorPoint = 98;
  534. Pixel rightPixel = null;
  535. Pixel leftPixel = null;
  536. Pixel[][] pixels = this.getPixels2D();
  537. for (int row = 0; row < 98; row++)
  538. {
  539. for (int col = 0; col < 88; col++)
  540. {
  541. rightPixel = pixels[row][col];
  542. leftPixel = pixels[mirrorPoint - row + mirrorPoint][col];
  543. leftPixel.setColor(rightPixel.getColor());
  544. }
  545. }
  546. Picture flowerNoBlue = new Picture(flower2);
  547. flowerNoBlue.zeroBlue();
  548. this.copy2(flowerNoBlue,300,350,80,500);
  549.  
  550. Picture flowerinverse = new Picture(flower2);
  551. flowerinverse.invert();
  552. this.copy2(flowerinverse, 100, 300, 80, 300);
  553. //this.copy(flowerNoBlue,300,0);
  554. //this.copy(flower1,400,0);
  555. //this.copy(flower2,500,0);
  556. //this.mirrorVertical();
  557.  
  558.  
  559. this.write("collage.jpg");
  560. }
  561.  
  562. public void myCollage()
  563. {
  564. Picture flower1 = new Picture("flower1.jpg");
  565. this.copy2(flower1,10,20, 0, 100);
  566. this.write("mycollage.jpg");
  567. }
  568.  
  569. /** Method to show large changes in color
  570. * @param edgeDist the distance for finding edges
  571. */
  572. public void edgeDetection(int edgeDist)
  573. {
  574. Pixel leftPixel = null;
  575. Pixel rightPixel = null;
  576. Pixel topPixel = null;
  577. Pixel bottomPixel = null;
  578.  
  579. Pixel[][] pixels = this.getPixels2D();
  580. for (int row = 0; row < pixels.length - 1; row++)
  581. {
  582. for (int col = 0;
  583. col < pixels[0].length-1; col++)
  584. {
  585. leftPixel = pixels[row][col];
  586. rightPixel = pixels[row][col+1];
  587. topPixel = pixels[row][col];
  588. bottomPixel = pixels[row + 1][col];
  589. if (leftPixel.colorDistance(rightPixel.getColor()) > edgeDist ||
  590. topPixel.colorDistance(bottomPixel.getColor()) > edgeDist)
  591. leftPixel.setColor(Color.BLACK);
  592. else
  593. leftPixel.setColor(Color.WHITE);
  594. }
  595. }
  596. }
  597.  
  598. /**
  599. * Method for edge detection, my way.
  600. * Handy methods used in this method are below.
  601. *
  602. *
  603. *
  604. */
  605.  
  606. public void edgeDetection2(int edgeDist)
  607. {
  608. Pixel currentPixel = null, testPixel = null;
  609.  
  610. // width and height of pixel cluster
  611. int testWidth = 3;
  612. int testHeight = 3;
  613.  
  614. Pixel[][] pixels = this.getPixels2D();
  615. double[][] edgeAngle = new double[Math.round(pixels.length / testHeight)][Math.round(pixels[0].length / testWidth)];
  616. // the 2D array edgeAngle will store the perceived angle (in radians) of the edge
  617.  
  618. for (int row = 0; row < pixels.length - testHeight; row += testHeight)
  619. {
  620. for (int col = 0; col < pixels[0].length - testWidth; col += testWidth)
  621. {
  622. Pixel[][] currentPixels = this.getPixelCluster(pixels, row, col, testWidth, testHeight);
  623.  
  624. double greatestDistance = -10;
  625. double greatestAngle = -1; // not actually the greatest value, but corresponding to greatest distance
  626. // I may need to determin which of the two partial clusters is darker, for filling in purposes
  627. for (double angle = 0; angle < Math.PI + 0.1; angle += Math.PI / 8)
  628. {
  629. ArrayList<Pixel> group1 = this.getPartialArray(currentPixels, angle, 0);
  630. Color group1Color = this.getAverageColor(this.getPixelColors(group1));
  631. ArrayList<Pixel> group2 = this.getPartialArray(currentPixels, angle, 1);
  632. Color group2Color = this.getAverageColor(this.getPixelColors(group2));
  633.  
  634. double currentColorDistance = this.colorDistance(group1Color, group2Color);
  635.  
  636. if (currentColorDistance > greatestDistance)
  637. {
  638. greatestDistance = currentColorDistance;
  639. greatestAngle = angle;
  640. }
  641.  
  642. }
  643.  
  644. greatestAngle = Math.round(greatestAngle * 100.0) / 100.0;
  645. edgeAngle[row / testHeight][col / testWidth] = greatestAngle;
  646.  
  647.  
  648. if (greatestDistance > edgeDist)
  649. {
  650. ArrayList<Pixel> group1 = this.getPartialArray(currentPixels, greatestAngle, 0);
  651. ArrayList<Pixel> group2 = this.getPartialArray(currentPixels, greatestAngle, 1);
  652.  
  653. for (Pixel pixel : group1)
  654. {
  655. pixel.setColor(Color.BLACK);
  656. }
  657.  
  658. for (Pixel pixel : group2)
  659. {
  660. pixel.setColor(Color.WHITE);
  661. }
  662. }
  663. else
  664. {
  665. for (Pixel[] pixelArray : currentPixels)
  666. {
  667. for (Pixel pixel : pixelArray)
  668. {
  669. pixel.setColor(Color.WHITE);
  670. }
  671. }
  672. }
  673. }
  674. }
  675.  
  676. File file = new File("outputAngles.txt");
  677. try{
  678. PrintWriter writer = new PrintWriter(file, "UTF-8");
  679.  
  680.  
  681. for (int row = 0; row < edgeAngle.length; row++)
  682. {
  683. for (int col = 0; col < edgeAngle[0].length; col++)
  684. {
  685. writer.print(edgeAngle[row][col]);
  686. writer.print(" ");
  687. }
  688. writer.print("\n");
  689. }
  690. writer.close();
  691. }
  692. catch(Exception e){ e.printStackTrace(); }
  693. }
  694.  
  695. public double colorDistance(Color testColor1, Color testColor2)
  696. {
  697. double redDistance = testColor2.getRed() - testColor1.getRed();
  698. double greenDistance = testColor2.getGreen() - testColor1.getGreen();
  699. double blueDistance = testColor2.getBlue() - testColor1.getBlue();
  700. double distance = Math.sqrt(redDistance * redDistance +
  701. greenDistance * greenDistance +
  702. blueDistance * blueDistance);
  703. return distance;
  704. }
  705.  
  706. public Color getAverageColor(Color[] myColors)
  707. {
  708. int totalRed = 0;
  709. int totalGreen = 0;
  710. int totalBlue = 0;
  711.  
  712. int total = 0;
  713.  
  714. for (Color currentColor : myColors)
  715. {
  716. totalRed += currentColor.getRed();
  717. totalGreen += currentColor.getGreen();
  718. totalBlue += currentColor.getBlue();
  719. total++;
  720. }
  721.  
  722. return new Color(totalRed / total, totalGreen / total, totalBlue / total);
  723.  
  724. }
  725.  
  726. public Color[] getPixelColors(ArrayList<Pixel> pixels)
  727. {
  728. Color[] myColors = new Color[pixels.size()];
  729. int count = 0;
  730. for (Pixel currentPixel : pixels)
  731. {
  732. myColors[count] = currentPixel.getColor();
  733. count++;
  734. }
  735.  
  736. return myColors;
  737. }
  738.  
  739. public Pixel[][] getPixelCluster(Pixel[][] pixels, int startRow, int startCol,
  740. int width, int height)
  741. {
  742. Pixel[][] pixelCluster = new Pixel[height][width];
  743.  
  744. if (pixels.length < startRow + height || pixels[0].length < startCol + width)
  745. {
  746. return pixelCluster;
  747. }
  748.  
  749. for (int row = startRow; row < startRow + height; row++)
  750. {
  751. for (int col = startCol; col < startCol + width; col++)
  752. {
  753. pixelCluster[row - startRow][col - startCol] = pixels[row][col];
  754. }
  755. }
  756.  
  757. return pixelCluster;
  758. }
  759.  
  760. /**
  761. * Method getPartialArray takes an array of pixels,
  762. * an angle to divide the array, and the "type of" that
  763. * determines whether it returns the top/right (0) or
  764. * the bottom/left (1)
  765. *
  766. * This one only takes the elements that lie on the line of division
  767. *
  768. * Need to update this method to match the one below ********
  769. *
  770. * @param pixels
  771. * @param angle the angle to divide, given in radians (0 to pi)
  772. */
  773. public ArrayList<Pixel> getPartialArrayLine(Pixel[][] fullArray, double angle, int typeOf)
  774. {
  775. int rows = fullArray.length, cols = fullArray[0].length;
  776. int centerRow = rows / 2, centerCol = cols / 2;
  777. int arrayLength = (rows * cols);
  778. ArrayList<Pixel> tempList = new ArrayList<Pixel>();
  779.  
  780. double slope = Math.tan(angle);
  781. if (slope == 0) slope = 0.001;
  782. double newSlope = -1 / slope;
  783.  
  784. for (int i = 0; i < arrayLength; i++)
  785. {
  786. int currentRow = i / cols, currentCol = i % cols;
  787. //System.out.println(i + "\t" + currentRow + " " + currentCol);
  788. if (currentCol == (newSlope * (currentRow - centerRow) + centerCol))
  789. // this tests whether the current cell is above/below the "line"
  790. {
  791. if (typeOf == 1)
  792. {
  793. tempList.add(fullArray[currentRow][currentCol]);
  794. //System.out.println("added " + i);
  795. }
  796. }
  797. else
  798. {
  799. if (typeOf == 0)
  800. {
  801. tempList.add(fullArray[currentRow][currentCol]);
  802. //System.out.println("added " + i);
  803. }
  804. }
  805. }
  806.  
  807. return tempList;
  808. }
  809.  
  810.  
  811. /**
  812. * Method getPartialArray takes an array of pixels,
  813. * an angle to divide the array, and the "type of" that
  814. * determines whether it returns the top/right (0) or
  815. * the bottom/left (1)
  816. *
  817. * Need to update this method to match the one below ********
  818. *
  819. * @param pixels
  820. * @param angle the angle to divide, given in radians (0 to pi)
  821. */
  822. public ArrayList<Pixel> getPartialArray(Pixel[][] fullArray, double angle, int typeOf)
  823. {
  824. int rows = fullArray.length, cols = fullArray[0].length;
  825. int centerRow = rows / 2, centerCol = cols / 2;
  826. int arrayLength = (rows * cols);
  827. ArrayList<Pixel> tempList = new ArrayList<Pixel>();
  828.  
  829. double slope = Math.tan(angle);
  830. if (slope == 0) slope = 0.001;
  831. double newSlope = -1 / slope;
  832.  
  833. for (int i = 0; i < arrayLength; i++)
  834. {
  835. int currentRow = i / cols, currentCol = i % cols;
  836. //System.out.println(i + "\t" + currentRow + " " + currentCol);
  837. if (currentCol < (newSlope * (currentRow - centerRow) + centerCol))
  838. // this tests whether the current cell is above/below the "line"
  839. {
  840. if (typeOf == 1)
  841. {
  842. tempList.add(fullArray[currentRow][currentCol]);
  843. //System.out.println("added " + i);
  844. }
  845. }
  846. else
  847. {
  848. if (typeOf == 0)
  849. {
  850. tempList.add(fullArray[currentRow][currentCol]);
  851. //System.out.println("added " + i);
  852. }
  853. }
  854. }
  855.  
  856. return tempList;
  857. }
  858.  
  859. public static ArrayList<Integer> getPartialArray(int[][] fullArray, double angle, int typeOf)
  860. {
  861. int rows = fullArray.length, cols = fullArray[0].length;
  862. int centerRow = rows / 2, centerCol = cols / 2;
  863. int arrayLength = (rows * cols);
  864. ArrayList<Integer> tempList = new ArrayList<Integer>();
  865.  
  866. double slope = Math.tan(angle);
  867. if (slope == 0) slope = 0.001;
  868. double newSlope = -1 / slope;
  869.  
  870. for (int i = 0; i < arrayLength; i++)
  871. {
  872. int currentRow = i / cols, currentCol = i % cols;
  873. System.out.println(i + "\t" + currentRow + " " + currentCol);
  874. if (currentCol < (newSlope * (currentRow - centerRow) + centerCol))
  875. {
  876. if (typeOf == 1)
  877. {
  878. tempList.add(fullArray[currentRow][currentCol]);
  879. System.out.println("added " + i);
  880. }
  881. }
  882. else
  883. {
  884. if (typeOf == 0)
  885. {
  886.  
  887. tempList.add(fullArray[currentRow][currentCol]);
  888. System.out.println("added " + i);
  889. }
  890. }
  891. }
  892.  
  893. return tempList;
  894. }
  895.  
  896.  
  897. /* Main method for testing - each class in Java can have a main
  898. * method
  899. */
  900. public static void main(String[] args)
  901. {
  902. Picture beach = new Picture("caterpillar.jpg");
  903. beach.explore();
  904. beach.zeroBlue();
  905. beach.explore();
  906. }
  907.  
  908. } // this } is the end of class Picture, put all new methods before this
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement