Results 1 to 1 of 1
- 05-11-2014, 09:30 PM #1
Member
- Join Date
- May 2014
- Posts
- 1
- Rep Power
- 0
Load and display all files with .jpg, .png and .gif from directory path
Hello, i'm doing this task for my university java class and I've got a big problem with displaying pictures. I'm sucesfully load and recognize the extensions, but when they load I've got only frames with bugged diagonals (first is ok and adapts to the size of the frame, but next ones are broken and I don't know how to fix it) please help.
This is Main class
Java Code:package cw6_zad2; //C:\Users\MEN8\Desktop\ZDJECIA\test import java.awt.image.BufferedImage; import java.io.File; import java.io.FilenameFilter; import java.io.IOException; import java.util.concurrent.TimeUnit; import javax.imageio.ImageIO; import javax.swing.JFrame; public class Main extends JFrame { private static final long serialVersionUID = -1906466112293567427L; private static String path; private int time; private static int fSize; // array of supported extensions (use a List if you prefer) final static String[] EXTENSIONS = new String[]{ "gif", "png", "jpg" // and other formats you need }; // filter to identify images based on their extensions final static FilenameFilter IMAGE_FILTER = new FilenameFilter() { @Override public boolean accept(final File dir, final String name) { for (final String ext : EXTENSIONS) { if (name.endsWith("." + ext)) { return (true); } } return (false); } }; public void readParams() { String arg = javax.swing.JOptionPane.showInputDialog("Prosze podac adres folderu oraz czas wyswietlania"); String[] tab =arg.split(" "); path = tab[0]; time = Integer.parseInt(tab[1]); fSize = Integer.parseInt(tab[2]); final File dir = new File(path); if (dir.isDirectory()) { for (final File f : dir.listFiles(IMAGE_FILTER)) { try { repaint(); BufferedImage img = ImageIO.read(f); ImagePanel p = new ImagePanel(f.getName()); add(p); pack(); setTitle(f.getName()); setLocationRelativeTo(null); setVisible(true); try { TimeUnit.SECONDS.sleep(this.time); if(f.getName() != "") { dispose(); repaint(); }else { repaint(); setTitle("Przekatne"); add(new ImageField(img.getWidth(), img.getHeight())); setDefaultCloseOperation(EXIT_ON_CLOSE); pack(); setLocationRelativeTo(null); setVisible(true); } } catch (Exception e) {} System.out.println("image: " + f.getName()); System.out.println(" width : " + img.getWidth()); System.out.println(" height: " + img.getHeight()); System.out.println(" size : " + f.length()); }catch (final IOException e) { // handle errors here } } } } public static void main(String[] args) { Main m = new Main(); m.readParams(); } }
Java Code:package cw6_zad2; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Image; import java.awt.MediaTracker; import java.awt.Toolkit; import javax.swing.JPanel; public class ImagePanel extends JPanel { private static final long serialVersionUID = -8023867670007487938L; Image img; boolean loaded = false; int w, h; MediaTracker mt; public ImagePanel(String imgFileName) { loadImage(imgFileName); } public void paintComponent(Graphics g) { super.paintComponent(g); if (this.img != null && this.loaded) g.drawImage(this.img, 0, 0, getWidth(), getHeight(), this); else { super.paintComponent(g); this.w = getWidth(); this.h = getHeight(); g.setColor(Color.blue); g.drawLine(0, 0, this.w, this.h); g.drawLine(0, this.h, this.w, 0); } } private void loadImage(String imgFileName) { this.img = Toolkit.getDefaultToolkit().getImage(imgFileName); this.mt = new MediaTracker(this); this.mt.addImage(this.img, 1); try { this.mt.waitForID(1); } catch (InterruptedException exc) { } this.w = this.img.getWidth(this); this.h = this.img.getHeight(this); if (this.w != -1 && this.w != 0 && this.h != -1 && this.h != 0) { this.loaded = true; setPreferredSize(new Dimension(this.w, this.h)); } else setPreferredSize(new Dimension(200, 200)); } }
Java Code:package cw6_zad2; import java.awt.Dimension; import javax.swing.JComponent; public class ImageField extends JComponent { private static final long serialVersionUID = -6546532428097591931L; Dimension d; public ImageField(int w, int h) { this.d = new Dimension(w, h); setMinimumSize(d); setPreferredSize(new Dimension(d)); setMaximumSize(d); } }
Similar Threads
-
Display the list of files from a directory and obtain its output in index html format
By unsaqa@gmail.com in forum New To JavaReplies: 7Last Post: 07-29-2013, 03:27 PM -
[SOLVED] Load applet from REMOTE directory
By marksu988 in forum Java AppletsReplies: 11Last Post: 08-02-2012, 03:46 PM -
how can i get directory path without select any file ?
By amr magdy in forum New To JavaReplies: 1Last Post: 12-29-2011, 12:52 PM -
I need to set new directory path in tomcat
By Mayank Joshi in forum Java ServletReplies: 2Last Post: 05-05-2010, 01:16 PM -
load all files in a directory
By moomoo in forum New To JavaReplies: 1Last Post: 04-21-2008, 11:18 AM
Bookmarks