Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Conditionally Modifying Pixels - Computer Science 1 - Lab | CPSC 1301, Lab Reports of Computer Science

Material Type: Lab; Class: Computer Science 1; Subject: Computer Science; University: Columbus State University; Term: Unknown 1989;

Typology: Lab Reports

Pre 2010

Uploaded on 08/04/2009

koofers-user-df9
koofers-user-df9 🇺🇸

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
LAB 17 – Conditionally Modifying Pixels (part 1 – Replacing Colors)
Topics
To replace one color with another in a picture
To conditionally execute a statement or block of statements using an if statement.
6.1 Conditional Pixel Changes
Type in the following code in the Program window (changing the comments):
import java.awt.Color;
/* Program that transforms pictures using loops and conditionals
*
* Picture and Pixel classes are defined in bookClasses developed
* at Georgia Tech by Mark Guzdial / Barbara Ericson
*
* @author Wayne Summers
* @date Sept. 23, 2007
*/
public class TransformPictures
{
public static void main(String[] args)
{
String fileName1, fileName2;
System.out.println(“Find the source file”);
fileName1 = FileChooser.pickAFile(); // note that this pops up a window to select a jpg file
Picture sourcePicture;
sourcePicture = new Picture(fileName1);
// calls to methods that manipulate images go here
sourcePicture.show();
}
}
6.1.2 Replacing Colors
i) Type in the following code in the Program window below the main method and before the last }:
/**
* Method that turns brown in a source picture into red
* @param sourcePic - picture Object to be modified
*/
public static void turnBrownIntoRed (Picture sourcePic)
{
Color brown = new Color(42, 25, 15);
Pixel[] pixels = sourcePic.getPixels();
Pixel pix= null;
int len = pixels.length;
// loop through the array of pixels in the picture
for (int index = 0; index < len; index++)
{
// get the current pixel
pf3

Partial preview of the text

Download Conditionally Modifying Pixels - Computer Science 1 - Lab | CPSC 1301 and more Lab Reports Computer Science in PDF only on Docsity!

LAB 17 – Conditionally Modifying Pixels (part 1 – Replacing Colors)

Topics  To replace one color with another in a picture  To conditionally execute a statement or block of statements using an if statement.

6.1 Conditional Pixel Changes

Type in the following code in the Program window (changing the comments): import java.awt.Color; /* Program that transforms pictures using loops and conditionals

  • Picture and Pixel classes are defined in bookClasses developed
  • at Georgia Tech by Mark Guzdial / Barbara Ericson
  • @author Wayne Summers
  • @date Sept. 23, 2007 / public class TransformPictures { public static void main(String[] args) { String fileName1, fileName2; System.out.println(“Find the source file”); fileName1 = FileChooser.pickAFile(); // note that this pops up a window to select a jpg file Picture sourcePicture; sourcePicture = new Picture(fileName1); // calls to methods that manipulate images go here sourcePicture.show(); } } 6.1.2 Replacing Colors i) Type in the following code in the Program window below the main method and before the last }: /*
  • Method that turns brown in a source picture into red
  • @param sourcePic - picture Object to be modified */ public static void turnBrownIntoRed (Picture sourcePic) { Color brown = new Color(42, 25, 15); Pixel[] pixels = sourcePic.getPixels(); Pixel pix= null; int len = pixels.length; // loop through the array of pixels in the picture for (int index = 0; index < len; index++) { // get the current pixel

pix = pixels[index]; // if the color is near brown then double the amount of red if (pix.colorDistance(brown) < 50.0) { pix.setColor(new Color( (int) (pix.getRed() * 2.0), pix.getGreen(), pix.getBlue()) ); } //end if } //end loop } ii) Type in the following code in the Program window in the main method and before the pic.show(); turnBrownIntoRed (sourcePicture); iii) Test your program with different pictures that have brown (e.g. Barbara.jpg). 6.1.3 Reducing Redeye iv) Type in the following code in the Program window below the main method and before the last }: /**

  • Method that removes the redeye from a source picture
  • @param sourcePic - picture Object to be modified
  • @param startX – starting x-coordinate of region to be checked
  • @param startY – starting y-coordinate of region to be checked
  • @param endX – ending x-coordinate of region to be checked
  • @param endY – ending y-coordinate of region to be checked
  • @param oldColor – color to be replaced
  • @param newColor – new color to be replaced with
  • @param distance – distance allowed from oldColor before color needs to be changed */ public static void replaceColor (Picture sourcePic, int startX, int startY, int endX, int endY, Color oldColor, Color newColor, double distance) { Pixel pixelObj = null; // loop through the pixels in the rectangle defined by the // startX, startY, and endX and endY for (int x = startX; x < endX; x++) { for (int y = startY; y < endY; y++) { // get the current pixel pixelObj = sourcePic.getPixel(x,y); // if the color is near red then change it if (pixelObj.colorDistance(oldColor) < distance) { pixelObj.setColor(newColor); } //end if } //end inner loop } //end outer loop } v) Type in the following code in the Program window in the main method and before the pic.show();