Reputation: 11
I'm writing a java image panel, to make some stuff with images in java. It's a first shot, to see how I can do things better.
So I wrote those three Classes and the problem is when I use the class Diaporama
(you can see the code below) in the class MainWindow
I have nothing shown.
But if i use the public static void main (String [] args)
in the class Diaporama, everything is working.
/**RechercheFichier.java*/
//this class while return an ArrayList of String which contain all file path selected
public class RechercheFichier {
private JFileChooser myFileChooser;
private ArrayList<String> FileList;
public RechercheFichier() {
myFileChooser = new JFileChooser();
myFileChooser.setCurrentDirectory(new File("."));
myFileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
try {
int value = myFileChooser.showOpenDialog(null);
if (value == JFileChooser.APPROVE_OPTION) {
File SelectedFile = myFileChooser.getSelectedFile();
String Filename = SelectedFile.getPath();
FileList = new ArrayList<String>();
if (SelectedFile.isDirectory()) {
String[] myFile = SelectedFile.list();
for (int i = 0; i < myFile.length; i++) {
if (myFile[i].endsWith(".png") == true
|| myFile[i].endsWith(".jpg") == true
|| myFile[i].endsWith(".jpeg") == true) {
FileList.add(Filename + "/" + myFile[i]);
}
}
} else
FileList.add(Filename);
} else
JOptionPane.showMessageDialog(null,
"User did not choose a file.");
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e.getMessage(), "Error",
JOptionPane.ERROR_MESSAGE);
}
}
//getter and setter
public ArrayList<String> getFileList() {
return FileList;
}
public void setFileList(ArrayList<String> fileList) {
FileList = fileList;
}
Diaporama class:
/**Diaporama.java*/
public class Diaporama extends JFrame {
private JFrame frame;
private JPanel panel;
private JLabel lab;
public Diaporama() {
frame = new JFrame();
panel = new JPanel();
lab = null;
frame.setTitle("Diaporama");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
RechercheFichier file = new RechercheFichier();
ArrayList<String> toto = file.getFileList(); //toto contain all file path
//Collections.sort(toto);
for (int i = 0; i < toto.size(); i++) {
panel = new JPanel();
frame.setLayout(new GridLayout(1,1));
frame.setVisible(true);
frame.setSize(600, 500);
frame.setLocation(200, 200);
// this.image = getToolkit().getImage(toto.get(i));
System.out.println(toto.get(i));
this.lab = new JLabel(new ImageIcon(toto.get(i)));
panel.add(lab);
frame.setContentPane(panel);
try {
Thread.sleep(2000L);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// imagePanel.remove(imagePanel);
panel.remove(lab);
panel.revalidate();
frame.pack();
frame.repaint();
panel = null;
}
frame.dispose();
}
//public static void main(String [] args)
//{
// new Diaporama();
//}
MainWindow class:
/** MainWindow.java**/
public class MainWindow extends JFrame implements ActionListener{
private JFrame frame;
private JButton buttonDiapo, buttonOpen, buttonSearch, buttonTag, buttonAlbum, buttonQuit;
private JPanel panel;
public JFrame getFrame()
{
return frame;
}
public MainWindow()
{
frame = new JFrame("The best Jphoto");
frame.setSize(300, 400);
frame.setLocation(200, 200);
buttonDiapo = new JButton("Diaporama");
buttonOpen = new JButton("Ouvrir Photo");
buttonQuit = new JButton ("Quitter");
buttonSearch = new JButton("Recherche par tag");
buttonTag = new JButton("Add tag");
buttonAlbum = new JButton("Creer un album");
panel = new JPanel();
panel.setLayout(new FlowLayout());
panel.add(buttonOpen);
panel.add(buttonDiapo);
panel.add(buttonAlbum);
panel.add(buttonSearch);
panel.add(buttonTag);
panel.add(buttonQuit);
frame.getContentPane().add(panel, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
//ajout du action Listener pour le bouton Open
buttonOpen.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// new OpenImg();
}
});
//ajout du action Listener pour le bouton Diapo
buttonDiapo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//the problem is here
new Diaporama();
}
});
buttonQuit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
buttonAlbum.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// new Album();
}
});
buttonSearch.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// new Search();
}
});
buttonTag.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// new Tag();
}
});
}
public static void main(String[] args) {
// Set the look and feel to Java Swing Look
new MainWindow();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}
Upvotes: 1
Views: 869
Reputation: 11
I finally change the Diaporama class, i implemented it as an
instance of a thread. Curiously,
implementing Diaporama as Runnable and lunch it as a thread, makes it work. however, i still don't understand why the first version doesn't work. Some of my teacher told me about the java graphic thread, will not refresh the frame, or something like this.
thank you for your help
i paste here my new code.
import java.util.ArrayList;
@SuppressWarnings("serial")
public class DiapoThread implements Runnable {
Thread thread;
ArrayList<String> toto = null;
public void init() {
RechercheFichier file = new RechercheFichier();
file.RechercheFichierFileOrDirectory();
toto = file.getFileList();
}
public void start() {
(thread = new Thread(this)).start();
}
public void stop() {
thread = null;
}
public void run() {
this.init();
new DiaporamaView(toto);
this.stop();
}
}
the DiaporamaView.java
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class DiaporamaView extends JFrame{
private int iframe = 0;
private JFrame frame = null;
private JPanel panel = null;
private JLabel lab = null;
private ArrayList<String> toto = null;
public DiaporamaView(ArrayList<String> list)
{
ArrayList<String> toto = list;
long delay = 2500L;
frame = new JFrame();
panel = new JPanel();
JPanel panBoutton = new JPanel();
frame.setTitle("Diaporama");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(panel, BorderLayout.CENTER);
panBoutton.setLayout(new GridLayout(1,1));
JButton fermer = new JButton("fermer fenetre");
fermer.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.dispose();
}
});
panBoutton.add(fermer);
this.frame.setVisible(true);
this.panel.setBackground(Color.GRAY);
frame.setLocation(100, 200);
try {
while (iframe < toto.size()) {
if (iframe == 0)
frame.setSize(400, 450);
else if (iframe % 2 == 0)
frame.setSize(401, 451);
else
frame.setSize(399, 449);
Image img = ImageIO.read(new File(toto.get(iframe)));
// redimensionner les images,
int width = img.getWidth(null);
int height = img.getHeight(null);
int new_height = 448;
int new_width = (width / height) * new_height;
ImageIcon ii = new ImageIcon(img.getScaledInstance(new_width, new_height, img.SCALE_FAST));
this.lab = new JLabel(ii);
panel.add(lab);
frame.add(panBoutton, BorderLayout.SOUTH);
//repaint();
Thread.sleep(delay);
panel.remove(lab);
panel.revalidate();
iframe++;
// frame.pack();
frame.repaint();
}
} catch (Exception e) {
e.printStackTrace();
}
}
Upvotes: 0
Reputation: 8960
It's a bit inefficient to pop up JFrame
s every time you click a button. Have you considered using JDialogs?
But, if you reaaaaaalllly want a solution to you problem, you can try:
Diaporama diaporamaFrame = new Diaporama();
diaporamaFrame.setVisible(true);
Stick this into your listener. Good luck.
Upvotes: 1
Reputation: 4715
public static void main (String [] args)
Provides the entry point to program. Without it nothing will be able to run.
In the Java language, when you execute a class with the Java interpreter, the runtime system starts by calling the class's main() method. The main() method then calls all the other methods required to run your application.
For more information follow this link http://wiki.answers.com/Q/What_is_the_use_of_main_method_in_java
Upvotes: 1