import java.awt.*; import java.awt.font.*; import java.awt.geom.*; import java.t
ID: 3859738 • Letter: I
Question
import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.text.*;
/**
* A class that represents a picture. This class inherits from
* SimplePicture and allows the student to add functionality to
* the Picture class.
*
* Copyright Georgia Institute of Technology 2004-2005
* @author Barbara Ericson ericson@cc.gatech.edu
*/
public class Picture extends SimplePicture
{
///////////////////// constructors //////////////////////////////////
/**
* Constructor that takes no arguments
*/
public Picture ()
{
/* not needed but use it to show students the implicit call to super()
* child constructors always call a parent constructor
*/
super();
}
/**
* Constructor that takes a file name and creates the picture
* @param fileName the name of the file to create the picture from
*/
public Picture(String fileName)
{
// let the parent class handle this fileName
super(fileName);
}
/**
* Constructor that takes the width and height
* @param width the width of the desired picture
* @param height the height of the desired picture
*/
public Picture(int width, int height)
{
// let the parent class handle this width and height
super(width,height);
}
/**
* Constructor that takes a picture and creates a
* copy of that picture
*/
public Picture(Picture copyPicture)
{
// let the parent class do the copy
super(copyPicture);
}
////////////////////// methods ///////////////////////////////////////
/**
* Method to return a string with information about this picture.
* @return a string with information about the picture such as fileName,
* height and width.
*/
public String toString()
{
String output = "Picture, filename " + getFileName() +
" height " + getHeight()
+ " width " + getWidth();
return output;
}
} // end of class Picture, put all new methods before this
Add a static main method to the Picture class, and inject code into it that demonstrates the operation of the all of the methods that you subsequently add to the Picture class, as specified below.
Add the increaseRed method shown in the book.
Overload the increaseRed method with an increaseRed method that accepts a number that specifies an increase factor.
Add an increaseGreen method.
Overload the increaseGreen method with an increaseGreen method that accepts a number that specifies an increase factor.
Add an increaseBlue method.
Overload the increaseBlue method with an increaseBlue method that accepts a number that specifies an increase factor.
Add a blurr method that blurrs within a specified rectangle. You should have x, y, width, and height arguments, where the x and y arguments represent the upper-left point of origin.
Add another method of your own devising, that does something interesting.
Be sure to remove any unnecessarily duplicated code that you may have added.
Submit the Picture.java file, along with the jpeg picture that you used. Submit these two files together as a single zipped attachment.
Explanation / Answer
The required code for the Picture.java is as follows. I have done all the required functionalities in the program. Hope it will help to understand the code.
PictureClass.java
import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.text.*;
public class PictureClass extends SimplePicture
{
public PictureClass ()
{
super();
}
public PictureClass(String fName)
{
super(fName);
}
public PictureClass(int wid, int ht)
{
super(wid,ht);
}
public PictureClass(PictureClass copyPic)
{
super(copyPic);
}
public String toString()
{
String op = "PictureClass, filename " + getFileName() +
" ht " + getHeight()
+ " wid " + getWidth();
return op;
}
public void cpKat()
{
String srcFl =
FileChooser.getMediaPath("img1.jpg");
PictureClass srcPic = new PictureClass(srcFl);
Pixel srcPix = null;
Pixel trgtPix = null;
for (int srcX = 0, targetX = 0;
srcX < srcPic.getWidth();
srcX++, targetX++)
{
for (int srcY = 0, targY = 0;
srcY < srcPic.getHeight();
srcY++, targY++)
{
srcPix = srcPic.getPixel(srcX,srcY);
trgtPix = this.getPixel(targetX,targY);
trgtPix.setColor(srcPix.getColor());
}
}
}
public void cpyKat2()
{
PictureClass katPic = new
PictureClass(FileChooser.getMediaPath("img1.jpg"));
this.cpy(katPic,0,0,katPic.getWidth(),katPic.getHeight(),100,100);
}
public void cpyFace()
{
PictureClass katPic = new
PictureClass(FileChooser.getMediaPath("img1.jpg"));
this.cpy(katPic,70,3,135,80,100,100);
}
public void cpy(PictureClass srcPic, int startX, int startY,
int eofX, int eofY, int targX, int targY)
{
Pixel srcPix = null;
Pixel trgtPix = null;
for (int io = startX, ut = targX;
io < eofX;
io++, ut++)
{
for (int qq = startY, ty = targY;
qq < eofY;
qq++, ty++)
{
srcPix = srcPic.getPixel(io,qq);
trgtPix = this.getPixel(ut,ty);
trgtPix.setColor(srcPix.getColor());
}
}
}
public void clrBlue()
{
Pixel[] pixArr = this.getPixels();
Pixel pix = null;
int indices = 0;
// loop through all the pixels
while (indices < pixArr.length)
{
// get the current pix
pix = pixArr[indices];
// set the blue on the pix to 0
pix.setBlue(0);
indices++;
}
}
public void clrRed()
{
Pixel[] pixArr = this.getPixels();
Pixel pix = null;
int indice = 0;
while (indice < pixArr.length)
{
pix = pixArr[indice];
pix.setRed(0);
indice++;
}
}
public void mirrHoriz()
{
int pt = this.getHeight() / 2;
Pixel pixe = null;
Pixel btmPix = null;
for (int qq = 0; qq < pt; qq++)
{
for (int io = 0; io < this.getWidth(); io++)
{
pixe = this.getPixel(io, qq);
btmPix = this.getPixel( io, this.getHeight() - 1 -qq);
btmPix.setColor(pixe.getColor());
}
}
}
public void crColl()
{
PictureClass pic = new PictureClass (FileChooser.getMediaPath("JB.jpg"));
this.cpy(pic,0,0,pic.getWidth(),pic.getHeight(),0,0);
pic.clrBlue();
this.pos();
PictureClass pic1 =
new PictureClass (FileChooser.getMediaPath("JB and Jaz1.jpg"));
this.cpy(pic1,0,0,pic1.getWidth(),pic1.getHeight(), 250,0);
PictureClass pic2 =
new PictureClass (FileChooser.getMediaPath("jordy1.jpg"));
this.cpy(pic2,0,0,pic2.getWidth(),pic2.getHeight(),636,0);
this.mirrHoriz();
}
public void gray()
{
Pixel[] pixArr = this.getPixels();
Pixel pix = null;
int lum = 0;
double redVal = 0;
double greenVal = 0;
double blueVal = 0;
for (int uu = 0; uu < pixArr.length; uu++)
{
pix = pixArr[uu];
redVal = pix.getRed() * 0.299;
greenVal = pix.getGreen() * 0.587;
blueVal = pix.getBlue() * 0.114;
lum = (int) (redVal + greenVal + blueVal);
pix.setColor(new Color(lum,lum,lum));
}
}
public void sepia()
{
Pixel pixie = null;
double redVal = 0;
double greenVal = 0;
double blueVal = 0;
for (int io = 0; io < this.getWidth(); io++)
{
for (int qq = 0; qq < this.getHeight(); qq++)
{
// get the current pix and color values
pixie = this.getPixel(io,qq);
redVal = pixie.getRed();
greenVal = pixie.getGreen();
blueVal = pixie.getBlue();
if (redVal < 60)
{
redVal = redVal * 0.9;
greenVal = greenVal * 0.9;
blueVal = blueVal * 0.9;
}
else if (redVal < 190)
{
blueVal = blueVal * 0.8;
}
else
{
blueVal = blueVal * 0.9;
}
pixie.setRed((int) redVal);
pixie.setGreen((int) greenVal);
pixie.setBlue((int) blueVal);
}
}
}
public void pos()
{
Pixel pixie = null;
double redVal = 0;
double greenVal = 0;
double blueVal = 0;
this.gray();
for (int io = 0; io < this.getWidth(); io++)
{
for (int qq = 0; qq < this.getHeight(); qq++)
{
pixie = this.getPixel(io,qq);
redVal = pixie.getRed();
greenVal = pixie.getGreen();
blueVal = pixie.getBlue();
if (redVal < 64)
{
redVal = 31;
}
else if (redVal < 128)
{
redVal = 95;
}
else if (redVal < 128)
{
redVal = 95;
}
else if (redVal < 192)
{
redVal = 159;
}
else
{
redVal = 223;
}
if (redVal < 64)
{
redVal = 31;
}
else if (blueVal < 128)
{
blueVal = 95;
}
else if (redVal < 128)
{
blueVal = 95;
}
else if (blueVal < 192)
{
blueVal = 159;
}
else
{
blueVal = 223;
}
if (redVal < 64)
{
redVal = 31;
}
else if (greenVal < 128)
{
greenVal = 95;
}
else if (greenVal < 128)
{
greenVal = 95;
}
else if (greenVal < 192)
{
greenVal = 159;
}
else
{
greenVal = 223;
}
pixie.setRed((int) redVal);
pixie.setGreen((int) greenVal);
pixie.setBlue((int) blueVal);
}
}
}
public void swappingBack(PictureClass oldBack,
PictureClass newBack)
{
Pixel current = null;
Pixel oldPixel = null;
Pixel newPixie = null;
for (int io=0; io<this.getWidth(); io++)
{
for (int qq=0; qq<this.getHeight(); qq++)
{
current = this.getPixel(io,qq);
oldPixel = oldBack.getPixel(io,qq);
if (current.colorDistance(oldPixel.getColor()) < 15.0)
{
newPixie = newBack.getPixel(io,qq);
current.setColor(newPixie.getColor());
}
}
}
}
public void chromaKB(PictureClass newBack)
{
Pixel current = null;
Pixel newPixie = null;
for (int io=0; io<this.getWidth(); io++)
{
for (int qq=0; qq<this.getHeight(); qq++)
{
}
}
}
public static void main (String[] args){
PictureClass picss = new PictureClass(1000,800);
picss.crColl();
picss.show();
picss.write(FileChooser.getMediaPath("img.jpg"));
}
}
Please rate the answer if it helped.....Thankyou
Hope it helps.....
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.