Pagina 1 di 4 1 2 3 ... ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 37
  1. #1

    Visualizzazione immagini in sequenza

    Ciao a tutti.Non sono piuttosto bravo in java e chiedo a voi esperti se potete indirizzarmi verso la giusta soluzione.
    Ho realizzato una classe che estende jframe in java.Si tratta di un frame per visualizzare due semplicissime immagini(inserite in jlabel).
    dovrei visualizzare 2 immagini per volta presenti in una directory da me creata.vorrei capire come faccio a visualizzarne due per x secondi e poi passare alle successive due.(devo usare thread?timer?)Non chiedo necessariamente il codice java ma anche uno pseudocodice che mi aiuti a capire cosa devo fare e perchè...
    grazie ragazzi

    by Disperato

  2. #2

    Moderazione

    Per Java c'è una sezione apposita, ti sposto lì.
    Amaro C++, il gusto pieno dell'undefined behavior.

  3. #3
    Lo zio G... aiuta sempre in questo Link c'è giusto un esempio di come creare uno slideshow che dovrebbe risolvere i tuoi dubbi....

  4. #4
    Utente di HTML.it
    Registrato dal
    Sep 2012
    Messaggi
    707
    Riguardo ai timer, Java ha diverse classi con funzionalità varie, prova a guardare:
    - java.util.timer (http://docs.oracle.com/javase/7/docs...til/Timer.html)
    - ScheduledExecutorService (http://docs.oracle.com/javase/7/docs...orService.html)
    - javax.swing.Timer (http://docs.oracle.com/javase/7/docs...ing/Timer.html)
    L'ultimo è un tipo di timer specifico per lavorare insieme al Swing (non che gli altri non vadano bene). Ne si parla anche qui: http://docs.oracle.com/javase/tutori...isc/timer.html
    Ciao.

  5. #5
    Avevo una mezza idea,cioè la seguente. Ho 4 immagini in una cartella all'interno del package cioè 1.jpg 2.jpg 3.jpg 4.jpg. devo visualizzarne 2 per volta per 2 secondi,poi schermata nera e poi ancora 2.La mia idea era questa ma sicuramente c'è qualche errore concettuale...
    codice:
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.Toolkit;
    
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    import java.awt.Color;
    import java.awt.Cursor;
    import javax.swing.JLabel;
    
    
    public class MyFrame extends JFrame {
    
    	private static JPanel contentPane;
    
    	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					MyFrame frame = new MyFrame();
    					frame.setVisible(true);
    					
    					JLabel lblNewLabel = new JLabel();
    					
    					lblNewLabel.setBackground(Color.BLACK);
    					lblNewLabel.setBounds(48, 296, 132, 170);
    					
    					
    					JLabel lblNewLabel_1 = new JLabel();
    					
    					lblNewLabel_1.setBounds(1138, 296, 132, 170);
    					
    					
    					for (int i = 1; i <= 2; i++) {
    						ImageIcon img = new ImageIcon(i + ".jpeg");
    						lblNewLabel.setIcon(img);
    						contentPane.add(lblNewLabel);
    						lblNewLabel_1.setIcon(new ImageIcon(i++ + ".jpg"));
    						contentPane.add(lblNewLabel_1);
    						try {
    							Thread.sleep(2000);
    						} catch (InterruptedException e) {
    							// TODO Auto-generated catch block
    							e.printStackTrace();
    						}
    					}
    		
    						
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
    
    	/**
    	 * Create the frame.
    	 */
    	public MyFrame() {
    		setUndecorated(true);
    		setForeground(Color.BLACK);
    		setExtendedState(this.MAXIMIZED_BOTH);
    		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    		this.setSize(dim.width, dim.height);
    		setDefaultCloseOperation(this.EXIT_ON_CLOSE);
    		getContentPane().setLayout(null);
    		contentPane = new JPanel();
    		contentPane.setBackground(Color.BLACK);
    		contentPane.setForeground(Color.BLACK);
    		setContentPane(contentPane);
    		contentPane.setLayout(null);
    		
    
    		
    		
    			
    	}
    }

  6. #6
    Utente di HTML.it
    Registrato dal
    Sep 2012
    Messaggi
    707
    Il codice va postato nei tag "code", altrimenti risulta difficile leggerlo.
    A parte questo vedo lo stesso che usi i thread e non i timer, di qui il fatto che non funziona o funziona male.
    Quandi dissi "Riguardo ai timer" forse non sono stato chiaro, intendevo che devi usare i timer.

  7. #7
    coder ti ringrazio.La scelta era ricaduta sui thread perchè mi sembravano piu semplici da gestire..ora sto cercando come un disperato roba sui timer e non riesco a capire il loro uso in particolare avevo intenzione di usare java.swing.timer ma incontro delle difficoltà..Temo di avere dei problemi circa dove inserire il codice (nel costruttore del frame?nella parte run?o dentro il main?) insieme anche al problema di dovere visualizzare due immagini per volta per 2 secondi,visualizzare una scermata nera e successivamente altre due immagini. Il codice che ti posto ora è di prova con 4 immagini di una cartella del mio pc.


    codice:
    public class MyFrame2 extends JFrame { /** * */ private static JPanel contentPane; /** * Launch the application. */ public static void main(String [] args){ EventQueue.invokeLater(new Runnable() { public void run() { try { MyFrame2 frame = new MyFrame2(); frame.setVisible(true); //creo due jlabel posizionate sul mio schermo JLabel lblNewLabel = new JLabel(); lblNewLabel.setForeground(Color.WHITE); lblNewLabel.setBackground(Color.BLACK); lblNewLabel.setBounds(48, 296, 200, 170); JLabel lblNewLabel_1 = new JLabel(); lblNewLabel_1.setForeground(Color.WHITE); lblNewLabel_1.setBounds(1038, 296, 200, 170); String s = "C://Documents and Settings//utente//workspace//Progect//src//album//"; ImageIcon img = new ImageIcon(s + "1.jpg"); lblNewLabel.setIcon(img); contentPane.add(lblNewLabel); lblNewLabel_1.setIcon(new ImageIcon(s + "2.jpg")); contentPane.add(lblNewLabel_1); //vorrei inserire qualcosa per far visualizarre le immagini per 2 secondi, //cancellarle(per avere schermo nero) e visualizzare le 2 successive cioè.. img = new ImageIcon(s + "3.jpg"); lblNewLabel.setIcon(img); contentPane.add(lblNewLabel); lblNewLabel_1.setIcon(new ImageIcon(s + "4.jpg")); contentPane.add(lblNewLabel_1); } catch (Exception e) { e.printStackTrace(); } }} );} /** * Create the frame. */ public MyFrame2() { setUndecorated(true); setForeground(Color.BLACK); setExtendedState(Frame.MAXIMIZED_BOTH); Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); this.setSize(dim.width, dim.height); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); getContentPane().setLayout(null); contentPane = new JPanel(); contentPane.setBackground(Color.BLACK); contentPane.setForeground(Color.BLACK); setContentPane(contentPane); contentPane.setLayout(null); } }

  8. #8
    Utente di HTML.it
    Registrato dal
    Sep 2012
    Messaggi
    707
    Fai almeno un tentativo... guardati questo: http://docs.oracle.com/javase/tutori...isc/timer.html

    In un punto del tuo codice crei il timer come spiegato lì, e definisci una funzione actionPerformed, che ti verrà chiamata ogni tot secondi. In quella funzione visualizzi l'immagine successiva (iterando su un array, una lista, quello che vuoi).

    Se non ci riesci ti farò un esempio, ma almeno provaci.

  9. #9
    Mha... ti avevo anche postato un link un cui c'è proprio tutta l'implementazione (bastava solo modificare la logica 2 foto più tosto che uno) cmq dato che siamo quasi a natale eccoti qui
    codice:
    package it.temp;
    
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.Icon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    import javax.swing.Timer;
    import javax.swing.UIManager;
    
    public class SlideShow extends JFrame {
    	
    	private static final long serialVersionUID = 1L;
    	private Timer timer;
    	private JLabel image1 = new JLabel();
    	private JLabel image2 = new JLabel();
    	private JPanel container = new JPanel();
    	JPanel blackPanel = new JPanel();
    	private Icon[] icons = { UIManager.getIcon("OptionPane.informationIcon"),
    			UIManager.getIcon("OptionPane.errorIcon"),
    			UIManager.getIcon("OptionPane.warningIcon"),
    			UIManager.getIcon("OptionPane.informationIcon") };
    
    	private int i = 0;
    
    	public SlideShow() {
    		super("SlideShow");
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		this.setLocationByPlatform(true);
    		blackPanel.setBackground(Color.BLACK);
    		blackPanel.setSize(100, 100);
    		image1.setIcon(icons[0]);
    		image2.setIcon(icons[1]);
    		container.add(image1);
    		container.add(image2);
    		this.setLayout(new BorderLayout());
    		this.add(container);
    		this.pack();
    		this.setVisible(true);
    		timer = new Timer(2000, action);
    		timer.start();
    	}
    
    	private ActionListener action = new ActionListener() {
    		public void actionPerformed(ActionEvent ae) {
    			i++;
    			System.out.println(i);
    
    			if (i == 1) {
    				container.remove(image1);
    				container.remove(image2);
    				container.add(blackPanel);
    			}
    			if (i == 2) {
    				container.remove(blackPanel);
    				image1.setIcon(icons[2]);
    				image2.setIcon(icons[3]);
    				container.add(image1);
    				container.add(image2);
    				timer.stop();
    			}
    
    			repaint();
    		}
    	};
    
    	public static void main(String... args) {
    		SwingUtilities.invokeLater(new Runnable() {
    			public void run() {
    				new SlideShow();
    			}
    		});
    	}
    }

  10. #10
    Utente di HTML.it
    Registrato dal
    Sep 2012
    Messaggi
    707
    Trovo che potesse essere fatta in modo più semplice anche senza seguire quell'esempio... la mia versione:
    codice:
    import java.awt.FlowLayout;
    import java.awt.Color;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JFrame;
    import javax.swing.Icon;
    import javax.swing.Timer;
    import javax.swing.UIManager;
    import javax.swing.JLabel;
    
    class Demo extends JFrame implements ActionListener {
        private Timer timer;
        private JLabel label, label2;
        private int iconIndex = 0;
        private Icon icons[][] = {
            { UIManager.getIcon("OptionPane.informationIcon"), UIManager.getIcon("OptionPane.errorIcon") },
            { UIManager.getIcon("OptionPane.warningIcon"), UIManager.getIcon("OptionPane.informationIcon") }
            // qui puoi aggiungere altre coppie di icone
       };
    
       public Demo() {
            label = new JLabel(icons[iconIndex][0]);
            label2 = new JLabel(icons[iconIndex][1]);
            add(label);
            add(label2);
            getContentPane().setBackground(Color.BLACK);
            setLayout(new FlowLayout());
            setSize(600, 400);
            // creo timer
            timer = new Timer(2000, this);
            timer.restart();
        }
        
        public void actionPerformed(ActionEvent e) {
            iconIndex = iconIndex + 1 >= icons.length ? 0 : iconIndex + 1;
            label.setIcon(icons[iconIndex][0]);
            label2.setIcon(icons[iconIndex][1]);
        }    
    
        public static void main(String args[]) {
            new Demo().setVisible(true);
        }
    }

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.