Results 1 to 6 of 6
  1. #1
    Poles_Apart is offline Member
    Join Date
    Dec 2011
    Posts
    14
    Rep Power
    0

    Default Making two picture objects into one.

    Right now I have to make a collage type project where I need to take two images and stack them on top of each other and make a border. My program so far allows the user to choose two images, it displays the images, tells the user their size, and creates a border around them. I however can not figure out how to combine both of the images into a single object that i can move them around inside of. This is my code so far:
    Java Code:
    class TwoCollageMaker {
      public static void main(String a[]){
        Picture pict1=new Picture(FileChooser.pickAFile());
        System.out.print("Picture 1's width: " +pict1.getWidth());
        System.out.println(", Picture 1's height: "+pict1.getHeight());
        Picture pict2=new Picture(FileChooser.pickAFile());
        System.out.print("Picture 2's width: " +pict2.getWidth());
        System.out.println(", Picture 2's height: " +pict2.getHeight());
        
        
        //Picture 1 
        int widthNew;
        int heightNew;
        widthNew = pict1.getWidth();
        heightNew = pict1.getHeight();
        int borderThickness = 10;
        widthNew = widthNew + borderThickness*2;
        heightNew = heightNew + borderThickness*2;
        
        Picture pictNew = new Picture(widthNew, heightNew);
      
        pict1.compose(pictNew,10,10);
        pictNew.show();
       
        //Picture 2
        int widthNew2;
        int heightNew2;
        widthNew2 = pict2.getWidth();
        heightNew2 = pict2.getHeight();
        int borderThickness2 = 10;
        widthNew2 = widthNew2 + borderThickness2*2;
        heightNew2 = heightNew2 + borderThickness2*2;
        
        Picture pictNew2 = new Picture(widthNew2, heightNew2);
      
        pict2.compose(pictNew2,10,10);
        pictNew2.show();
    
        
        
      }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: Making two picture objects into one.

    how to combine both of the images into a single object
    One way would be to create a BufferedImage that is large enough to show both the images and then use its graphics context to draw the two images on it.
    Then you would have one image that had the content of the two images.

  3. #3
    Poles_Apart is offline Member
    Join Date
    Dec 2011
    Posts
    14
    Rep Power
    0

    Default Re: Making two picture objects into one.

    Thats an idea, however i was looking through the instructions and the picture class that i got from my textbook and it says to use the compose method which is this:
    Java Code:
    /**
       * Method to compose (copy) this picture onto a target picture
       * at a given point.
       * @param target the picture onto which we copy this picture
       * @param targetX target X position to start at
       * @param targetY target Y position to start at
       */
      public void compose(Picture target, int targetX, int targetY) {
     
        Pixel currPixel = null;
        Pixel newPixel = null;
    
        // loop through the columns
        for (int srcX=0, trgX = targetX; srcX < this.getWidth();
             srcX++, trgX++) {
      
          // loop through the rows
          for (int srcY=0, trgY=targetY; srcY < this.getHeight();
               srcY++, trgY++) {
    
            // get the current pixel
            currPixel = this.getPixel(srcX,srcY);
    
            /* copy the color of currPixel into target,
             * but only if it'll fit.
             */
            if (trgX < target.getWidth() && trgY < target.getHeight()) {
              newPixel = target.getPixel(trgX,trgY);
              newPixel.setColor(currPixel.getColor());
            }
          }
        }
      }
    I'm not quite sure how to use it though my professor didnt give us a demo on it.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: Making two picture objects into one.

    Sorry, I don't know anything about the Picture class. If you are using third party packages, it will be hard to get help here.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Making two picture objects into one.

    I've never heard of the Picture class and cannot find it in the Java 1.7 API and thus must assume that it's not part of standard Java. You're best to follow the instructions in your manual then.

  6. #6
    Poles_Apart is offline Member
    Join Date
    Dec 2011
    Posts
    14
    Rep Power
    0

    Default Re: Making two picture objects into one.

    Ok, thanks for the help.

Similar Threads

  1. Making a java picture
    By The Dark Dragon in forum New To Java
    Replies: 3
    Last Post: 11-25-2011, 02:42 PM
  2. Jar file making - picture problem
    By amman in forum New To Java
    Replies: 3
    Last Post: 05-06-2011, 01:14 PM
  3. Making a picture Warhol Style
    By chick in forum New To Java
    Replies: 4
    Last Post: 04-13-2010, 10:08 PM
  4. Making a pointer to objects
    By Bongeh in forum New To Java
    Replies: 9
    Last Post: 04-01-2010, 05:18 PM
  5. Replies: 1
    Last Post: 08-11-2009, 03:00 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
  •