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.