Ola Pessoal,
Creio que meu problema seja bem simples sou bem novo em JAva.
Estou fazendo um pequeno app para trocar um certo numero de imagens com jLabel. Porem so altera a primeira e a ultima. Ja tentei colocar sleep e nada. Alguem poderia me ajudar.
Valeu Pela ajuda.
Codigo Completo - Erro/Nao execucao em
loadimage(nameIcon);
package digitalpicutreframe;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.BorderFactory;
import java.util.logging.Level;
import java.util.logging.Logger;
public class PictureFrame extends javax.swing.JFrame {
public PictureFrame() {
initComponents();
}
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
try {
Photo =(javax.swing.JLabel)java.beans.Beans.instantiate(getClass().getClassLoader(), "digitalpicutreframe.PictureFrame_Photo");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (java.io.IOException e) {
e.printStackTrace();
}
jTextStart = new javax.swing.JTextField();
jTextEnd = new javax.swing.JTextField();
jtbPress = new javax.swing.JToggleButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowOpened(java.awt.event.WindowEvent evt) {
formWindowOpened(evt);
}
});
Photo.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
PhotoMouseClicked(evt);
}
});
jTextStart.setText("1");
jTextEnd.setText("10");
jTextEnd.setName(""); // NOI18N
jtbPress.setText("Press to Start");
jtbPress.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jtbPressActionPerformed(evt);
}
});
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(31, 31, 31)
.addComponent(jTextStart, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTextEnd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jtbPress))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(Photo, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(31, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(22, 22, 22)
.addComponent(Photo, javax.swing.GroupLayout.PREFERRED_SIZE, 208, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 36, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextStart, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jTextEnd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jtbPress))
.addContainerGap())
);
pack();
setLocationRelativeTo(null);
}// </editor-fold>
private void PhotoMouseClicked(java.awt.event.MouseEvent evt) {
}
private void formWindowOpened(java.awt.event.WindowEvent evt) {
Photo.setVerticalTextPosition(JLabel.BOTTOM);
Photo.setHorizontalTextPosition(JLabel.CENTER);
Photo.setHorizontalAlignment(JLabel.CENTER);
Photo.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
}
private void jtbPressActionPerformed(java.awt.event.ActionEvent evt) {
if (jtbPress.isSelected()){
jtbPress.setText("Press to Pause");
int i = 1;
int result;
String nameIcon;
for ( i = 0; i < 10; i++)
{
result = (i%10)+1;
if (result >= Integer.parseInt(jTextStart.getText())
&& (result <= Integer.parseInt(jTextEnd.getText())))
{
nameIcon = "src/Images/"+result+".jpg";
loadimage(nameIcon);
try {
new Thread().sleep(200);
} catch (InterruptedException ex) {
Logger.getLogger(PictureFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
jtbPress.setText("Press to Start");
jtbPress.setSelected(false);
} else
{
jtbPress.setText("Press to Start");
}
}
public void loadimage(String nameIcon) {
Photo.setVisible(false);
ImageIcon ImageIcon = new ImageIcon(nameIcon);
Photo.setIcon(ImageIcon);
Photo.setIcon(scaleImage(ImageIcon, 250, 300));
Photo.doLayout();
Photo.validate();
Photo.setVisible(true);
}
public ImageIcon scaleImage(ImageIcon icon, int w, int h)
{
int nw = icon.getIconWidth();
int nh = icon.getIconHeight();
if(icon.getIconWidth() > w)
{
nw = w;
nh = (nw * icon.getIconHeight()) / icon.getIconWidth();
}
if(nh > h)
{
nh = h;
nw = (icon.getIconWidth() * nh) / icon.getIconHeight();
}
return new ImageIcon(icon.getImage().getScaledInstance(nw, nh, Image.SCALE_DEFAULT));
}
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 | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(PictureFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(() -> {
new PictureFrame().setVisible(true);
});
}
// Variables declaration - do not modify
private javax.swing.JLabel Photo;
private javax.swing.JTextField jTextEnd;
private javax.swing.JTextField jTextStart;
private javax.swing.JToggleButton jtbPress;
// End of variables declaration
}