Question #61311

Hi there i am so sorry to bother with such a length question but i am unable to do it. I am struggling with the following question:

Once the user starts the thread by clicking a button, the program must choose a random image out of an image array, and display the image in the center. When the program starts, the user must be prompted for their name. The time must also be displayed (hh:mm) and must be part of the thread, to keep it updated. The pictures name must be displayed in the appropriate box. When the user stops the thread, the picture name must be cleared and the component in which the image was found must be set to a random color. The thread must also be stopped when the exit button is clicked.

Expert's answer

Answer on question #61311, Programming & Computer Science / Java | JSP | JSF | for completion

Once the user starts the thread by clicking a button, the program must choose a random image out of an image array, and display the image in the center. When the program starts, the user must be prompted for their name. The time must also be displayed (hh:mm) and must be part of the thread, to keep it updated. The pictures name must be displayed in the appropriate box. When the user stops the thread, the picture name must be cleared and the component in which the image was found must be set to a random color. The thread must also be stopped when the exit button is clicked.

Solution

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package forpeter;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.ImageIcon;
public class Main {
private static final String photos[] = {"icon1.jpg", "icon1.jpg", "icon1.jpg"};
private static final Color colors[] = {Color.black,Color.red};
private static Random random = new Random();
private static JFrame frame = new JFrame();
private static LoggingJFrame loggingJFrame = new LoggingJFrame();
public static void main(String args[]) throws InterruptedException{
Thread thread = new SetRandomPhoto();
thread.start();
thread.join();
loggingJFrame.jButton1.addActionListener(new LoggingButtonActionListener());
loggingJFrame.setResizable(false);
loggingJFrame.setVisible(true);
}
private static void stop(){
frame.dispose();
loggingJFrame.dispose();
}
public static class LoggingButtonActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
frame.name.setText(loggingJFrame.jTextField1.getText());
frame.setResizable(false);
frame.setVisible(true);
}
}
}
public static class StopButtonActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
stop();
}
}
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]));
}
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package forpeter;
/**
*
* @author admin
*/
public class LoggingJFrame extends javax.swing.JFrame {
/**
* Creates new form LoggingJFrame
*/
public LoggingJFrame() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
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);</editor-fold>
jButton1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jButton1.setText("ok");
jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
jLabel1.setText("Enter name:");
jTextField1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
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();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc="Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
*
For
details
see
http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
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(LoggingJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(LoggingJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(LoggingJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(LoggingJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} //<editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new LoggingJFrame().setVisible(true);
}
}
// Variables declaration - do not modify
public javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
public javax.swing.JTextField jTextField1;
// End of variables declaration
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package forpeter;
/**
*
* @author admin
*/
public class JFrame extends javax.swing.JFrame {
/**
* Creates new form JFrame
\*/
public JFrame() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
image = new javax.swing.JLabel();
name = new javax.swing.JLabel();
stop = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
name.setText("jLabel1");
stop.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N</editor-fold>
stop.setText("stop");
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();
}//
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
/<editor-fold defaultstate="collapsed" desc="Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
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);
} //<editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new JFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
public javax.swing.JLabel image;
public javax.swing.JLabel name;
public javax.swing.JButton stop;</editor-fold>
// End of variables declaration

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS