Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: What does this part of the code mean?

  1. #1
    Member
    Join Date
    Aug 2012
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default What does this part of the code mean?

    I am studying a code that allows you to browse for one image, and then the program opens up two windows. The window on the left displays the image and the one on the right displays the same exact image but with a weird effect. I am trying to fully understand the code but I'm having trouble understanding this part of the code from one of the classes:

    //
    	// Transition by rows left to right top to bottom
    	public void transitionLRTB(Graphics g, BufferedImage leftImage) 
    	{
    		int width = leftImage.getWidth();
    		int height = leftImage.getHeight();
    		for (int j=0; j<height; j++)
    		{
    			for (int i=0; i<width; i++)
    			{
    				int pixelColor= leftImage.getRGB(i,j);
    				img.setRGB(i, j, pixelColor);
    			}
    			repaint();
    			try { Thread.sleep(10); } catch (InterruptedException e) { };
    		}
    	}


    I know it's a mutator object but I stop understanding what it does once it starts dealing with iterations and ++'s.

    Here is the full code:

    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.image.BufferedImage;
     
    import javax.swing.JPanel;
     
     
    public class LeftPanel extends JPanel 
    {
     
    	private BufferedImage img;
     
    	public LeftPanel(BufferedImage img)
    	{
    		this.img = img;
    	}
     
    	public void paintComponent (Graphics g){
    		super.paintComponent(g);
    		g.drawImage(this.img, 0, 0 , null);		
    	}
    }


    public void transitionLRTB(Graphics g, BufferedImage leftImage) 
    	{
    		int width = leftImage.getWidth();
    		int height = leftImage.getHeight();
    		for (int j=0; j<height; j++)
    		{
    			for (int i=0; i<width; i++)
    			{
    				int pixelColor= leftImage.getRGB(i,j);
    				img.setRGB(i, j, pixelColor);
    			}
    			repaint();
    			try { Thread.sleep(10); } catch (InterruptedException e) { };
    		}
    	}
     
    	//
    	// Transition by diagonals bottom to top left to right
    	public void transitionDiagonal45LR(Graphics g, BufferedImage leftImage)
    	{
    		int width = leftImage.getWidth();
    		int height = leftImage.getHeight();
    		for (int row=0; row<height; row++)
    		{
    			int diagonalRow = row;
    			for (int col=0; col<=row; col++) 
    			{
    				int pixelColor= leftImage.getRGB(col,diagonalRow);
    				img.setRGB(col, diagonalRow, pixelColor);
    				diagonalRow--;
    			}
    			repaint();
    			try { Thread.sleep(10); } catch (InterruptedException e) 
    			{ };
    		}
     
    		if (width > height) 
    		{
    			// Image with larger width than height		
    			for (int i=0; i<(width-height); i++) 
    			{
    				int row = height-1;
    				int col = i+1;
    				for (int j=0; j<height; j++) 
    				{
    					int pixelColor= leftImage.getRGB(col,row);
    					img.setRGB(col, row, pixelColor);
    					col++; row--;
    				}
    				repaint();
    				try { Thread.sleep(10); } catch (InterruptedException e) { };
    			}
     
    			for (int i=0; i<=(width); i++) 
    			{
    				int diagonalRow = height-1;
    				for (int col=(width-height+i); col<width; col++) 
    				{
    					int pixelColor= leftImage.getRGB(col,diagonalRow);
    					img.setRGB(col, diagonalRow, pixelColor);
    					diagonalRow--;
    				}
    				repaint();
    				try { Thread.sleep(10); } catch (InterruptedException e) { };
    			}
    		}
    		else {
    			// Lab Q2: Add code to consider image with larger height than width
    		}
    	}
     
    }


    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.image.BufferedImage;
    import java.awt.image.ImageObserver;
    import java.awt.image.Raster;
    import java.io.File;
    import java.io.IOException;
     
    import javax.imageio.ImageIO;
    import javax.swing.JFileChooser;
    import javax.swing.JFrame;
    import javax.swing.filechooser.FileNameExtensionFilter;
     
    public class TransitionTester {
     
    	/**
    	 * @param args
    	 * 
    	 */
     
    	private static BufferedImage img;
     
    	public static void main(String[] args) {
     
    		final int FrameWidth = 400;
    		final int FrameHeight = 500;
    		final int LeftFrameXLeft = 300;
    		final int RightFrameXLeft = LeftFrameXLeft + FrameWidth + 10;
    		final int FrameYTop = 200;
     
    		BufferedImage img = null;
    		BufferedImage rightImage = null;
     
    		//Create left frame with source image
    		JFrame leftFrame = new JFrame("Source Frame");
    		leftFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    		leftFrame.setLocation(LeftFrameXLeft, FrameYTop);
    		leftFrame.setSize(FrameWidth, FrameHeight);
     
    		File imageFile = null;
     
    		boolean imageLoaded=false;
    		while (!imageLoaded) {
    			// Load an image file
    			JFileChooser chooser = new JFileChooser("Hello");
    			FileNameExtensionFilter filter = new FileNameExtensionFilter("JPG & GIF Images", "jpg", "gif");
    			chooser.setFileFilter(filter);
    			int returnVal = chooser.showOpenDialog(leftFrame.getContentPane());
    			if(returnVal == JFileChooser.APPROVE_OPTION) {
    				System.out.println("You chose to open this file: " + chooser.getSelectedFile().getName());
    			}
    			if (returnVal == JFileChooser.CANCEL_OPTION) {
    				leftFrame.dispose();
    				return;
    			}			
     
    			imageFile = chooser.getSelectedFile();
     
    			try {
    				img = ImageIO.read(imageFile);
    			} catch (IOException e) {
    				System.out.println("Error attempting to load image File: " + e);
    				return;
    			}
    			// Reject image if it does not fit in frame
    			imageLoaded = ((img.getWidth() <= FrameWidth) && (img.getHeight() <= FrameHeight));
    			if (!imageLoaded) { System.out.println("Image too large"); }
    		}
    		LeftPanel myLeftPanel = new LeftPanel(img);
    		leftFrame.getContentPane().add(myLeftPanel);
     
    		try {
    			rightImage = ImageIO.read(imageFile);
    		} catch (IOException e) {
    			System.out.println("Error attempting to load image File: " + e);
    			return;
    		}
     
    		RightPanel myRightPanel = new RightPanel(rightImage);
    		myRightPanel.clearImage(myRightPanel.getGraphics());
     
    		// Create right Frame
    		JFrame rightFrame = new JFrame("Destination Frame");
    		rightFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    		rightFrame.setLocation(RightFrameXLeft, FrameYTop);
    		rightFrame.setSize(FrameWidth, FrameHeight);
    		rightFrame.getContentPane().add(myRightPanel);
     
    		leftFrame.setVisible(true);
    		rightFrame.setVisible(true);
     
    		final int NumTransitions = 2;
    		int nextTransition = 0;
    		while(true) {
    			myRightPanel.clearImage(myRightPanel.getGraphics());
    			switch(nextTransition) {
    			case 0: myRightPanel.transitionLRTB(myRightPanel.getGraphics(), img); break;
    			case 1: myRightPanel.transitionDiagonal45LR(myRightPanel.getGraphics(), img); break;
    			}
    			nextTransition = (nextTransition + 1) % NumTransitions;
    		}
    	}
    }

    The part I don't understand is in the RightPanel class. Thank you all very much.


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: What does this part of the code mean?

    Quote Originally Posted by jean28 View Post
    The part I don't understand is in the RightPanel class.
    But you've not posted a RightPanel class.

  3. #3
    Member
    Join Date
    Aug 2012
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: What does this part of the code mean?

    Here is the RightPanel code. I need to understand which part of it causes the horizontal and vertical transitions.

    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.image.BufferedImage;
     
    import javax.swing.JPanel;
     
     
    public class RightPanel extends JPanel 
    {
    	private BufferedImage img;
     
    	public RightPanel(BufferedImage img)
    	{
    		this.img = img;
    	}
     
    	public void paintComponent (Graphics g)
    	{
    		super.paintComponent(g);
    		g.drawImage(this.img, 0, 0 , null);		
    	}
     
    	public void clearImage(Graphics g)
    	{
    		int width = img.getWidth();
    		int height = img.getHeight();
    		for (int i=0; i<width; i++)
    		{
    			for (int j=0; j<height; j++)
    			{
    				img.setRGB(i,j,0xffffffff);
    			}
    		}
    	}
     
    	//
    	// Transition by columns top to bottom left to right
    	public void transitionTBLR(Graphics g, BufferedImage leftImage) 
    	{
    		// Lab Q1: Your Code Here
    	}
     
    	//
    	// Transition by rows left to right top to bottom
    	public void transitionLRTB(Graphics g, BufferedImage leftImage) 
    	{
    		int width = leftImage.getWidth();
    		int height = leftImage.getHeight();
    		for (int j=0; j<height; j++)
    		{
    			for (int i=0; i<width; i++)
    			{
    				int pixelColor= leftImage.getRGB(i,j);
    				img.setRGB(i, j, pixelColor);
    			}
    			repaint();
    			try { Thread.sleep(10); } catch (InterruptedException e) { };
    		}
    	}
     
    	//
    	// Transition by diagonals bottom to top left to right
    	public void transitionDiagonal45LR(Graphics g, BufferedImage leftImage)
    	{
    		int width = leftImage.getWidth();
    		int height = leftImage.getHeight();
    		for (int row=0; row<height; row++)
    		{
    			int diagonalRow = row;
    			for (int col=0; col<=row; col++) 
    			{
    				int pixelColor= leftImage.getRGB(col,diagonalRow);
    				img.setRGB(col, diagonalRow, pixelColor);
    				diagonalRow--;
    			}
    			repaint();
    			try { Thread.sleep(10); } catch (InterruptedException e) 
    			{ };
    		}
     
    		if (width > height) 
    		{
    			// Image with larger width than height		
    			for (int i=0; i<(width-height); i++) 
    			{
    				int row = height-1;
    				int col = i+1;
    				for (int j=0; j<height; j++) 
    				{
    					int pixelColor= leftImage.getRGB(col,row);
    					img.setRGB(col, row, pixelColor);
    					col++; row--;
    				}
    				repaint();
    				try { Thread.sleep(10); } catch (InterruptedException e) { };
    			}
     
    			for (int i=0; i<=(width); i++) 
    			{
    				int diagonalRow = height-1;
    				for (int col=(width-height+i); col<width; col++) 
    				{
    					int pixelColor= leftImage.getRGB(col,diagonalRow);
    					img.setRGB(col, diagonalRow, pixelColor);
    					diagonalRow--;
    				}
    				repaint();
    				try { Thread.sleep(10); } catch (InterruptedException e) { };
    			}
    		}
    		else {
    			// Lab Q2: Add code to consider image with larger height than width
    		}
    	}
     
    }

  4. #4
    Member
    Join Date
    Aug 2012
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: What does this part of the code mean?

    Basically what I need to know is what set of instructions would make it go from top to bottom and then from left to right.

    This is what I have:

    // Transition by rows left to right top to bottom
    	public void transitionLRTB(Graphics g, BufferedImage leftImage) 
    	{
    		int width = leftImage.getWidth();
    		int height = leftImage.getHeight();
    		for (int j=0; j<height; j++)
    		{
    			for (int i=0; i<width; i++)
    			{
    				int pixelColor= leftImage.getRGB(i,j);
    				img.setRGB(i, j, pixelColor);
    			}
    			repaint();
    			try { Thread.sleep(10); } catch (InterruptedException e) { };
    		}
    	}

    and then I have

    // Transition by diagonals bottom to top left to right
    	public void transitionDiagonal45LR(Graphics g, BufferedImage leftImage)
    	{
    		int width = leftImage.getWidth();
    		int height = leftImage.getHeight();
    		for (int row=0; row<height; row++)
    		{
    			int diagonalRow = row;
    			for (int col=0; col<=row; col++) 
    			{
    				int pixelColor= leftImage.getRGB(col,diagonalRow);
    				img.setRGB(col, diagonalRow, pixelColor);
    				diagonalRow--;
    			}
    			repaint();
    			try { Thread.sleep(10); } catch (InterruptedException e) 
    			{ };
    		}

  5. #5
    Member
    Join Date
    Aug 2012
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: What does this part of the code mean?

    Could it be something like this?

    //		int width = leftImage.getWidth();
    //		int height = leftImage.getHeight();
    //		for (int i=0; i<width; i++)
    //		{
    //		for (int j=0; i<width; j++)
    //			{
    //				int pixelColor= leftImage.getRGB(i,j);
    //				img.setRGB(i, j, pixelColor);
    //			}
    //			repaint();
    //			try { Thread.sleep(10); } catch (InterruptedException e) { };
    //		}

  6. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: What does this part of the code mean?

    Quote Originally Posted by jean28 View Post
    Basically what I need to know is what set of instructions would make it go from top to bottom and then from left to right
    Yes, I see that: // Lab Q1: Your Code Here
    Doing homework for others is generally frowned upon and considered cheating. I will be happy to answer any questions you have to help you complete the project.



    Quote Originally Posted by jean28 View Post
    Could it be something like this?...
    What did it do when you tried it?

Similar Threads

  1. What does this short part of a code do?
    By Kranti1992 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 28th, 2012, 11:31 AM
  2. How to find a a certain part in string
    By redzero36 in forum Java Theory & Questions
    Replies: 7
    Last Post: July 19th, 2011, 06:39 PM
  3. A Part Question
    By Aksand in forum Java Theory & Questions
    Replies: 5
    Last Post: April 13th, 2011, 11:12 AM
  4. [SOLVED] Help Again Last Part of my Problem :(
    By metalx66 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 19th, 2011, 09:48 PM
  5. can' stop working a part of code.plzz help
    By kyros in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 30th, 2010, 03:10 PM