Results 1 to 7 of 7
- 05-20-2010, 06:32 PM #1
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
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
Thanks in advance for any helpJava 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); }
- 05-20-2010, 07:08 PM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Just thought I would add this observation
In the changeBigPhoto function i have this:
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?Java Code:ImageIcon tempBig=bigIcon; ImageIcon tempSmall=newImg;
- 05-20-2010, 07:34 PM #3
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
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(); }
- 05-20-2010, 07:53 PM #4
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
The thing with non primitive types, like any object, is that doing:
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.Java Code:Object o1 = new Object(); Object o2 = o1;
Try running this piece of code and see the results.
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.Java Code:int[] a1 = new int[10]; //all elements automatically initialized to 0 int[] a2 = a1; a1[2] = 5; System.out.println(a2[2]);
Ever seen a dog chase its tail? Now that's an infinite loop.
- 05-20-2010, 08:21 PM #5
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
I see, it is working like pointers in c++ ???
- 05-21-2010, 07:01 AM #6
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
It's a pretty similar process, yeah. For example, in C, you could do:
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++.Java Code:int a = 5; int* pa = &a; int* pb = pa; *pb = 6;
Ever seen a dog chase its tail? Now that's an infinite loop.
- 05-21-2010, 02:42 PM #7
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Similar Threads
-
Swapping elements of an array help please
By ikillu in forum New To JavaReplies: 11Last Post: 01-15-2012, 08:49 PM -
Swapping Characters
By besweeet in forum New To JavaReplies: 8Last Post: 02-18-2010, 04:37 PM -
swapping elements of vector
By sara12345 in forum New To JavaReplies: 1Last Post: 01-07-2010, 09:45 PM -
small FTP app, commons-net-ftp, problems with run
By metoda in forum New To JavaReplies: 1Last Post: 11-28-2009, 05:19 PM -
Problems to show images in applets
By Felissa in forum Java AppletsReplies: 1Last Post: 07-06-2007, 09:12 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks