All pastes #267529 Raw Edit

Something

public text v1 · immutable
·published 2006-12-04 22:33 UTC
rendered paste body
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("</html>")){
                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<n;i++){
            
            try{
                URL iUrl = new URL(imageURL[i]);
                image[i] = new ImageIcon(iUrl);
            }catch(MalformedURLException murl){System.err.println("create images didn't successfully complete");}
            
        }
            
            return image;
    }
    public void actionPerformed(ActionEvent e) 
    {
        if (e.getActionCommand().equals("0"))
        {    
            selectedImage = 0;
            frame.setVisible(false);
        }
        else if (e.getActionCommand().equals("1"))
        {    
            selectedImage = 1;
            frame.setVisible(false);
        }
        else if (e.getActionCommand().equals("2"))
        {    
            selectedImage = 2;
            frame.setVisible(false);
        }
        else if (e.getActionCommand().equals("3"))
        {    
            selectedImage = 3;
            frame.setVisible(false);
        }
        else if (e.getActionCommand().equals("4"))
        {    
            selectedImage = 4;
            frame.setVisible(false);
        }
        else if (e.getActionCommand().equals("5"))
        {    
            selectedImage = 5;
            frame.setVisible(false);
        }
        else if (e.getActionCommand().equals("6"))
        {    
            selectedImage = 6;
            frame.setVisible(false);
        }
        else if (e.getActionCommand().equals("7"))
        {    
            selectedImage = 7;
            frame.setVisible(false);
        }
        else if (e.getActionCommand().equals("8"))
        {    
            selectedImage = 8;
            frame.setVisible(false);
        }
        else if (e.getActionCommand().equals("9"))
        {    
            selectedImage = 9;
            frame.setVisible(false);
        }
        else if (e.getActionCommand().equals("cancel"))
        {
                selectedImage = -1;
                frame.setVisible(false);
        }
    }
}
mport 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("</html>")){
                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<n;i++){
            
            try{
                URL iUrl = new URL(imageURL[i]);
                image[i] = new ImageIcon(iUrl);
            }catch(MalformedURLException murl){System.err.println("create images didn't successfully complete");}
            
        }
            
            return image;
    }
    public void actionPerformed(ActionEvent e) 
    {
        if (e.getActionCommand().equals("0"))
        {    
            selectedImage = 0;
            frame.setVisible(false);
        }
        else if (e.getActionCommand().equals("1"))
        {    
            selectedImage = 1;
            frame.setVisible(false);
        }
        else if (e.getActionCommand().equals("2"))
        {    
            selectedImage = 2;
            frame.setVisible(false);
        }
        else if (e.getActionCommand().equals("3"))
        {    
            selectedImage = 3;
            frame.setVisible(false);
        }
        else if (e.getActionCommand().equals("4"))
        {    
            selectedImage = 4;
            frame.setVisible(false);
        }
        else if (e.getActionCommand().equals("5"))
        {    
            selectedImage = 5;
            frame.setVisible(false);
        }
        else if (e.getActionCommand().equals("6"))
        {    
            selectedImage = 6;
            frame.setVisible(false);
        }
        else if (e.getActionCommand().equals("7"))
        {    
            selectedImage = 7;
            frame.setVisible(false);
        }
        else if (e.getActionCommand().equals("8"))
        {    
            selectedImage = 8;
            frame.setVisible(false);
        }
        else if (e.getActionCommand().equals("9"))
        {    
            selectedImage = 9;
            frame.setVisible(false);
        }
        else if (e.getActionCommand().equals("cancel"))
        {
                selectedImage = -1;
                frame.setVisible(false);
        }

        searchTest.setNewIcon(image[selectedIcon]);
    }
}
 
=== TEST class====
==================

import javax.swing.*;
import java.awt.*;

public class searchTest 
{
    private JLabel myLabel;

    public static void main(String args[])
    {
        RetrivalAlbumCover rac = new RetrivalAlbumCover();
        ImageIcon icon = rac.go("kiss", "");
        JFrame j = new JFrame("album covers");    
        JLabel myLabel;
        myLabel = new JLabel(resizeIcon(icon, 120, 120));
        j.add(myLabel);
        j.setSize(300, 300);    
        j.setVisible(true);
    }
    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 void setNewIcon(ImageIcon newIcon)
    {
        JFrame.remove(myLabel);
        myLabel = new JLabel(newIcon);
        JFrame.add(myLabel);
    }
}