// Import
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class FlickrViewer extends JFrame implements ActionListener{
JTextField addrField;
FlickrPhotoPanel flickrPhotoPanel;
FlickrREST flickrREST;
static String flickrApiKey = "af31386ccc6c58fe0fa974c83e00c072"; //ここは自分の取得したAPIキーをいれる

--------------------------------------------------------------------------------
ここでまず、コンテナとコンポーネントについて説明します。
コンテナとは、他の部品を配置できる部品です。
今回でいうと、eclipseを実行したときに出てくる入力バーの配置してあるスペースや、
enterを押して表示される写真が出てくるスペース全体をコンテナといいます。
コンポーネントとは、コンテナの中に実際に配置されるもののことを言います。
今回でいうと、入力バーや、表示される写真がコンポーネントといえます。
--------------------------------------------------------------------------------



public FlickrViewer(){
super("FlickrViewer");

this.addrField = new JTextField(""); //これがコンポーネント(入力バー)
this.addrField.addActionListener(this);// これもコンポーネント(写真表示の方)
this.getContentPane().add(addrField, BorderLayout.NORTH);
this.flickrPhotoPanel = new FlickrPhotoPanel();
this.getContentPane().add(this.flickrPhotoPanel,
BorderLayout.CENTER);
this.flickrREST = new FlickrREST();

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(640, 480);
this.setVisible(true);
}//↑FlickrViewerおわり
--------------------------------------------------------------------------------

次に、イベントとイベントリスナーについて説明します。

イベントとは、マウスやリターンが押される、ボタンが押される、ウィンドウが閉じられるなどの出来事のことで、
イベントリスナーとは、イベントの受け取り手で、送られるイベントに対応して、イベントリスナーが部品として設定されます。
ここから下のソースコードが、これらの設定部分です。
--------------------------------------------------------------------------------

public void actionPerformed(ActionEvent e){ //イベントリスナーとしての機能
String keyword = addrField.getText();
String request2flickr = new String(
"http://flickr.com/services/rest/?method=flickr.photos.search&per_page=10&api_key="+
flickrApiKey +
"&tags="+
keyword
);
this.flickrREST.sendRequestAndSetObj(request2flickr);
this.flickrPhotoPanel.setPhotoList
(this.flickrREST.getFlickrObjList());
this.flickrPhotoPanel.repaint();
}
public static void main (String args[]) {
FlickrViewer fv = new FlickrViewer();
}
}
// import
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

// コンストラクタ
public class FlickrREST {
ArrayList flickrObjList;
public FlickrREST(){
this.flickrObjList = new ArrayList();
}
//↑public FlickrRESTおわり

public void sendRequestAndSetObj(String request){
try{
URL url = new URL(request);
InputStream input = url.openStream();
Document dom = response2DOM(input);
dom2ObjList(dom);
}catch(Exception e){
e.printStackTrace();
}
}//↑sendRequestAndSetObj()おわり

public ArrayList getFlickrObjList(){
return this.flickrObjList;
}//↑ArrayList getFlickrObjList() おわり

private Document response2DOM(InputStream input){
try {
DocumentBuilder builder =
DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document dom = builder.parse(input);
return dom;
}catch(Exception e){
e.printStackTrace();
return null;
}
}
private void dom2ObjList(Document dom){
if(this.flickrObjList == null){ //エラーのとき
this.flickrObjList = new ArrayList();
}else{
this.flickrObjList.clear(); //1度clearにする
}
try{
Element root = dom.getDocumentElement();
NodeList childNodes = root.getChildNodes();
Node photosNode = null;
for(int i=0; i Node node = childNodes.item(i);
if(node.getNodeName().equalsIgnoreCase("photos")){
photosNode = node;
break;
}
}
childNodes = photosNode.getChildNodes();
for(int i=0; i Node node = childNodes.item(i);
if(node.getNodeName().equalsIgnoreCase("photo")){
String id = node.getAttributes
().getNamedItem("id").getTextContent();
String owner = node.getAttributes
().getNamedItem("owner").getTextContent();
String secret = node.getAttributes
().getNamedItem("secret").getTextContent();
String server = node.getAttributes
().getNamedItem("server").getTextContent();
String farm = node.getAttributes
().getNamedItem("farm").getTextContent();
String title = node.getAttributes
().getNamedItem("title").getTextContent();
String ispublic = node.getAttributes
().getNamedItem("ispublic").getTextContent();
String isfriend = node.getAttributes
().getNamedItem("isfriend").getTextContent();
String isfamily = node.getAttributes
().getNamedItem("isfamily").getTextContent();
FlickrPhoto dst = new FlickrPhoto(
id,
owner,
secret,
server,
farm,
title,
ispublic,
isfriend,
isfamily
);
flickrObjList.add(dst);
}
}
}catch(Exception e){
e.printStackTrace();
}
}//↑dom2ObjList(Document dom)おわり
}//↑public class FlickrRESTおわり
//↓importする
import java.awt.image.*;
import javax.imageio.*;
import java.net.*;
import java.io.IOException;

//↓public classをつくる

public class FlickrPhoto {
String id;
String owner;
String secret;
String server;
String farm;
String title;
boolean ispublic;
boolean isfriend;
boolean isfamily;
BufferedImage img75x75;

//↓FlickerPhoto関数
FlickrPhoto(
String in_id,
String in_owner,
String in_secret,
String in_server,
String in_farm,
String in_title,
String in_ispublic,
String in_isfriend,
String in_isfamily
){
this.id = in_id;
this.owner = in_owner;
this.secret = in_secret;
this.server = in_server;
this.farm = in_farm;
this.title = in_title;
this.ispublic = in_ispublic.equals("1");
this.isfriend = in_isfriend.equals("1");
this.isfamily = in_isfamily.equals("1");
//update75x75xImg()関数の実行
this.update75x75xImg();
}
public void update75x75xImg(){
String urlString = new String(
"http://farm" +
this.farm +
".static.flickr.com/" +
this.server +
"/" +
this.id +
"_" +
this.secret +
"_s.jpg"
);
try{
img75x75 = ImageIO.read(new URL(urlString));
}catch(IOException ioe){
ioe.printStackTrace();
}
}
public BufferedImage getImg75(){
return img75x75;
}
}//おわり