d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Homework Assignment > You Up For It?
12Next
Add Reply New Topic New Poll
Member
Posts: 2,458
Joined: Sep 20 2009
Gold: 10,110.50
Mar 18 2014 04:27pm
Looking for anyone with java experience to help me out with my school homework assignment.

Creating two easy methods

Deals with:
Arrays
Nested Loops
Conditionals
Making “movies” from a sequence of pictures

Anyone up for it? It's a 1st year university course.
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Mar 18 2014 04:58pm
Absolutely. Just ask whatever questions you have here.
Member
Posts: 2,458
Joined: Sep 20 2009
Gold: 10,110.50
Mar 18 2014 05:16pm
Task:
A digital effect often seen in videos, presentations and commercials is fading out and also fading in. To achieve a fade out, starting with a digital image and a colour to fade out to, we slowly turn the image into an image made up of pixels that are all the that colour by constructing a number of intermediate images. Each of these intermediate images is constructed from the pixel in the initial image we start with, and a proportion of the end colour. The construction is done systematically, so that when looking at a sequence of pictures, startingPicture, p1, p2, p3, endingPicture, it appears that each successive intermediate picture p1, p2 and p3 becomes more and more like endingPicture, which for the fade out, is a solid colour. The more intermediate pictures we create, the smoother the fade out. A similar process is used to fade in, where we start with an image made up entirely of a start colour, and slowly progress to the pixel colours found in our ending image.
Part A:
The first part of the assignment will be to create two methods to add to the Picture class: fadeOut and fadeIn. Further detail can be found below, but lets introduce the algorithm first for fading out. The algorithm for fading in will be very similar. You will have a startingPicture, and a colour, both of which you can pick. To create the a sequence of pictures that fade out: startingPicture, p1, p2, p3, endingPicture, where endingPicture is a solid colour, we will need to create and modify p1,p2,p3 and endingPicture pixel by pixel using an equation. We are going to calculate the red, green and blue values for each pixel as being somewhere between the startingPicture pixel values, and the solid colour. For each pixel of the fading pictures, we compute its red, green and blue values individually using the following formula for each colour (example here shown for red): redValue = startRed + ((endRed – startRed)/numStages) * k where numStages is the total number of stages (ex 4; p1,p2,p3 and endingPicture for the example above) to be used in the fade out, and k is the kth picture. So, for example, to compute the red value of a pixel in p2, the value of k would be 2. If the starting red value of that pixel was 100, and the end value was 200, the formula would be: redValue of pixel = 100 + ((200 – 100)/4) * 2 This formula must be used three times on every pixel in order to get the red, green and blue values for the pixel. This idea can be extended for any number of intermediate pictures. If we wanted 7 stages instead of 3, then k would range from 1 to 7, and we would have divided by 7 in the formula. In general, the formula for each colour is: colourValue = startValue + ((endValue – startValue)/(numStages )) * k For Part A of the assignment, you will write TWO methods in the Picture class that form an intermediate picture in the fade out and fade in sequence respectively, as specified below. The methods will use the picture parameter to modify “this” picture to be a stage in the fade out / fade in process.
Functional Specifications for Part A

1. You are to write a method fadeOut and add it to the Picture class. It will have the header public void fadeOut(Picture startPicture, Color fadeTo, int numStages, int k)
• startPicture : the starting picture
• fadeTo : the solid colour to be used in the final image
• numStages : the total number of stages in the fade out process, including the final image
• k : the current stage in the fading process
(will be a number between 1 and numStages inclusive)

fadeOut() will be invoked on a Picture object that is the “target” picture, that is, the picture that represents the kth stage in fading from the starting picture to the ending colour. The method will form the picture for the current stage (ie stage k) by setting the red, green, and blue values for the pixels in this picture according to the above formula, pixel by pixel. Your method should be written so that it will not crash if the startPicture and “this” image are of different sizes. We will test this with our own selected pictures, as long as no red text appears and the program exits normally, you will not be docked marks.



2. You are to write a method fadeIn and add it to the Picture class. It will have the header public void fadeIn(Picture endPicture, Color fadeFrom, int numStages, int k)
• endPicture : the ending picture
• fadeFrom : the solid colour to be used in the first image
• numStages : the total number of stages in the fade in process, including the first image
• k : the current stage in the fading process
(will be a number between 0 and numStages-1 inclusive)

fadeIn() will be invoked on a Picture object that is the “target” picture, that is, the picture that represents the kth stage in fading from the starting colour to the ending picture. The method will form the picture for the current stage (ie stage k) by setting the red, green, and blue values for the pixels in this picture according to the above formula (note that the starting and ending values will not be from the same sources), pixel by pixel. Your method should be written so that it will not crash if the endPicture and “this” image are of different sizes. We will test this with our own selected pictures, as long as no red text appears and the program exits normally, you will not be docked marks.



3. You will write a class TestFading (in TestFading.java) that consists primarily of a main method that tests fadeOut() and fadeIn() for correctness. It must contain the following tests:
i. Get a picture using FileChooser. form 4 stages of the fade out process and display them, along with the starting picture. (You should end up with a total of 5 pictures on the screen from this test: the starting picture, the 3 intermediate faded pictures, and the ending picture which is a solid colour. You should be able to move them around on the screen using the mouse so that all can be displayed.)
ii. Perform the same steps for fadeIn.

Did I scare you guys away? :rolleyes:
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Mar 18 2014 05:56pm
Well, I dont have the Picture class, so I don't know how the picture is actually encapsulated, but the methods appear straight forward. You would need to iterate over each pixel of the image and apply the formula. What exactly are you stuck on?

Could you provide either the code for the Picture class or the API so we can take a look?
Member
Posts: 2,458
Joined: Sep 20 2009
Gold: 10,110.50
Mar 18 2014 06:31pm
Quote (Minkomonster @ Mar 18 2014 07:56pm)
Well, I dont have the Picture class, so I don't know how the picture is actually encapsulated, but the methods appear straight forward. You would need to iterate over each pixel of the image and apply the formula. What exactly are you stuck on?

Could you provide either the code for the Picture class or the API so we can take a look?


I'm not sure what you mean by API, as for Picture class, it's rather a large file with many methods involved but this is what I have so far:
public void fadeOut(Picture startPicture, Color fadeTo, int numbStages, int k)
{
/*startPicture : the starting picture
* fadeTo : the solid colour to be used in the final image
* numStages : the total number of stages in the fade out process, including the final image
* k : the current stage in the fading process(will be a number between 1 and numStages inclusive)
*/Pixel[] pixelArray = this.getPixels();
Pixel pixel = null;
int redV, blueV, greenV = 0;
Picture startPicture = FileChooser.pickAFile();
Color fadeTo = 255;
int numStages = 4;
int k = //Will be a number between 1 and numStages inclusive

//Loop through all pixels in the picture
for (int i=0; i<pixelArray.length;i++){
pixel = pixelArray[i];
redV= startRed + ((endRed - startRed)/ 3) * k;
greenV= startGreen + ((endGreen - startGreen)/ 3) * k;
blueV = startBlue + ((endBlue - startBlue)/ 3) * k;
/*redValue = startRed + ((endRed – startRed)/numStages) * k
* colourValue = startValue + ((endValue – startValue)/(numStages )) * k
* where numStages is the total number of stages (ex 4; p1,p2,p3 and endingPicture for the example
* above) to be used in the fade out, and k is the kth picture.
*/pixel.setColor(new Color(255-redV, 255-greenV, 255-blueV));
}
}


This is my fadeOut method


I basically need help with the structure of the method. How do I have it so it asks the user to pick a picture then the method tests to make sure the picture is the right size, then proceeds with the whole fading part and ends up showing you the last picture (which is tested to be the same size).



This post was edited by Elitist on Mar 18 2014 06:39pm
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Mar 18 2014 06:40pm
Quote (Elitist @ Mar 18 2014 07:31pm)
I'm not sure what you mean by API, as for Picture class, it's rather a large file with many methods involved but this is what I have so far:
  public void fadeOut(Picture startPicture, Color fadeTo, int numbStages, int k)
  {
    /*startPicture : the starting picture
    * fadeTo : the solid colour to be used in the final image
    * numStages : the total number of stages in the fade out process, including the final image
    * k : the current stage in the fading process(will be a number between 1 and numStages inclusive)
    */Pixel[] pixelArray = this.getPixels();
    Pixel pixel = null;
    int redV, blueV, greenV = 0;
    Picture startPicture = FileChooser.pickAFile();
    Color fadeTo = 255;
    int numStages = 4;
    int k = //Will be a number between 1 and numStages inclusive

    //Loop through all pixels in the picture
    for (int i=0; i<pixelArray.length;i++){
      pixel = pixelArray[i];
      redV= startRed + ((endRed - startRed)/ 3) * k;
      greenV= startGreen + ((endGreen - startGreen)/ 3) * k;
      blueV = startBlue + ((endBlue - startBlue)/ 3) * k;
      /*redValue = startRed + ((endRed – startRed)/numStages) * k
      * colourValue = startValue + ((endValue – startValue)/(numStages )) * k
      * where numStages is the total number of stages (ex 4; p1,p2,p3 and endingPicture for the example
      * above) to be used in the fade out, and k is the kth picture.
      */pixel.setColor(new Color(255-redV, 255-greenV, 255-blueV));
    }
  }


This is my fadeOut method


Can you past the Pixel class?
Member
Posts: 2,458
Joined: Sep 20 2009
Gold: 10,110.50
Mar 18 2014 06:45pm
the pixel class has over 300 lines it appears... you want me to just copy/paste it on here?
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Mar 18 2014 07:01pm
Well, it is either that or I guess what methods the class has.

This has not been compiled, but should give an idea on what to do:

Code
public void fadeOut(Picture startPicture, Color fadeTo, int numStages, int k)
{
Color current, newColor;
int red, green, blue;
Pixel[] source, target;

target = this.getPixels();
source = startPicture.getPixels();

//if the pictures are not the same length, return
if(target.length != source.length)
return;


for(int i = 0; i < target.length; i++)
{
current = source[i].getColor();
newColor(current, fadeTo,numStages,k);

target[i].setColor(newColor);

}

}

public void fadeIn(Picture endPicture, Color fadeFrom, int numStages, int k)
{
Color current, newColor;
int red, green, blue;
Pixel[] source, target;

target = this.getPixels();
source = endPicture.getPixels();

//if the pictures are not the same length, return
if(target.length != source.length)
return;

for(int i = 0; i < target.length; i++)
{
current = source[i].getColor();
newColor = getFadedColor(fadeFrom, current);

target[i].setColor(newColor);

}

}

private Color getFadedColor(Color start, Color end, int numStages, int k)
{
int red, blue, green;

red = getColorValue(start.getRed(), end.getRed(), numStages, k);
blue = getColorValue(start.getBlue(), end.getBlue(), numStages, k);
green = getColorValue(start.getGreen(), end.getGreen(), numStages, k);
return new Color(red, green, blue);
}

private int getColorValue(int startValue, int endValue, int numStages, int k)
{
return startValue + ((endValue - startValue) / numStages) * k;
}
Member
Posts: 2,458
Joined: Sep 20 2009
Gold: 10,110.50
Mar 18 2014 07:08pm
Which would you prefer, pixel class or picture class?

The assignment is asking to place the method in the picture class, so I'm assuming you would need that instead of pixel?

I'm appreciating your help.
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Mar 18 2014 07:10pm
I dont know what methods the Picture class or the Pixel class have. I took an educated guess with the getColor() method on the Pixel class and the setColor method on the Pixel class.
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll