import java.awt.*;import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.*;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.*;
public class DisplayExample extends JPanel {
ImageIcon[] icons;
List iconList;
public DisplayExample(ImageIcon[] icons) {
this.icons = icons;
iconList = new ArrayList();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for(int j = 0; j < iconList.size(); j++) {
IconStore iconStore = (IconStore)iconList.get(j);
iconStore.draw(g);
}
}
private void display(String s, int x, int y) {
for(int j = 0; j < icons.length; j++) {
if(icons[j].getDescription().equals(s)) {
iconList.add(new IconStore(icons[j], x, y));
break;
}
}
}
private JPanel getUIPanel() {
JPanel panel = new JPanel();
ButtonGroup group = new ButtonGroup();
ActionListener l = new ActionListener() {
public void actionPerformed(ActionEvent e) {
JRadioButton rb = (JRadioButton)e.getSource();
String id = rb.getActionCommand();
int x = 20, y = 20, w = getWidth();
for(int j = 0; j < iconList.size(); j++) {
int dw = ((IconStore)iconList.get(j)).icon.getIconWidth() + 10;
x += dw;
if(x + dw > w) {
y += ((IconStore)iconList.get(j)).icon.getIconHeight() + 10;
x = 20;
}
}
display(id, x, y);
repaint();
}
};
for(int j = 0; j < icons.length; j++) {
String id = icons[j].getDescription();
JRadioButton rb = new JRadioButton(id);
rb.setActionCommand(id);
rb.addActionListener(l);
group.add(rb);
panel.add(rb);
}
return panel;
}
public static void main(String[] args) throws IOException {
String[] ids = { "--", "--g--", "h-", "-t" };
BufferedImage image = null;
ImageIcon[] icons = new ImageIcon[ids.length];
for(int j = 0; j < icons.length; j++) {
String name = "geek" + ids[j];
image = ImageIO.read(new File("images/" + name + ".gif"));
icons[j] = new ImageIcon(image, name);
}
DisplayExample test = new DisplayExample(icons);
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(test);
f.getContentPane().add(test.getUIPanel(), "Last");
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
}
}
class IconStore {
ImageIcon icon;
int x;
int y;
public IconStore(ImageIcon icon, int x, int y) {
this.icon = icon;
this.x = x;
this.y = y;
}
public void draw(Graphics g) {
g.drawImage(icon.getImage(), x, y, null);
}
}
geek images found at bottom of [url=http://java.sun.com/docs/books/tutorial/uiswing/components/examples/index.html]Using Swing Components: Examples[/url]