Abstract Window Toolkit (AWT) - ImageIcon help!

I've been reading everything in the forums and still I'm stuck...

In my program I have a method:

publicvoid display(String s,int x,int y){ ...}

I'd like display to add an ImageIcon at x,y and display it on the screen. When display is called at a later time, I'd like that image to be added to the same screen, so that the calls

display("1.png", 0, 0);

display("2.png", 10, 0);

...

will display 1.png and 2.png right next to each other (assuming they're 10 pixels wide). What is the simplest way to do this?

[959 byte] By [dijitarua] at [2007-11-26 23:15:45]
# 1

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]

crwooda at 2007-7-10 14:15:38 > top of Java-index,Desktop,Core GUI APIs...
# 2

I tried that code, ran it, it worked. Interestingly enough, only the last picture seems to display still. My other .java uses the display() method, but here is the .java I've been playing around with to just try and get MORE than one picture displayed. When I want to add a picture, I create a new JComponent and throw that on top of the JFrame...is this wrong?

import java.awt.*;

import javax.swing.*;

public class Display extends JComponent {

static JFrame frame = new JFrame("Display");

ImageIcon image;

int x,y;

public Display(int number, int x_coord, int y_coord) {

image = new ImageIcon("./img/" + number + ".png");

this.x = x_coord;

this.y = y_coord;

}

public void paintComponent(Graphics g) {

image.paintIcon(this, g, x, y);

super.paintComponent(g);

}

public static void main(String args[]) {

frame.add(new Display(8,0,0)); // add "8.png" at 0,0 to JFrame

frame.add(new Display(6,185,0));// add "6.png" at 185,0 to JFrame

frame.setSize(600,600);

frame.setVisible(true);

}

}

Only the last "frame.add..." shows up, presumably because some layer is on top of the first. How do I fix this?

dijitarua at 2007-7-10 14:15:38 > top of Java-index,Desktop,Core GUI APIs...
# 3

Use JLabel instead of painting the icon yourself.

If you want to use [url http://java.sun.com/docs/books/tutorial/uiswing/layout/none.html]Absolute Positioning[/url] you have to set the layout manager of the content pane to null.

import javax.swing.ImageIcon;

import javax.swing.JFrame;

import javax.swing.JLabel;

public class Display extends JLabel {

public Display(int number, int x, int y) {

super(new ImageIcon("./img/" + number + ".png"));

setBounds(x, y, getIcon().getIconWidth(), getIcon().getIconHeight());

}

public static void main(String args[]) {

JFrame frame = new JFrame("Display");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.getContentPane().setLayout(null);

frame.add(new Display(8,0,0)); // add "8.png" at 0,0 to JFrame

frame.add(new Display(6,185,0));// add "6.png" at 185,0 to JFrame

frame.setSize(600,600);

frame.setVisible(true);

}

}

Rodney_McKaya at 2007-7-10 14:15:38 > top of Java-index,Desktop,Core GUI APIs...
# 4
Ahhh. Thanks much rodney_m. That solves most of my problems. The only problem now is...nothing shows up. =/ I don't need a paintComponent() in Display ?
dijitarua at 2007-7-10 14:15:38 > top of Java-index,Desktop,Core GUI APIs...