Skip to content

Instantly share code, notes, and snippets.

@manusa
Last active February 7, 2021 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manusa/04ed943ee0de062126143d3c806be783 to your computer and use it in GitHub Desktop.
Save manusa/04ed943ee0de062126143d3c806be783 to your computer and use it in GitHub Desktop.
/*
* ImageButton.java
*
* Created on 30 de mayo de 2007, 8:35
*/
package blogtests;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
/**
*
* @author Marc Nuri San Félix
*/
public class ImageButton extends javax.swing.JFrame {
/** Creates new form imageButton */
public imageButton() {
initComponents();
test();
}
public void test(){
try {
URL pepe = new URL("http://search.yahooapis.com/ImageSearchService/V1/imageSearch?appid=YahooDemo&query=client%20icon&results=1&format=gif");
BufferedReader br = new BufferedReader(new InputStreamReader(pepe.openStream()));
String text ="";
String line = null;
while((line = br.readLine()) != null){
text += line+"\n";
}
text = text.substring(text.indexOf("<Thumbnail>"));
text = text.substring(text.indexOf("<Url>")+5, text.indexOf("</Url>"));
BufferedImage img = ImageIO.read(new URL(text));
jButton1.setIcon(
new ImageIcon(img));
} catch (MalformedURLException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
private void initComponents() {
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("Customers");
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(jButton1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(305, 305, 305))
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(jButton1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>//GEN-END:initComponents
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new imageButton().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton1;
// End of variables declaration//GEN-END:variables
}
@manusa
Copy link
Author

manusa commented Feb 7, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment