Note that there are some explanatory texts on larger screens.

plurals
  1. PORefreshing JPanel after loading next Image into it
    primarykey
    data
    text
    <p>I have created <code>ImagePanel</code> which is able to display images from the specified directory -> it sleeps 1 second and loads next image from the java project's directory. It actually loads next image but it is not displayed(it does not refresh the panel), when it is done with all the files from the directory it shows only the last image from the directory. I would like to make it refresh after loading every image.</p> <pre><code> import java.awt.Color; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.SwingUtilities; import javax.swing.border.LineBorder; public class Okno extends JFrame { JPanel jp; ImagePanel ImagePanel; JButton buttonExit; JButton buttonWyjscie; public Okno() { } public void createGUI() { setSize(400, 400); setLayout(new GridLayout()); buttonExit = new JButton("Exit"); buttonWyjscie = new JButton("Wyjscie"); // Sluchacz sluchacz = new Sluchacz(); // buttonExit.addActionListener(sluchacz); buttonExit.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.exit(0); } }); jp = new JPanel(); jp.setBorder(new LineBorder(new Color(40, 120, 80), 4)); ImagePanel = new ImagePanel(); ImagePanel.setBorder(new LineBorder(Color.blue, 4)); jp.add(buttonExit); add(jp); add(ImagePanel); setVisible(true); slajd(); setDefaultCloseOperation(EXIT_ON_CLOSE); } public void slajd() { try { File f = new File("."); File[] tablicaPlikow = f.listFiles(); for (File el : tablicaPlikow) { String rozszerzenie = el.getName().substring( el.getName().length() - 3); if (rozszerzenie.equals("jpg") || rozszerzenie.equals("peg")) { System.out.println(rozszerzenie); ImagePanel.setImage(el); } repaint(); } setVisible(true); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new Okno().createGUI(); } }); } } </code></pre> <hr> <pre><code>import java.awt.Graphics; import java.awt.Image; import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; import javax.swing.JPanel; public class ImagePanel extends JPanel { private BufferedImage image; public ImagePanel() { } public ImagePanel(String sciezka) { setImage(new File(sciezka)); } public void setImage(File plik) { try { image = ImageIO.read(plik); System.out.println("tutaj"); repaint(); Thread.sleep(1000); } catch (Exception e) { e.printStackTrace(); } } public void paint(Graphics g) { if (image != null) { Image b = image.getScaledInstance(getWidth(), getHeight(), Image.SCALE_FAST); g.drawImage(image, 0, 0, getWidth(), getHeight(), null); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload