GoogleOlympiad
GoogleOlympiad is a Java program to display each of the 17 Google 2008 Olympic images.
This program demonstrates how to load and cache images from a jar file. An enclosed script shows how to collect a set of images.
Download: Click here to download the jar file.
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.util.*;
import java.util.List;
import javax.swing.*;
import javax.swing.border.*;
/**
* @author John B. Matthews
*/
public class GoogleOlympiad extends JPanel implements ActionListener {
private static final int MAX = 20;
private static final Font sans = new Font("SansSerif", Font.PLAIN, 16);
private static final Border border =
BorderFactory.createMatteBorder(4, 16, 4, 16, Color.lightGray);
private List<String> list = new ArrayList<String>(MAX);
private List<ImageIcon> cache = new ArrayList<ImageIcon>(MAX);
private JLabel imageLabel = new JLabel();
private JButton prevButton = new JButton();
private JButton nextButton = new JButton();
private JComboBox favorites;
public GoogleOlympiad() {
this.setLayout(new BorderLayout());
list.add("opening.gif");
list.add("cycling.gif");
list.add("weightlifting.gif");
list.add("diving.gif");
list.add("rhythm.gif");
list.add("rings.gif");
list.add("basketball.gif");
list.add("badminton.gif");
list.add("soccer.gif");
list.add("rowing.gif");
list.add("pingpong.gif");
list.add("swimming.gif");
list.add("trackfield.gif");
list.add("highjump.gif");
list.add("martialarts.gif");
list.add("baseball.gif");
list.add("closing.gif");
for (int i = 0; i < list.size(); i++) cache.add(i, null);
JLabel titleLabel = new JLabel();
titleLabel.setText("The Google 2008 Olympiad");
titleLabel.setHorizontalAlignment(JLabel.CENTER);
titleLabel.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 24));
titleLabel.setBorder(border);
this.add(titleLabel, BorderLayout.NORTH);
imageLabel.setIcon(getImage(0));
imageLabel.setHorizontalAlignment(JLabel.CENTER);
imageLabel.setBorder(border);
this.add(imageLabel, BorderLayout.CENTER);
favorites = new JComboBox(
list.toArray(new String[list.size()]));
favorites.setActionCommand("favs");
favorites.addActionListener(this);
prevButton.setText("\u22b2Prev");
prevButton.setFont(sans);
prevButton.setActionCommand("prev");
prevButton.addActionListener(this);
nextButton.setText("Next\u22b3");
nextButton.setFont(sans);
nextButton.setActionCommand("next");
nextButton.addActionListener(this);
JPanel controlPanel = new JPanel();
controlPanel.add(prevButton);
controlPanel.add(favorites);
controlPanel.add(nextButton);
controlPanel.setBorder(border);
this.add(controlPanel, BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent ae) {
String cmd = ae.getActionCommand();
if ("favs".equals(cmd)) {
int index = favorites.getSelectedIndex();
ImageIcon image = getImage(index);
imageLabel.setIcon(image);
if (image != null) imageLabel.setText("");
else imageLabel.setText("Image not available.");
}
if ("prev".equals(cmd)) {
int index = favorites.getSelectedIndex() - 1;
if (index < 0) index = list.size() - 1;
favorites.setSelectedIndex(index);
}
if ("next".equals(cmd)) {
int index = favorites.getSelectedIndex() + 1;
if (index > list.size() - 1) index = 0;
favorites.setSelectedIndex(index);
}
}
public JButton getDefault() { return nextButton; }
// Return the (possibly cached) image having the given index.
private ImageIcon getImage(int index) {
ImageIcon image = cache.get(index);
if (image != null) return image;
String name = "images/" + list.get(index);
URL url = GoogleOlympiad.class.getResource(name);
if (url != null) {
image = new ImageIcon(url);
}
cache.set(index, image);
return image;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GoogleOlympiad go = new GoogleOlympiad();
frame.add(go);
frame.setTitle("Google Olympiad");
frame.setSize(400, 300);
frame.setVisible(true);
go.getDefault().requestFocusInWindow();
}
});
}
}
Copyright © 2008 John B. Matthews. Distributed under the terms of the GPL