import javax.swing.*; import java.awt.*; import java.io.*; import java.awt.event.*; import java.net.*; public class RetrivalAlbumCover implements ActionListener { private String sPerformer, sAlbumName; private ImageIcon[] images; public int selectedImage; private JFrame frame; public ImageIcon go(String performer, String albumName) { search(performer, albumName); try { Thread.sleep(15000); } catch (Exception e) { e.getMessage(); } if (selectedImage != -1) { return images[selectedImage]; } else return null; } public void search(String performer, String albumName){ sPerformer = performer; sAlbumName = albumName; ButtonGroup bg = new ButtonGroup(); frame = new JFrame("Album Cover Results"); Container cn = frame.getContentPane(); cn.setLayout(new BorderLayout()); JPanel buttonPanel = new JPanel(); JButton bCancel = new JButton(" Cancel "); bCancel.addActionListener(this); bCancel.setActionCommand("cancel"); JPanel imagePanel = new JPanel(); imagePanel.setLayout(new FlowLayout()); images = getImages(); JButton[] bIcon = new JButton[images.length]; for (int i = 0; i < images.length; i++) { ImageIcon temp = resizeIcon(images[i], 120, 120); bIcon[i] = new JButton(temp); bIcon[i].addActionListener(this); bIcon[i].setActionCommand(""+i); imagePanel.add(bIcon[i]); if (i < images.length-1) imagePanel.add(Box.createHorizontalStrut(40)); } JTextArea info = new JTextArea("Please select an album cover or hit cancel to return"); info.setEditable(false); cn.add(info, BorderLayout.NORTH); JScrollPane spIcons = new JScrollPane(imagePanel,JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); cn.add(spIcons,BorderLayout.CENTER); buttonPanel.add(bCancel); cn.add(buttonPanel,BorderLayout.SOUTH); frame.setSize(500,240); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE ); frame.setResizable(false); } public static ImageIcon resizeIcon(ImageIcon icon, int width, int height) { Image img = icon.getImage(); Image thumbnail = img.getScaledInstance(width, height, Image.SCALE_SMOOTH); return new ImageIcon(thumbnail); } public ImageIcon[] getImages(){ String imageURL[] = new String[30];; /* store images' URLs */ ImageIcon[] image; /* store the images objects. */ BufferedReader in = null; int n = 0; int i,j; sPerformer = sPerformer.replace(' ','+'); sAlbumName = sAlbumName.replace(' ','+'); String url = "http://www.slothradio.com/covers/?adv=&artist=" + sPerformer+ "+&album=" + sAlbumName; System.out.println(url); try { URL imageServer = new URL(url); String inStr = new String(""); in = new BufferedReader (new InputStreamReader(imageServer.openStream())); while (!inStr.equals("")){ inStr = in.readLine(); i = inStr.indexOf("http:"); j = inStr.indexOf(".jpg"); if ((i != -1) && (j!=-1)) { imageURL[n] = inStr.substring(i,j+4); n = n+1; } } }catch (MalformedURLException murl){System.out.println("malformed URL Exception");} /*catch (FileNotFoundException fnfe){}*/ catch (IOException ioe){System.out.println("io exception");} finally { try { if (in != null) in.close(); }catch (IOException ioe){} } if (n > 10) n = 10; image = new ImageIcon[n]; for (i=0; i")){ inStr = in.readLine(); i = inStr.indexOf("http:"); j = inStr.indexOf(".jpg"); if ((i != -1) && (j!=-1)) { imageURL[n] = inStr.substring(i,j+4); n = n+1; } } }catch (MalformedURLException murl){System.out.println("malformed URL Exception");} /*catch (FileNotFoundException fnfe){}*/ catch (IOException ioe){System.out.println("io exception");} finally { try { if (in != null) in.close(); }catch (IOException ioe){} } if (n > 10) n = 10; image = new ImageIcon[n]; for (i=0; i