Problemi con SwingWorker e JProgressBar

Ho bisogno di convertire una qualsiasi immagine data a un array di byte per i requisiti di crittografia. Sto usando JProgressBar per monitorare l’avanzamento della conversione nel caso in cui l’immagine scelta sia grande:

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()); 

Ho definito una class ProgressBar che utilizza SwingWorker come segue:

 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 { @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 } } } 

Purtroppo, questo errore si verifica!

 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.actionPerformsd(Images.java:184) at javax.swing.AbstractButton.fireActionPerformsd(AbstractButton.java:2018) at javax.swing.AbstractButton$Handler.actionPerformsd(AbstractButton.java:2341) at javax.swing.DefaultButtonModel.fireActionPerformsd(DefaultButtonModel.java:4 02) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252) 

Non so cosa c’è di sbagliato nel mio codice! Leggo SwingWorker e JProgressBar prima di scrivere questo codice, ma sento che mi manca qualcosa! Posso avere un suggerimento che aiuti.

Grazie 🙂

    Sembra che tu non capisca cosa fa SwingWorker .

    SwingWorker fornisce un mezzo con il quale è ansible eseguire attività di lunga durata al di fuori del contesto del Thread di dispacciamento dell’evento. Ciò significa che il tuo programma non sembrerà congelato. Il vantaggio dell’utilizzo di SwingWorker (rispetto all’utilizzo di una semplice Thread ) è che fornisce un certo numero di metodi facili da usare per risincronizzare gli aggiornamenti sull’EDT, il quale è indentato per impedirti di infrangere le regole del thread singolo di Swing: Nessun elemento dell’interfaccia utente deve essere creato o modificato su alcun thread diverso dall’EDT.

    Dai un’occhiata più da vicino a Worker Threads e SwingWorker e javax.swing.SwingWorker e Concurrency in Swing per maggiori dettagli

    Convertire

     import java.awt.Dimension; import java.awt.EventQueue; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.image.BufferedImage; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import java.io.ByteArrayOutputStream; import java.io.File; import java.util.Iterator; import java.util.List; import java.util.concurrent.ExecutionException; import javax.imageio.ImageIO; import javax.imageio.ImageReader; import javax.imageio.ImageWriter; import javax.imageio.event.IIOReadProgressListener; import javax.imageio.event.IIOWriteProgressListener; import javax.imageio.stream.ImageInputStream; import javax.imageio.stream.ImageOutputStream; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JProgressBar; import javax.swing.SwingUtilities; import javax.swing.SwingWorker; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; public class ConvertImage { public static void main(String[] args) { new ConvertImage(); } public ConvertImage() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { ex.printStackTrace(); } TestPane tp = new TestPane(); JFrame frame = new JFrame("Testing"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(tp); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); tp.convert(new File("C:\\Users\\Shane Whitehead\\Dropbox\\Wallpapers\\animepaper.net_wallpaper_art_anime_aria_duanwu_festival_205050_wonderngo_7680x4800-a8aecc9c.jpg")); } }); } public class TestPane extends JPanel implements ImageConverstionListener { private JLabel label = new JLabel("Waiting..."); private JProgressBar pb = new JProgressBar(); public TestPane() { setLayout(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.gridwidth = GridBagConstraints.REMAINDER; add(label, gbc); add(pb, gbc); } public void convert(File file) { ConverterWorker worker = new ConverterWorker(file, this); worker.addPropertyChangeListener(new PropertyChangeListener() { @Override public void propertyChange(final PropertyChangeEvent evt) { if ("progress".equalsIgnoreCase(evt.getPropertyName())) { pb.setValue((int) evt.getNewValue()); } } }); worker.execute(); } @Override public Dimension getPreferredSize() { return new Dimension(200, 200); } @Override public void failedToConvertImage(File source, Throwable cause) { cause.printStackTrace(); JOptionPane.showMessageDialog(this, "Failed to convert " + source + "
    " + cause.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); } @Override public void imageConverted(File source, byte[] bytes) { JOptionPane.showMessageDialog(this, "Converted image to " + bytes.length + " bytes", "Converted", JOptionPane.INFORMATION_MESSAGE); } @Override public void setMessage(String msg) { label.setText(msg); } } public interface ImageConverstionListener { public void failedToConvertImage(File source, Throwable cause); public void imageConverted(File source, byte[] bytes); public void setMessage(String msg); } public class ConverterWorker extends SwingWorker { private File source; private ImageConverstionListener listener; public ConverterWorker(File source, ImageConverstionListener listener) { this.source = source; this.listener = listener; } @Override protected void process(List chunks) { listener.setMessage(chunks.get(chunks.size() - 1)); } @Override protected ByteArrayOutputStream doInBackground() throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); publish("Reading image..."); try (ImageInputStream iis = ImageIO.createImageInputStream(source)) { Iterator readers = ImageIO.getImageReaders(iis); if (readers.hasNext()) { ImageReader reader = readers.next(); reader.addIIOReadProgressListener(new IIOReadProgressListener() { @Override public void sequenceStarted(ImageReader source, int minIndex) { } @Override public void sequenceComplete(ImageReader source) { } @Override public void imageStarted(ImageReader source, int imageIndex) { } @Override public void imageProgress(ImageReader source, float percentageDone) { setProgress(Math.round(percentageDone)); } @Override public void imageComplete(ImageReader source) { } @Override public void thumbnailStarted(ImageReader source, int imageIndex, int thumbnailIndex) { } @Override public void thumbnailProgress(ImageReader source, float percentageDone) { } @Override public void thumbnailComplete(ImageReader source) { } @Override public void readAborted(ImageReader source) { } }); reader.setInput(iis); try { BufferedImage img = reader.read(0); publish("Converting image..."); try (ImageOutputStream ios = ImageIO.createImageOutputStream(baos)) { Iterator writers = ImageIO.getImageWritersByFormatName("png"); if (writers.hasNext()) { ImageWriter writer = writers.next(); writer.addIIOWriteProgressListener(new IIOWriteProgressListener() { @Override public void imageStarted(ImageWriter source, int imageIndex) { } @Override public void imageProgress(ImageWriter source, float percentageDone) { setProgress(Math.round(percentageDone)); } @Override public void imageComplete(ImageWriter source) { } @Override public void thumbnailStarted(ImageWriter source, int imageIndex, int thumbnailIndex) { } @Override public void thumbnailProgress(ImageWriter source, float percentageDone) { } @Override public void thumbnailComplete(ImageWriter source) { } @Override public void writeAborted(ImageWriter source) { } }); writer.setOutput(ios); try { writer.write(img); } finally { writer.removeAllIIOWriteProgressListeners(); } } } } finally { reader.removeAllIIOReadProgressListeners(); } } } return baos; } @Override protected void done() { try { ByteArrayOutputStream baos = get(); listener.imageConverted(source, baos.toByteArray()); } catch (InterruptedException | ExecutionException ex) { listener.failedToConvertImage(source, ex); } } } }