Boa tarde Pessoal!
Sou iniciante no java e estou com um problema que aparentemente parece ser simples, porém depois de algumas tentativas não consegui resolver. Estou fazendo um trabalho de uma disciplina na faculdade utilizando o imageJ, onde o usuário seleciona uma imagem via JFileChooser e é exibido como icon em uma JLabel, até esse ponto tudo Ok. O problema ocorre quando tento atualizar de novo essa Jlabel com uma nova imagem proveniente do efeito que o usuário seleciona via menupopup.
JFileChoser:
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser fileChooser =new JFileChooser();
int returnVal = fileChooser.showOpenDialog(this); if (returnVal == JFileChooser.APPROVE_OPTION) { File file = fileChooser.getSelectedFile(); try {
ImagePlus img = ij.IJ.openImage(file.getCanonicalPath()); IJ.run(img, "Size...", "width=373 height=464 depth=1 constrain average interpolation=Bilinear"); jLabel1.setIcon(new ImageIcon(img.getBufferedImage())); PublicVar publicVar = PublicVar.getInstance(); publicVar.setList(img); } catch (IOException ex) { ex.getMessage(); }
} }
No evento de clique do mouse sobre o JLabel ele cria os menus com as opções de alteração da imagem:
private void jLabel1MouseClicked(java.awt.event.MouseEvent evt) { // TODO add your handling code here: Rotation rotation = new Rotation(); Mirroring mirroring = new Mirroring(); JPopupMenu popup = new JPopupMenu(); popup.add(rotation.Rotation()); popup.add(mirroring.Mirroring());
popup.show(this, evt.getX(), evt.getY()); }
Por exemplo irei selecionar a opção Rotation, que por sua vez instanciará a classe rotate do pacote Effects e chamará o método de rotação retornando como resultado uma imageplus para ser exibida na JLabel e que também é adicionada numa classe singleton para ser recuperada de qualquer parte do projeto.
public class Rotation extends Main.Main {
/** * * @return Retorna um Menu para rotação */ public JMenu Rotation(){
JMenu Rotacao = new JMenu("Rotação");
JMenuItem Rot1 = new JMenuItem("Rotação 90+"); Rot1.addActionListener((ActionEvent e) -> { Rotate r1 = new Rotate(); PublicVar publicVar = PublicVar.getInstance();
ImagePlus alterada = r1.Rot90Pos(publicVar.getlastList()); publicVar.setList(alterada); alterada.show(); jLabel1.setIcon(null); jLabel1.setIcon(new ImageIcon(alterada.getBufferedImage())); jLabel1.repaint();
});
JMenuItem Rot2 = new JMenuItem("Rotação 90-"); Rot2.addActionListener((ActionEvent e) -> { //será implementado código });
Rotacao.add(Rot1); Rotacao.add(Rot2);
return Rotacao;
}
}
Rotate:
public class Rotate {
public ImagePlus Rot90Pos (ImagePlus img){ ImagePlus imgp = img.duplicate(); ij.IJ.run(imgp,"Rotate... ", "angle=90 grid=1 interpolation=Bilinear stack"); return imgp; } public ImagePlus Rot90Neg (ImagePlus img){ ImagePlus imgp = img.duplicate(); ImageProcessor pimgp = imgp.getProcessor(); pimgp.rotateRight(); imgp.updateAndDraw(); return imgp; }}
Singleton:
public class PublicVar {
private final ArrayList<ImagePlus> List = new ArrayList<>();
private PublicVar() { }
public static PublicVar getInstance() { return PublicVarHolder.INSTANCE; }
private static class PublicVarHolder {
private static final PublicVar INSTANCE = new PublicVar(); }
public void setList(ImagePlus img) { List.add(img); }
public ImagePlus getlastList() {
int Max = List.lastIndexOf(this.List); ImagePlus img = List.get(Max + 1); return img; }
}
Fiz um teste usando o Show() do imageplus e a variável alterada está com o valor correto, porém só não exibe na JLabel.
Grato desde já pela ajuda.