●作ったもの
flickrの写真を検索し、二重円の形に表示するブラウザ
●作り方
以下コードと解説
☆このプログラムでは4つのクラス(FlickrPhoto、FlickrREST.java、FlickrPhotoPanel.java、FlickrViewer.java)を作ることが必要。それぞれに、newで宣言されたインスタンスを作成することでプログラミングしていく。ボタン、リストなどをコンポーネントと呼ぶが、それを複数収納できるコンテナを利用する。
・//FlickrPhoto.java//
//importする//
import java.awt.image.*;
import javax.imageio.*;
import java.net.*;
import java.io.IOException;
public class FlickrPhoto{
//string型の変数宣言する\\
String id;
String owner;
String secret;
String server;
String farm;
String title;
//boolean型の変数宣言する//
boolean ispublic;
boolean isfriend;
boolean isfamily;
//BufferedImageを使って変数宣言http://goo.gl/koKYO//
BufferedImage img75x75;
//FlickrPhoto関数//
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();
}
}//end of update75x75xImg()//
public BufferedImage getImg75(){
return img75x75;
}//end of getImg75()
}//end of FlickrPhoto
・//FlickrREST.java//
//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{
ArrayListflickrObjList;
//コンストラクタ//
public FlickrREST(){
this.flickrObjList=new ArrayList();
}//end of 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();
}
}//end of sendRequestAndSetObj()//
public ArrayList getFlickrObjList(){
return this.flickrObjList;
}//end of ArrayList getFlickrObjectList()//
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();
}
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();
}
}//end of dom2ObjList(Document dom)//
}//end of public class FlickrREST//
・//FlickrPhotoPanel.java//
//import//
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import java.util.*;
public class FlickrPhotoPanel extends JPanel{
ArrayListphotoImgList;//この中身のリストはBufferdImage//
FlickrPhotoPanel(){
photoImgList = new ArrayList();
}
public void setPhotoList(ArrayListflickrObjectList){
for(FlickrPhoto photo : flickrObjectList){//FlickrPhotoのリストから画像だけのリストを作る//
photoImgList.add(photo.getImg75());//リストに追加//
}
}//end of setPhotoList
//上書き(オーバーライド)//
public void paint(Graphics g){
int width = this.getWidth();
int height = this.getHeight();
int radius = width < height ? width/2-50 : height/2-50;
g.setColor(getBackground());
g.fillRect(0,0,width,height);
int photoNum = photoImgList.size();
if(0 < photoNum){
double angle = 2.0*Math.PI/photoNum;
for(int i=0; i int x=(int)(width/2 + radius*Math.cos(angle*i));
int y=(int)(height/2 + radius*Math.sin(angle*i));
g.drawImage(photoImgList.get(i), x-74/10, y-74/10, 50, 50, this);
int a=(int)(width/2 + radius*Math.cos(angle*i))/2;
int b=(int)(height/2 + radius*Math.sin(angle*i))/2;
g.drawImage(photoImgList.get(i), a+150, b+100, 50, 50, this);
}
}
}
}//end of FlickrPhotoPanel//
・//FlickrViewer.java//
//import//
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FlickrViewer extends JFrame implements ActionListener{
JTextField addrField;
FlickrPhotoPanel flickrPhotoPanel;
FlickrREST flickrREST;
//自分のAPIキーを登録//
static String flickrApiKey="80a2d2660ef4c13532e47bc365804dd9";
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);
}//end of FlickerViewer//
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();
}
☆↑がイベント(入力するとflickrにリクエストを送る)とイベントリスナー(this.flickrREST.sendRequestAndSetObj)の関係になっている。
public static void main(String args[]){
FlickrViewer fv = new FlickrViewer();
}
}

flickrの写真を検索し、二重円の形に表示するブラウザ
●作り方
以下コードと解説
☆このプログラムでは4つのクラス(FlickrPhoto、FlickrREST.java、FlickrPhotoPanel.java、FlickrViewer.java)を作ることが必要。それぞれに、newで宣言されたインスタンスを作成することでプログラミングしていく。ボタン、リストなどをコンポーネントと呼ぶが、それを複数収納できるコンテナを利用する。
・//FlickrPhoto.java//
//importする//
import java.awt.image.*;
import javax.imageio.*;
import java.net.*;
import java.io.IOException;
public class FlickrPhoto{
//string型の変数宣言する\\
String id;
String owner;
String secret;
String server;
String farm;
String title;
//boolean型の変数宣言する//
boolean ispublic;
boolean isfriend;
boolean isfamily;
//BufferedImageを使って変数宣言http://goo.gl/koKYO//
BufferedImage img75x75;
//FlickrPhoto関数//
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();
}
}//end of update75x75xImg()//
public BufferedImage getImg75(){
return img75x75;
}//end of getImg75()
}//end of FlickrPhoto
・//FlickrREST.java//
//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
//コンストラクタ//
public FlickrREST(){
this.flickrObjList=new ArrayList
}//end of 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();
}
}//end of sendRequestAndSetObj()//
public ArrayList getFlickrObjList(){
return this.flickrObjList;
}//end of ArrayList getFlickrObjectList()//
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();
}
try{
Element root = dom.getDocumentElement();
NodeList childNodes = root.getChildNodes();
Node photosNode = null;
for(int i=0; i
if(node.getNodeName().equalsIgnoreCase("photos")){
photosNode = node;
break;
}
}
childNodes = photosNode.getChildNodes();
for(int i=0; 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();
}
}//end of dom2ObjList(Document dom)//
}//end of public class FlickrREST//
・//FlickrPhotoPanel.java//
//import//
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import java.util.*;
public class FlickrPhotoPanel extends JPanel{
ArrayList
FlickrPhotoPanel(){
photoImgList = new ArrayList
}
public void setPhotoList(ArrayList
for(FlickrPhoto photo : flickrObjectList){//FlickrPhotoのリストから画像だけのリストを作る//
photoImgList.add(photo.getImg75());//リストに追加//
}
}//end of setPhotoList
//上書き(オーバーライド)//
public void paint(Graphics g){
int width = this.getWidth();
int height = this.getHeight();
int radius = width < height ? width/2-50 : height/2-50;
g.setColor(getBackground());
g.fillRect(0,0,width,height);
int photoNum = photoImgList.size();
if(0 < photoNum){
double angle = 2.0*Math.PI/photoNum;
for(int i=0; i
int y=(int)(height/2 + radius*Math.sin(angle*i));
g.drawImage(photoImgList.get(i), x-74/10, y-74/10, 50, 50, this);
int a=(int)(width/2 + radius*Math.cos(angle*i))/2;
int b=(int)(height/2 + radius*Math.sin(angle*i))/2;
g.drawImage(photoImgList.get(i), a+150, b+100, 50, 50, this);
}
}
}
}//end of FlickrPhotoPanel//
・//FlickrViewer.java//
//import//
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FlickrViewer extends JFrame implements ActionListener{
JTextField addrField;
FlickrPhotoPanel flickrPhotoPanel;
FlickrREST flickrREST;
//自分のAPIキーを登録//
static String flickrApiKey="80a2d2660ef4c13532e47bc365804dd9";
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);
}//end of FlickerViewer//
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();
}
☆↑がイベント(入力するとflickrにリクエストを送る)とイベントリスナー(this.flickrREST.sendRequestAndSetObj)の関係になっている。
public static void main(String args[]){
FlickrViewer fv = new FlickrViewer();
}
}


