I need to convert a any image given to a byte array for encryption requirement. I'm using JProgressBar to monitor the conversion progress in case the chosen image is large:
File p= new File("C:\");
BufferedImage oImg = ImageIO.read(p);
ByteArrayOutputStream ba = new ByteArrayOutputStream();
ImageIO.write(oImg, "jpg", ba);
ba.flush();
ProgressBar pb = new ProgressBar();
Thread thread = new Thread(pb);
thread.join();
pb.fireTask(ba.toByteArray());
I defined a ProgressBar
class that uses SwingWorker
as follows:
public class ProgressBar extends JPanel implements Runnable {
private JProgressBar progressBar;
private Task task;
private byte[] imgByteArray;
public void run() {
createGUI();
}
// Create the GUI
private void createGUI() {
JFrame frame = new JFrame("Converting...");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComponent newContentPane = new ProgressBar();
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);
JPanel panel = new JPanel();
progressBar = new JProgressBar(0, 100);
progressBar.setBounds(20, 22, 419, 20);
progressBar.setValue(0);
progressBar.setStringPainted(true);
panel.add(progressBar);
add(panel);
setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
frame.pack();
frame.setVisible(true);
}
/**
* Firing the Task
*/
public void fireTask(byte[] imgArray) {
System.arraycopy(imgArray, 0, imgByteArray, 0, imgByteArray.length);
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
task = new Task();
task.execute();
}
class Task extends SwingWorker<Void, Void> {
@Override
public Void doInBackground() {
for (int i=0; i<=imgByteArray.length; i++){
progressBar.setValue(i);
progressBar.repaint();
try{
Thread.sleep(50);
} catch (InterruptedException err){
}
}
return null;
}
public void done() {
Toolkit.getDefaultToolkit().beep();
setCursor(null); // turn off the wait cursor
}
}
}
Sadly, this error occurs!
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at ciphers.ProgressBar.fireTask(ProgressBar.java:65)
at ciphers.Images.imageEncryption(Images.java:310)
at ciphers.Images.access$1(Images.java:286)
at ciphers.Images$2.actionPerformed(Images.java:184)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:4
02)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
I don't know what is wrong with my code! I read SwingWorker
and JProgressBar
before writing this code but I feel I'm missing something! Can I get a hint that helps.
Thank you :)
You seem to not understand what
SwingWorker
does.SwingWorker
provides a means by which you can execute long running tasks outside the context of the Event Dispatching Thread. This means that your program won't appear to have become frozen. The benefit of using aSwingWorker
(over using a simpleThread
) is that it provides a number of easy to use methods to re-sync the updates back to the EDT, which is indented to stop you from breaking the single thread rules of Swing: No UI element should be created or modified on any thread other then the EDT.Tak a closer look at Worker Threads and SwingWorker and
javax.swing.SwingWorker<T,V>
and Concurrency in Swing for more details