mercredi 31 août 2016

Java thread must display new random image

At the moment my code only displays 1 random image each time it executes. I do not know how to make it display one "every 2 secs" for example. (I know its not perfect and ill fix the rest) I have three files for my code. FIRST

public class Main {
    private static final String photos[] = {"icon1.jpg", "icon2.jpg", "icon3.jpg"};
    private static final Color colors[] = {Color.black,Color.red};
    private static Random random = new Random();
    private static JFrame frame = new JFrame();
    private static FirstJFrame FirstJFrame = new FirstJFrame();

    public static void main(String args[]) throws InterruptedException{
        // This part is for displaying the date


        Thread thread = new SetRandomPhoto();
        thread.join();

        FirstJFrame.jButton1.addActionListener(new LoggingButtonActionListener());
        FirstJFrame.setResizable(false);
        FirstJFrame.setVisible(true);
    }   

    private static void stop(){
        thread.stop();
        frame.dispose();
        FirstJFrame.dispose();
    }
    private static void start() {
        thread.start();
    }

    public static class LoggingButtonActionListener implements ActionListener {
    @Override
        public void actionPerformed(ActionEvent e) {
            frame.name.setText(FirstJFrame.jTextField1.getText());
            frame.setResizable(false);
            frame.setVisible(true);
        }
    }

    public static class StopButtonActionListener implements ActionListener {
    @Override
        public void actionPerformed(ActionEvent e) {
            stop();
        }
    }
    public static class StartButtonActionListener implements ActionListener {
    @Override
        public void actionPerformed(ActionEvent e) {
            thread.start();
        }
    }

    private static class SetRandomPhoto extends Thread {
    @Override
        public void run() {
            int index = random.nextInt(photos.length);
            System.out.println(index);

            frame.image.setIcon(new ImageIcon(photos[index]));
        }
    }

}

SECOND

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.ImageIcon;
public class FirstJFrame extends javax.swing.JFrame {

    public FirstJFrame() {
        initComponents();
    }

 @SuppressWarnings("unchecked")
    private void initComponents() {
        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        jButton1.setFont(new java.awt.Font("Verdana", 0, 18)); 
        jButton1.setText("ok");
        jLabel1.setFont(new java.awt.Font("Verdana", 0, 24)); 
        jLabel1.setText("Enter name:");
        jTextField1.setFont(new java.awt.Font("Verdana", 0, 18)); 
        jTextField1.setToolTipText("");
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jLabel1)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(jTextField1,javax.swing.GroupLayout.PREFERRED_SIZE, 225, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
            .addGap(101, 101, 101)
            .addComponent(jButton1,
            javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)))
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,Short.MAX_VALUE)) 
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
            layout.createSequentialGroup()
            .addGap(71, 71, 71)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1)
            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE,29, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(18, 18, 18)
            .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 44,
            javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,Short.MAX_VALUE))
         );
         pack();
    }

    public static void main(String args[]) {
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FirstJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FirstJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FirstJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FirstJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FirstJFrame().setVisible(true);
            }
        });
    }

    public javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    public javax.swing.JTextField jTextField1;
}

THIRD

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.ImageIcon;
public class JFrame extends javax.swing.JFrame {

    public JFrame() {
        initComponents();
    }

 @SuppressWarnings("unchecked")

    private void initComponents() {
        image = new javax.swing.JLabel();
        name = new javax.swing.JLabel();
        stop = new javax.swing.JButton();
        start = new javax.swing.JButton();
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        name.setText("jLabel1");
        stop.setFont(new java.awt.Font("Verdana", 0, 14)); 
        stop.setText("Stop Thread");
        start.setFont(new java.awt.Font("Verdana", 0, 14)); 
        start.setText("Start Thread");
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
            .addGap(86, 86, 86)
            .addComponent(image, javax.swing.GroupLayout.PREFERRED_SIZE,
            193, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
            .addGap(138, 138, 138)
            .addComponent(stop))
            .addGroup(layout.createSequentialGroup()
            .addGap(162, 162, 162)
            .addComponent(name)))
            .addContainerGap(121, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(name)
            .addGap(33, 33, 33)
            .addComponent(image, javax.swing.GroupLayout.PREFERRED_SIZE, 165,
            javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(18, 18, 18)
            .addComponent(stop)
            .addContainerGap(34, Short.MAX_VALUE))
        );
        image.getAccessibleContext().setAccessibleName("image");
        pack();
    }

    public static void main(String args[]) {

        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JFrame.class.getName()).log(java.util.logging.Level.SEVERE,null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JFrame.class.getName()).log(java.util.logging.Level.SEVERE,null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JFrame.class.getName()).log(java.util.logging.Level.SEVERE,null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JFrame.class.getName()).log(java.util.logging.Level.SEVERE,null, ex);
        }

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new JFrame().setVisible(true);
            }
        });
    }

    public javax.swing.JLabel image;
    public javax.swing.JLabel name;
    public javax.swing.JButton stop;
    public javax.swing.JButton start;
}




Aucun commentaire:

Enregistrer un commentaire