Results 1 to 2 of 2
- 06-01-2011, 08:40 PM #1
Member
- Join Date
- Jun 2011
- Posts
- 19
- Rep Power
- 0
Thread Slows Down After First Run
Hello, I'm working on a little experimental program that displays an image sequence as if it were
a video. Everything works great except that after the first play of the video it begins to slow down
and appears to be choppy. At first run it displays the images at 29.97 fps with no problem, but after
that it drops down to maybe < 20 fps. This is my first time working with threads so any help
would be greatly appreciated. Thanks!
Java Code:import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; public class ImageSequenceViewer extends JFrame implements ActionListener, Runnable { private JButton startButton; private JButton stopButton; private JPanel canvas; private Thread runner; private volatile boolean isPlaying; private BufferedImage[] video; private volatile int curFrame; private int pause; private double fps = 29.97; private double frameDuration; public static void main(String[] args) { ImageSequenceViewer viewer = new ImageSequenceViewer(); viewer.loadImageSequence(); } public ImageSequenceViewer() { super("ImageSequenceViewer"); setDefaultCloseOperation(EXIT_ON_CLOSE); Container pane = getContentPane(); isPlaying = false; curFrame = 0; frameDuration = 1.0 / fps; pause = (int) (frameDuration * 1000.0); canvas = new JPanel(); pane.add(canvas, BorderLayout.CENTER); startButton = new JButton("Start"); stopButton = new JButton("Stop"); JPanel panel = new JPanel(); panel.add(startButton); startButton.addActionListener(this); panel.add(stopButton); stopButton.addActionListener(this); pane.add(panel, BorderLayout.SOUTH); setBounds(0, 0, 500, 500); setVisible(true); } public void loadImageSequence() { JFileChooser fc = new JFileChooser(); fc.setMultiSelectionEnabled(true); fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); fc.showOpenDialog(null); File directory = fc.getSelectedFile(); if (directory.isDirectory()) { File[] children = directory.listFiles(); video = new BufferedImage[children.length]; File file; for (int i = 0; i < children.length; i++) { file = children[i]; BufferedImage img = null; try { img = ImageIO.read(file); } catch (Exception e) { img = new BufferedImage(500, 500, BufferedImage.TYPE_INT_ARGB); } video[i] = img; } } } public void actionPerformed(ActionEvent ae) { Object source = ae.getSource(); if (source == startButton) { if (!isPlaying) { runner = new Thread(this); runner.start(); isPlaying = true; } } else if (source == stopButton) { if (runner != null && isPlaying) { runner.interrupt(); isPlaying = false; } } } public void run() { Graphics g = canvas.getGraphics(); while (isPlaying) { g.drawImage(video[curFrame], 0, 0, null); curFrame++; curFrame %= video.length; pause(pause); } g.dispose(); } public void pause(int milliseconds) { try { Thread.sleep(milliseconds); } catch (InterruptedException e) { System.out.println("Paused"); } } }
- 06-01-2011, 09:20 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,619
- Rep Power
- 5
Do not paint by calling getGraphic - Override the paintComponent method (or use an ImageIcon). Further, Swing is single threaded, so I'd recommend using a SwingTimer rather than a Thread
Painting in AWT and Swing
Similar Threads
-
Difference between Thread.yield() and Thread.sleep() methods
By Nageswara Rao Mothukuri in forum New To JavaReplies: 12Last Post: 07-30-2010, 05:37 PM -
how to reduce the thread sleep time and wake up the thread
By baktha.thalapathy in forum Threads and SynchronizationReplies: 2Last Post: 06-24-2010, 07:36 PM -
Trigger main thread method from secondary thread?
By DigitalMan in forum Threads and SynchronizationReplies: 8Last Post: 01-26-2010, 02:13 AM -
If JNI thread call the java object in another thread, it will crash.
By skaterxu in forum Advanced JavaReplies: 0Last Post: 01-28-2008, 07:02 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks