This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2012-08-18 20:41:09

GeonoTRON2000
Scratcher
Registered: 2009-12-24
Posts: 1000+

Java: Swing Images. HELP!!!

I can't get them to work for the life of me!  Here's what I'm using:

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("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.


http://i.imgur.com/BAEgGDL.png

Offline

 

#2 2012-08-19 06:54:03

TheSuccessor
Scratcher
Registered: 2010-04-23
Posts: 1000+

Re: Java: Swing Images. HELP!!!

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);
    }

}

/* No comment */

Offline

 

#3 2012-08-19 11:43:43

GeonoTRON2000
Scratcher
Registered: 2009-12-24
Posts: 1000+

Re: Java: Swing Images. HELP!!!

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...


http://i.imgur.com/BAEgGDL.png

Offline

 

#4 2012-08-19 12:12:04

TheSuccessor
Scratcher
Registered: 2010-04-23
Posts: 1000+

Re: Java: Swing Images. HELP!!!

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)


/* No comment */

Offline

 

#5 2012-08-19 12:30:10

GeonoTRON2000
Scratcher
Registered: 2009-12-24
Posts: 1000+

Re: Java: Swing Images. HELP!!!

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:

Code:

r = new Random();
Duck d = new Duck();
d.setLocation(r.nextInt(800), r.nextInt(600));
add(d);

http://i.imgur.com/BAEgGDL.png

Offline

 

#6 2012-08-19 15:14:51

poopo
Scratcher
Registered: 2009-09-20
Posts: 1000+

Re: Java: Swing Images. HELP!!!

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("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.


http://i45.tinypic.com/28rnqki.jpg

Offline

 

Board footer