Results 1 to 7 of 7
  1. #1
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default Swapping Images from small to big problems

    Hi, I was trying to make a simple program that swaps a thumbnail image (really a JButton with an icon) into a large image display and swap the old large image to where the thumbnail image was.

    A poor description:
    ----------------------



    LARGE IMAGE HERE




    ----------------------

    -----------------------------
    thumbnail thumbnail thumbnail
    -----------------------------

    The problem I am having is that it either swaps in the thumbnail but the large image does not become a thumbnail OR i can make the large image a thumbnail but the thumbnail pressed does not become a large image but the old large image stays put.

    here is the code, the problem is in the changeBigPhoto() function:
    smallPhotos, are really just JButtons
    bigPhoto is just a JLabel
    Java Code:
    public class ImageSlideShow {
    
    	private JFrame frame;
    	private JPanel bigPanel;
    	private JPanel smallPanel;
    	private ImageIcon bigIcon;
    	private ImageIcon smallIcons[];
    	private JLabel bigPhoto;
    	private JButton smallPhotos[];
    	
    	public ImageSlideShow()
    	{
    		
    		frame=new JFrame();
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(800,550);
    		
    		bigPanel=new JPanel();
    		bigPanel.setLayout(new GridBagLayout());
    	
    		smallPanel=new JPanel();
    		smallPanel.setLayout(new FlowLayout());
    		
    		// create a bigIcon and bigPhoto(JLabel)
    		bigIcon=new ImageIcon("bigphoto.jpg");
    		bigPhoto=new JLabel(bigIcon);
    		
    		//add bigPhoto(JLabel) to the bigPanel
    		bigPanel.add(bigPhoto);
    		
    		// declare an array of smallIcons, and smallPhotos(JButtons)
    		smallIcons=new ImageIcon[5];
    		smallPhotos=new JButton[5];
    		
    		// create smallIcons and smallButtons with these icons 
    		for(int i=0;i<smallIcons.length;i++)
    		{
    			smallIcons[i]=new ImageIcon("smallphoto2.jpg");
    		
    			// down scale the image of the small icon 
    			Image image=smallIcons[i].getImage().getScaledInstance(100,100,Image.SCALE_FAST);
    			smallIcons[i].setImage(image);
    
    			smallPhotos[i]=new JButton();
    			smallPhotos[i].setMargin(new Insets(0,0,0,0));
    		
    			// add an actionlistener to the smallPhotos(JButtons)
    			Handle h1=new Handle(smallIcons[i],i);
    			smallPhotos[i].addActionListener(h1);
    		
    			smallPhotos[i].setIcon(smallIcons[i]);
    		
    			// add the smallPhotos(JButtons) to the smallPanel(FlowLayout)
    			smallPanel.add(smallPhotos[i]);
    		}
    		
    		
    		// add panels to the frames contentPane and set the frame to visible
    		frame.getContentPane().add(bigPanel,BorderLayout.CENTER);
    		frame.getContentPane().add(smallPanel,BorderLayout.SOUTH);
    		frame.setVisible(true);
    		
    		
    	}
    	
    	// function that swaps the big photo for the thumbnail photo and 
    	// resizes the two photo's appropiately
    	// @param newImg - the icon of the thumbnail button pressed
    	// @param index = the array index of the smallIcon[] array
    	
    	public void changeBigPhoto(ImageIcon newImg,int index)
    	{
    		ImageIcon tempBig=bigIcon;
    		ImageIcon tempSmall=newImg;
    		
    		// change bigPhoto's icon to the clicked thumbnails icon
    		Image img1=tempSmall.getImage().getScaledInstance(600, 600, Image.SCALE_SMOOTH);
    		bigIcon.setImage(img1);
    		bigPhoto.setIcon(bigIcon);
    		
    		// change the clicked thumbnails icon to the former bigPhotos icon
    		smallIcons[index]=tempBig;
    		
    		// rescale the bigPhoto icon to fit in the small icon
    		Image img2=smallIcons[index].getImage().getScaledInstance(100,100,Image.SCALE_SMOOTH);
    		smallIcons[index].setImage(img2);
    		smallPhotos[index].setIcon(smallIcons[index]);
    		
    		// update the changes
    		frame.validate();
    		frame.repaint();
    	}
    	
    	// private inner class to handle button presses i.e selecting a thumbnail to view
    	private class Handle implements ActionListener{
    		
    		private ImageIcon ic;
    		private int index;
    		
    		public Handle(ImageIcon ic,int index)
    		{
    			this.ic=ic;
    			this.index=index;
    		}
    
    		@Override
    		public void actionPerformed(ActionEvent e) {
    			
    			changeBigPhoto(ic,index);
    			
    		}
    Thanks in advance for any help

  2. #2
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Just thought I would add this observation

    In the changeBigPhoto function i have this:

    Java Code:
    ImageIcon tempBig=bigIcon;
    ImageIcon tempSmall=newImg;
    bigIcon is the large Icon used on the JLabel and newImg is the small icon used on the small buttons. When i use the temps inside the function something weird is happening and I dont know why. The changes to the tempBigIcon is actually changing the bigIcon, and changes to the tempSmall is actuallly changing the newImg:confused: why would this happen?

  3. #3
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Okay I have found a solution, it was as simple as making tempBigIcon and tempSmallIcon=newImageIcon(); rolleyes::D

    I would still like to know the reason WHY this worked though :o

    Java Code:
    public void changeBigPhoto(int index)
    	{
    		tempBigIcon=new ImageIcon(bigIcon.getImage());
    		tempSmallIcon=new ImageIcon(smallIcons[index].getImage());
    		
    		// change the clicked thumbnails icon to the former bigPhotos icon
    		// rescale the bigPhoto icon to fit in the small icon
    		Image img2=tempBigIcon.getImage().getScaledInstance(100,100,Image.SCALE_SMOOTH);
    		smallIcons[index].setImage(img2);
    		smallPhotos[index].setIcon(smallIcons[index]);
    		
    		// change bigPhoto's icon to the clicked thumbnails icon
    		Image img1=tempSmallIcon.getImage().getScaledInstance(600, 600, Image.SCALE_SMOOTH);
    		bigIcon.setImage(img1);
    		bigPhoto.setIcon(bigIcon);
    		
    		// update the changes
    		frame.validate();
    		frame.repaint();
    		
    	}

  4. #4
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    The thing with non primitive types, like any object, is that doing:
    Java Code:
    Object o1 = new Object();
    Object o2 = o1;
    will just make both o1 and o2 point to the same memory location (referencing). Changes made to o1 will be reflected in o2 and vice versa.
    Try running this piece of code and see the results.
    Java Code:
    int[] a1 = new int[10]; //all elements automatically initialized to 0
    int[] a2 = a1;
    a1[2] = 5;
    System.out.println(a2[2]);
    Run this snippet, observe the results, and figure out why the output is 5, even though you didn't assign any values to the a2 array.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  5. #5
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    I see, it is working like pointers in c++ ???

  6. #6
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    It's a pretty similar process, yeah. For example, in C, you could do:
    Java Code:
    int a = 5;
    int* pa = &a;
    int* pb = pa;
    *pb = 6;
    The change will be reflected on both pointers. Also, my syntax probably isn't correct, since it's been years since I wrote something in C/C++.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  7. #7
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

Similar Threads

  1. Swapping elements of an array help please
    By ikillu in forum New To Java
    Replies: 11
    Last Post: 01-15-2012, 09:49 PM
  2. Swapping Characters
    By besweeet in forum New To Java
    Replies: 8
    Last Post: 02-18-2010, 05:37 PM
  3. swapping elements of vector
    By sara12345 in forum New To Java
    Replies: 1
    Last Post: 01-07-2010, 10:45 PM
  4. small FTP app, commons-net-ftp, problems with run
    By metoda in forum New To Java
    Replies: 1
    Last Post: 11-28-2009, 06:19 PM
  5. Problems to show images in applets
    By Felissa in forum Java Applets
    Replies: 1
    Last Post: 07-06-2007, 10:12 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
  •