Problems when inserting image from a Thread

0

I'm doing a simulation of a cinema and I'm trying to add an image to a JFrame from a Thread but the image does not appear in the window, this is the code I made

import java.awt.Image;

import javax.swing.ImageIcon;

import javax.swing.JLabel;

public class taquilla implements Runnable {

    public taquilla() {
        // TODO Auto-generated constructor stub
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub

    }

    @Override
    public void run() {
        JLabel taquillero = new JLabel("");
        Image img= new ImageIcon("Taquillero.png").getImage();
        ImageIcon img2=new ImageIcon(img.getScaledInstance(200, 100, Image.SCALE_SMOOTH));

        taquillero.setIcon(img2);
        taquillero.setBounds(100,10,30,30);
        taquillero.setSize(taquillero.getPreferredSize());
        simulador.ventanasim.add(taquillero);
    }

}

In another class is where I start the Thread with this code

public simulator () {

    super("Simulador Cinema");
    setExtendedState(MAXIMIZED_BOTH);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setLayout(null);
    panelImagen p = new panelImagen();
    p.setBorder(new EmptyBorder(5,5,5,5));
    p.setLayout(new BorderLayout(0,0));
    setContentPane(p);

    Thread taquilla[] = new Thread[BotonSetup.empletaq];
    for(int i=0;i<BotonSetup.empletaq;i++) {
        taquilla[i] = new Thread(taquillas1);
        taquilla[i].start();
    }
}
    
asked by Luis Daniel 11.05.2018 в 21:15
source

1 answer

0

I send you these suggestions so you can solve.

Code 1: The images add it in a button. You can change it.

public class ImagenesCine {

public ImagenesCine() {
}


public List<ImageIcon> cargandoImagenes(){
    List<ImageIcon> imagenes = new ArrayList<>();
    imagenes.add(new ImageIcon(getClass().getResource("/modelo/emoticon1.jpeg")));
    imagenes.add(new ImageIcon(getClass().getResource("/modelo/emoticon2.jpeg")));
    imagenes.add(new ImageIcon(getClass().getResource("/modelo/emoticon3.jpeg")));
    imagenes.add(new ImageIcon(getClass().getResource("/modelo/emoticon4.jpeg")));

    return imagenes;
}


public void cineFoto(JButton boton){

    List<ImageIcon> fotograma = this.cargandoImagenes();


    for(ImageIcon imagen : fotograma){
        boton.setIcon(imagen);


        try {
            Thread.sleep(2000);
        } catch (InterruptedException ex) {
            Logger.getLogger(ImagenesCine.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}



}

code 2: Implementation (Netbeans) Within the constructor, implement it in this way ... obvious the rest of the code that generates netbeans for flexibility

   public class Ventana extends javax.swing.JFrame {
    private ImagenesCine ic;       

    public Ventana() {
      initComponents();

      ic = new ImagenesCine();

    new Thread(new Runnable() {
        @Override
        public void run() {
          ic.cineFoto(Cine);
        }
    }).start();

    }
        ////obviando el resto. Cine es un boton o JButton...!!!
    
answered by 12.05.2018 в 00:38