PHP Code:
/**
* This class contains all interfaces and some event handling functions
*
* @author Hung Vu Pham
* @version 1, 3/12/2012
* @version 2, 5/12/2012
*/
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.Timer;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;
import javax.swing.KeyStroke;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import java.util.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;
import com.aetrion.flickr.*; // For FlickrJ classes.
import com.aetrion.flickr.photos.*;
import java.io.*; //for writing and reading file
public class testUserInterface1 extends JFrame implements ActionListener
{
/**
* @param Declaring all components for user interface
* @param Firslty, to declare components of menu such as
* @param JMenuBar to create a menu bar
* @param JMenu to create a menu on the menu bar such as menu File.
* @param JMenuItem to create a submenu of the menu such as fExit is a submenu of menu File.
*/
private JMenuBar menuBar;
private JMenu file, menu, help;
private JMenuItem fExit, mUpload, mRecentPhoto, mSearch, hGuide, hAuthor;
private JPanel upperPanel, centerPanel, InforPanel,imgPanel, recentPhotoPanel, searchPhotoPanel;
private JLabel welcome, lWelcome, lAbout, recentPhotoTitle, searchTitle;
/**
* @param Declaring Image paths to use by ImageIcon
* ImageIcon whatEverName = new ImageIcon("path to local pc","name of photo");
*
*/
//Welcome Image
ImageIcon iWelcome = new ImageIcon("IMAGE/flickr.jpg", "Flickr Image");
// Icon of Fairy Tail Team
ImageIcon iAbout = new ImageIcon("IMAGE/FairyTail.jpg", "Fairy Tail Team");
// Icons for 5 areas on the earth.
ImageIcon africa = new ImageIcon("IMAGE/Africa.jpg", "Africa");
ImageIcon asia = new ImageIcon("IMAGE/Asia.jpg", "Asia");
ImageIcon america = new ImageIcon("IMAGE/America.jpg", "America");
ImageIcon aus = new ImageIcon("IMAGE/Australia.jpg", "Australia");
ImageIcon eu = new ImageIcon("IMAGE/Europe.jpg", "Australia");
/**
* @author Athina Ioannou
* @author Edited by Hung
* @param this to declare all components that should be in Search Panel
* @param JButton btnStart, btnExit.
* @param JLabel lblSearch
* @param JTextField txtSearch
*/
private JButton btnStart, btnExit, btAfrica, btAsia, btAmerica, btEu, btAus;
private JLabel lblSearch;
private JTextField txtSearch;
private JPanel sContent;
/////////////////////////////////////////////////////////////
private PhotoPanel pp;
private String keyWord;
private String photoUrl;
private PhotoFinder pf;
private Timer timer;
private ArrayList<Photo> photoList;
private String originalKeyWord, oldKeyWord;
/////////////////////////////////////////////////////////////
public testUserInterface1()
{
//Add regular components to the window, using the default BorderLayout.
Container contentPane = getContentPane(); //the app window
// create upper panel to hold menu bar on the top of interface
upperPanel = new JPanel();
contentPane.add(upperPanel, BorderLayout.NORTH);
upperPanel.setLayout(new BorderLayout());
// Create menu bar to hold menus and submenus
menuBar = new JMenuBar();
//add menu bar to the top of interface
upperPanel.add(menuBar);
// Build File menu and its short-cut key
//file = new JMenu ("File");
//file.setMnemonic(KeyEvent.VK_F);
//file.getAccessibleContext().setAccessibleDescription(
// "File related operations");
// Create a submenu for File menu and ist short-cut key
fExit = new JMenuItem ("Exit", KeyEvent.VK_E);
fExit.addActionListener(this);
// add components of File menu
//menuBar.add(file);
//file.add(fExit);
// Build Menu menu and its short-cut key
menu = new JMenu ("Menu");
menu.setMnemonic(KeyEvent.VK_M);
menu.getAccessibleContext().setAccessibleDescription(
"Menu related operations");
// Create submenus for Menu menu and its short-cut key
//mRecentPhoto = new JMenuItem ("Check Recent Photos", KeyEvent.VK_C);
mSearch = new JMenuItem ("Search Photos", KeyEvent.VK_S);
// add components of File menu
menuBar.add(menu);
//menu.add(mRecentPhoto);
//mRecentPhoto.addActionListener(this);
menu.add(mSearch);
mSearch.addActionListener(this);
menu.addSeparator();
menu.add(fExit);
// Build Help Menu and its short-cut key
help = new JMenu ("Help");
help.setMnemonic(KeyEvent.VK_H);
help.getAccessibleContext().setAccessibleDescription(
"Help related operations");
// Create submenus for Help Menu and their short-cut key
hGuide = new JMenuItem ("Short-Cut Key Guide", KeyEvent.VK_S);
hAuthor = new JMenuItem ("About Us", KeyEvent.VK_A);
// add components of help menu
menuBar.add(help);
help.add(hGuide);
hGuide.addActionListener(this);
help.addSeparator();
help.add(hAuthor);
hAuthor.addActionListener(this);
// Build Center Panel which is stayed in the center of interface.
centerPanel = new JPanel();
centerPanel.setLayout (new FlowLayout());
contentPane.add(centerPanel, BorderLayout.CENTER);
// Build Infor Panel and display Image and Text.
InforPanel = new JPanel();
InforPanel.setLayout (new BorderLayout());
centerPanel.add(InforPanel);
// Build components for Infor Panel
welcome = new JLabel ("Welcome to Java Flickr Application", JLabel.CENTER);
InforPanel.add(welcome, BorderLayout.NORTH );
lWelcome = new JLabel (iWelcome);
InforPanel.add(lWelcome);
//Build recent Photo Panel to display most recent photos
//recentPhotoPanel = new JPanel();
//recentPhotoPanel.setLayout (new BorderLayout());
//recentPhotoPanel.setVisible(false);
//recentPhotoTitle = new JLabel ("Most Recent Photos on Flickr");
//recentPhotoPanel.add(recentPhotoTitle);
//centerPanel.add(recentPhotoPanel);
//Build Search Panel to display area where user can search photos with a keyword
searchPhotoPanel = new JPanel ();
searchPhotoPanel.setLayout (new BorderLayout());
searchPhotoPanel.setVisible(false);
searchTitle = new JLabel ("Welcome to Search Area", JLabel.CENTER);
searchPhotoPanel.add(searchTitle, BorderLayout.NORTH);
centerPanel.add(searchPhotoPanel);
//Build all buttons with images
btAfrica = new JButton(africa);
btAsia = new JButton(asia);
btAmerica = new JButton(america);
btEu = new JButton(eu);
btAus = new JButton(aus);
//Build Photo Pane to contain button and photo panel
JPanel photoPane = new JPanel();
photoPane.setLayout (new BorderLayout());
//Build Button Pane to contain all image button
JPanel buttonPane = new JPanel();
buttonPane.setLayout( new FlowLayout());
buttonPane.add(btAfrica);
buttonPane.add(btAsia);
buttonPane.add(btAmerica);
buttonPane.add(btEu);
buttonPane.add(btAus);
photoPane.add(buttonPane,BorderLayout.NORTH );
searchPhotoPanel.add(photoPane, BorderLayout.SOUTH);
/**
* @author Athina Ioannou
* @author Edited by Hung
* @param code below to build all components that should be in Search Panel
* @param adding all components and display them in Search Panel
* @param Handling events when user clicks on button Search
* @param JTextField txtSearch
*/
// Build Components for Search Content Panel
sContent = new JPanel(); // content Panel to display conents
sContent.setLayout( new FlowLayout());
lblSearch = new JLabel("Search");
txtSearch = new JTextField(20);
btnStart = new JButton("Start");
btnExit = new JButton("Exit");
//Add components to Search Panel and action handling
sContent.add(lblSearch);
sContent.add(txtSearch);
sContent.add(btnStart);
btnStart.addActionListener(this);
sContent.add(btnExit);
btnExit.addActionListener(this);
searchPhotoPanel.add(sContent, BorderLayout.CENTER);
// Build photo panel, photofinder and strings
pp = new PhotoPanel();
pp.setLayout (new FlowLayout());
photoPane.add(pp);
photoPane.setSize(600,400);
//searchPhotoPanel.add(photoPane, BorderLayout.SOUTH);
pf = new PhotoFinder();
photoList = new ArrayList<Photo>();
originalKeyWord = "";
oldKeyWord = "";
//titles = new ArrayList<String>();
timer = new Timer(1000, this);
}
public void displayUserInterface()
{
testUserInterface1 test = new testUserInterface1 ();
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test.setTitle("Java Flickr Application");
test.setSize(800,600);
test.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() instanceof JMenuItem)
{
JMenuItem source = (JMenuItem)(e.getSource());
//If user selects "Short-cut Key Guide" sub-menu, a dialog will be shown
if (source.equals(hGuide))
{
JOptionPane.showMessageDialog( testUserInterface1.this,
"Select appropriate menu option that you want from Java Flickr Application\n" +
"User should navigate to menu before submenu\n" +
"Alt + F => select File menu \n" +
"Alt + E => select Exit submenu \n" +
"Alt + M => select Menu menu \n" +
"Alt + C => select Check Recent Photos submenu \n" +
"Alt + S => select Search submenu \n" +
"Alt + H => select Help menu \n" +
"Alt + S => select Short-cut Key Guide submenu \n" +
"Alt + A => select About Us submenu \n" ,
"Help Contents", JOptionPane.PLAIN_MESSAGE);
}
//If user selects "About Us" sub-menu, a dialog will be shown
if (source.equals(hAuthor))
{
JOptionPane.showMessageDialog( testUserInterface1.this,
"Java Flickr Application is created by Fairy Tail Group \n\n" +
"Hung Vu Pham\n\n" +
"Jennifer Naiaretti\n\n" +
"Panagiota Tzana\n\n" +
"Athina Ioannou",
"About Us", JOptionPane.PLAIN_MESSAGE, iAbout);
}
//If user selects "Check Recent Photos" sub-menu, a panel to retrieve all photos from Flickr and display photos
//if (source.equals(mRecentPhoto))
//{
// InforPanel.setVisible(false);
// searchPhotoPanel.setVisible(false);
// recentPhotoPanel.setVisible(true);
//}
//If user selects "Search Photos" sub-menu, a panel to help user search photos on Flickr with specific keyword
//This panel also contains all photo after searching.
if (source.equals(mSearch))
{
InforPanel.setVisible(false);
//recentPhotoPanel.setVisible(false);
searchPhotoPanel.setVisible(true);
searchPhotoPanel.setSize(500,500);
}
//If "Exit" sub-menu is selected, a window closes.
if (source.equals(fExit))
{
dispose();
System.exit(0);
}
}
if (e.getSource() == btnExit)
{
dispose();
System.exit(0);
}
if (e.getSource() == btnStart)
{
if (timer.isRunning())
{
timer.stop();
}
keyWord = txtSearch.getText();
originalKeyWord = keyWord;
oldKeyWord = keyWord;
timer.start();
}
else
{
if (keyWord != null)
{
Random r = new Random();
photoList.clear();
photoList = pf.doQuery(keyWord);
int i = r.nextInt(photoList.size());
pp.setImage(photoList.get(i).getSmallUrl());
keyWord = photoList.get(i).getTitle();
Iterator it = photoList.iterator();
while (keyWord.equals(""))
{
Photo p = (Photo)it.next();
keyWord = p.getTitle();
}
//if (oldKeyWord.equals(keyWord))
//{
// keyWord = originalKeyWord;
// }
//oldKeyWord = keyWord;
keyWord = originalKeyWord;
{
//int num = photoList.size();
//for (int k=1; k < num; k++)
//jLabel[m].setIcon(photoList[m]);
pp.repaint();
}
System.out.println(keyWord);
}
}
}
}