I can't get them to work for the life of me! Here's what I'm using:
package org.thegt.duckhunt;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
public class Duck extends Container {
private ImageIcon i;
private JLabel l;
public Duck () {
i = new ImageIcon("resources/images/duck.png");
l = new JLabel(i);
l.setSize(i.getIconWidth(), i.getIconHeight());
l.setLocation(0, 0);
add(l);
}
}Can someone help? My image (in the resources.images package) is showing up as nothing.
Offline
Does this work?
package org.thegt.duckhunt;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
public class Duck extends Container {
private ImageIcon i;
private JLabel l;
public Duck () {
i = new ImageIcon(Duck.class.getResource("resources/images/duck.png"));
l = new JLabel(i);
l.setSize(i.getIconWidth(), i.getIconHeight());
l.setLocation(0, 0);
add(l);
}
}Offline
TheSuccessor wrote:
Does this work?
Code:
package org.thegt.duckhunt; import javax.imageio.ImageIO; import javax.swing.*; import java.awt.*; import java.awt.image.*; import java.io.*; public class Duck extends Container { private ImageIcon i; private JLabel l; public Duck () { i = new ImageIcon(Duck.class.getResource("resources/images/duck.png")); l = new JLabel(i); l.setSize(i.getIconWidth(), i.getIconHeight()); l.setLocation(0, 0); add(l); } }
Still shows up as nothing...
Offline
If it's not too much trouble, can you post a bit more of your code (namely the bit which adds Duck objects to itself)?
Last edited by TheSuccessor (2012-08-19 12:12:15)
Offline
TheSuccessor wrote:
If it's not too much trouble, can you post a bit more of your code (namely the bit which adds Duck objects to itself)?
Sorry, I deleted it, but it was:
r = new Random(); Duck d = new Duck(); d.setLocation(r.nextInt(800), r.nextInt(600)); add(d);
Offline
package org.thegt.duckhunt;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
public class Duck extends Container {
private ImageIcon i;
private JLabel l;
public Duck () {
i = new ImageIcon("resources/images/duck.png");
l = new JLabel(i);
add(l);
}
}setLocation is not a method in JLabel and I'm pretty sure that they already size themselves to their thumbnails.
Offline