Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Apr 2011
    Thanked 0 Times in 0 Posts

    Java - need help cropping an image

    Hello, I was wondering if someone could take a look at my Java code below and tell me what I'm doing wrong. I cannot seem to get my crop method to work. I'm having trouble understanding how to crop the image by passing the x,y coords and the width height to crop. Any advice/assistance would be really appreciated.

    import java.awt.Color;
    public class Prob03{

    public static void main(String[] args){
    Prob03Runner obj = new Prob03Runner();

    class Prob03Runner{

    public Prob03Runner() {
    System.out.println("My Name");


    public void run(){

    //a view facing the front of the skater.
    Picture front = new Picture("Prob03a.bmp");
    front = crop(front,123,59,110,256);

    //a view showing the right side of the skater
    Picture right = new Picture("Prob03b.bmp");
    right = crop(right,123,59,110,256);

    //a view showing the left side of the skater
    Picture left = new Picture("Prob03c.bmp");
    left = crop(left,123,59,110,256);

    //this will be the background for the new picture
    Picture snowScene = new Picture("Prob03d.jpg");
    snowScene = crop(snowScene,6,59,344,256);

    //draw the front view of the skater on the snowScen full size

    //draw the left side view of the skater on the snowScene at half size
    left = left.getPictureWithHeight(256/2);

    //draw the right side view of the skater on snowscene at one-third size
    right = right.getPictureWithHeight(256/3);

    //Display name on final output and display it
    snowScene.addMessage("Steve Bownds",10,15);
    }//end run method

    private Picture crop(Picture pic, int x1, int y1, int width, int height){

    int x2=x1+width-1;
    int y2=y1+height-1;

    Picture output = new Picture(x2-x1+1,y2-y1+1);

    Pixel pixel = null;
    Color color = null; //end listing 4

    for(int col = x1;
    col < (x2+1);
    col++) {
    for(int row = y1;
    row < (y2+1);
    color = pic.getPixel(col,row).getColor();
    pixel = output.getPixel(width-col+x1-1,row-y1);

    return output;

    //listing 5

    private void greenScreenDraw(
    Picture source,
    Picture dest,
    //place the upper-left corner of the source
    //image at the following location in the
    //destination image
    int destX,
    int destY){

    int width = source.getWidth();
    int height = source.getHeight();
    Pixel pixel = null;
    Color color = null;

    for(int row = 0;row < height;row++){
    for(int col = 0;col < width;col++){
    color = source.getPixel(col,row).getColor();
    pixel = dest.getPixel(destX + col,destY + row);




  2. #2
    New to the CF scene
    Join Date
    Apr 2011
    Thanked 0 Times in 0 Posts
    I'm in your class.

    I would edit your post and remove your name from the line where you actually add your name to the image:


    Also, in case you hadn't figured it out. You have to create the method "crop" from scratch, just like "greenScreenDraw" was created there.

    Nothing from his previous lessons helped me to create this crop method, neither did anything from the book. Took me hours of searching to find something that helped, and of course I didn't bookmark it so now I can't find it.

    I found this thread trying to figure out different methods of cropping an image, I'm having an extremely hard time with cropping and keeping the color inversions after cropping on Prob04.
    Last edited by Modilly; 04-17-2011 at 03:14 PM.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts