java multithreading swing jframe jlabel

java - ¿Cómo actualizar correctamente la imagen en JFrame?



multithreading swing (2)

Esto es lo que se me ocurrió:

private static class MyJPanel extends JPanel { private Image img = null; public MyJPanel() {} public void setImage(Image value) { if (img != value) { Image old = img; this.img = value; firePropertyChange("image", old, img); revalidate(); repaint(); } } public Image getImage() { return img; } @Override public Dimension getPreferredSize() { return img == null ? new Dimension(200, 200) : new Dimension(img.getWidth(this), img.getHeight(this)); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); if (img != null) { Graphics2D g2d = (Graphics2D) g.create(); int width = getWidth(); int height = getHeight(); double scaleFactor = getScaleFactorToFit(new Dimension(img.getWidth(this), img.getHeight(this)), getSize()); int x = (int) ((width - (img.getWidth(this) * scaleFactor)) / 2); int y = (int) ((height - (img.getHeight(this) * scaleFactor)) / 2); AffineTransform at = new AffineTransform(); at.translate(x, y); at.scale(scaleFactor, scaleFactor); g2d.setTransform(at); g2d.drawImage(img, 0, 0, this); g2d.dispose(); } } public double getScaleFactor(int iMasterSize, int iTargetSize) { return (double) iTargetSize / (double) iMasterSize; } public double getScaleFactorToFit(Dimension original, Dimension toFit) { double dScale = 1d; if (original != null && toFit != null) { double dScaleWidth = getScaleFactor(original.width, toFit.width); double dScaleHeight = getScaleFactor(original.height, toFit.height); dScale = Math.min(dScaleHeight, dScaleWidth); } return dScale; } } private static class MyJFrame extends JFrame implements Runnable { private BufferedImage img = null; private MyJPanel panel = null; public MyJFrame(BufferedImage image, String title) { super(title); img = image; } @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {} panel = new MyJPanel(); panel.setImage(img); setLayout(new BorderLayout()); add(BorderLayout.CENTER, panel); setLocation(200, 200); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); pack(); setVisible(true); } public void changeImage(BufferedImage image) { if ((panel != null) && (panel.getImage() != image)) panel.setImage(image); } }

Es bastante sencillo copiar y pegar del ejemplo que proporcionó @MadProgrammer.

Lo único que queda es el uso de EDT , que es más bien mágico para mí. Todavía estoy invocando este código de una manera sucia :

MyJFrame mjf = null; javax.swing.SwingUtilities.invokeLater(mjf = new MyJFrame(buffer, "RDP")); ... mjf.changeImage(buffer);

Mi pregunta es: ¿cómo uso el método changeImage con EDT ?

Este es un problema que me molesta por algunas horas y no puedo encontrar una solución por mi cuenta ...

He encontrado temas similares en toda la red, pero no pude encontrar exactamente el mismo problema con una solución bien explicada y lo más simple posible. También miré los documentos API de EDT y SwingWorker , pero fue demasiado complicado para mí :(

Entonces, vayamos al grano. Tengo un JFrame simple con JLabel adentro, que consiste en mi imagen:

private static class MyJLabel extends JLabel { private ImageIcon img = null; public MyJLabel(ImageIcon img) { super(); this.img = img; } @Override public void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(img.getImage(), 0, 0, getWidth(), getHeight(), this); } } private static class MyJFrame extends JFrame implements Runnable { private BufferedImage img = null; private MyJLabel label = null; public MyJFrame(BufferedImage image, String title) { super(title); img = image; } @Override public void run() { Dimension dims = new Dimension(img.getWidth(), img.getHeight()); dims = new Dimension(dims.width / 2, dims.height / 2); label = new MyJLabel(new ImageIcon(img)); label.setPreferredSize(dims); addComponentListener(new ComponentAdapter() { @Override public void componentResized(ComponentEvent e) { label.repaint(); } }); setLayout(new BorderLayout()); getContentPane().add(BorderLayout.CENTER, label); setLocation(200, 200); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); pack(); setVisible(true); } public void changeImage(BufferedImage image) { img = image; if (label != null) { label.setIcon(new ImageIcon(img)); label.repaint(); } } }

Es invocado por este fragmento de código:

buffer = receiveImage(in); // download image MyJFrame f = null; javax.swing.SwingUtilities.invokeLater(f = new MyJFrame(buffer, "RDP")); int x = 0; while (x <= 15) { txt.println("next"); // notify server that we are ready while (true) { // wait for server if (reader.readLine().equals("ready")) break; } buffer = receiveImage(in); // download image // do some magic here and refresh image somehow :( f.changeImage(buffer); // does not work! x++; }

Lamentablemente, mi enfoque con el método changeImage no funciona: no ocurre nada (la GUI se inicia pero nunca se actualiza).

Apreciaría poca ayuda con esto. El ejemplo simple , de trabajo con la explicación adecuada sería apreciado más;)

¡Saludos!


Personalmente, cambiaría su tamaño antes de aplicarlo a la etiqueta o usar un JPanel para realizar la pintura. JLabel tiene mucha funcionalidad arrastrando consigo.

Caso en punto, el problema que tienes es que realmente estás usando setIcon para establecer la imagen, pero usando paintComponent para pintar otra imagen (la inicial) encima de ella

Su etiqueta personalizada toma un ImageIcon como parámetro inicial y lo pinta como tal ...

private static class MyJLabel extends JLabel { private ImageIcon img = null; public MyJLabel(ImageIcon img) { super(); this.img = img; } @Override public void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(img.getImage(), 0, 0, getWidth(), getHeight(), this); } }

Inicializaste como tal ...

label = new MyJLabel(new ImageIcon(img));

Cabe señalar que si Icon soporte de Icon de JLabel , esto ...

label.setPreferredSize(dims);

Sería irrelevante ya que JLabel usaría el tamaño del icono para determinar su tamaño preferido ... pero de cualquier forma ...

Luego actualiza el ícono usando esto ..

img = image; if (label != null) { label.setIcon(new ImageIcon(img)); label.repaint(); }

Debe señalarse que, de acuerdo con su ejemplo, esto se ha llamado realmente fuera del EDT, que es peligroso y podría dar lugar a una pintura sucia.

Pero setIcon nunca cambia el valor de img dentro de MyLabel , así que cuando se llama a su método paintComponent , en realidad está pintando sobre el ícono que ha proporcionado en la actualización ...

// Paint the new Icon super.paintComponent(g); // Paint the old/initial image... g.drawImage(img.getImage(), 0, 0, getWidth(), getHeight(), this);

Actualizado

Personalmente, lo que haría sería crear un componente personalizado, usando algo como un JPanel y escalar la imagen original en función del tamaño actual del panel, por ejemplo ...

Ahora, normalmente, al realizar escalas de imagen, prefiero usar un enfoque de dividir y conquistar como se demuestra en Java: mantenimiento de la relación de aspecto de la imagen de fondo de JPanel , pero para este ejemplo, simplemente he usado AffineTransform por simplicidad

import java.awt.Dimension; import java.awt.EventQueue; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.HeadlessException; import java.awt.Image; import java.awt.geom.AffineTransform; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; public class ScalableImageExample { public static void main(String[] args) { new ScalableImageExample(); } public ScalableImageExample() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { ex.printStackTrace(); } try { ResizableImagePane pane = new ResizableImagePane(); pane.setImage(...); JFrame frame = new JFrame("Testing"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(pane); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } catch (IOException exp) { exp.printStackTrace(); } } }); } public class ResizableImagePane extends JPanel { private Image img; public ResizableImagePane() { } public void setImage(Image value) { if (img != value) { Image old = img; this.img = value; firePropertyChange("image", old, img); revalidate(); repaint(); } } public Image getImage() { return img; } @Override public Dimension getPreferredSize() { return img == null ? new Dimension(200, 200) : new Dimension(img.getWidth(this), img.getHeight(this)); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); if (img != null) { Graphics2D g2d = (Graphics2D) g.create(); int width = getWidth(); int height = getHeight(); double scaleFactor = getScaleFactorToFit(new Dimension(img.getWidth(this), img.getHeight(this)), getSize()); int x = (int)((width - (img.getWidth(this) * scaleFactor)) / 2); int y = (int)((height - (img.getHeight(this) * scaleFactor)) / 2); AffineTransform at = new AffineTransform(); at.translate(x, y); at.scale(scaleFactor, scaleFactor); g2d.setTransform(at); g2d.drawImage(img, 0, 0, this); g2d.dispose(); } } public double getScaleFactor(int iMasterSize, int iTargetSize) { return (double) iTargetSize / (double) iMasterSize; } public double getScaleFactorToFit(Dimension original, Dimension toFit) { double dScale = 1d; if (original != null && toFit != null) { double dScaleWidth = getScaleFactor(original.width, toFit.width); double dScaleHeight = getScaleFactor(original.height, toFit.height); dScale = Math.min(dScaleHeight, dScaleWidth); } return dScale; } } }