Olá gente…
tenho um programa que altera imagens em tempo de execução.
mas não consigo exibir a nova imagem, apenas a original.
estas imagens são alteradas no hd, e não em um buffer temporário.
como faço para atualizar esta imagem no programa?
obg.
Recarregue a imagem do HD e faça um set novamente na classe onde você exibe a imagem.
vc poderia dar um exemplo simples?
pois ja tentei redesenhar componente, usar setIcon…
Poste seu código.
eu uso o dot…trabalho de ed1
Depois de muitas modificações…
import java.awt.CardLayout;
import java.awt.FlowLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTree;
public class Tree implements ActionListener{
//criamos um JFrame chamado 'janela'
JFrame janela = new JFrame();
//criamos um JPanel chamado painel
JPanel painel = new JPanel();
//criamos um JButton chamado sul
JPanel Pnorte = new JPanel();
//criamos um JButton chamado leste
JPanel Pcentro = new JPanel();
//antigamente Icon
ImageIcon fig=new ImageIcon("temp.jpg");
ImageIcon temp;//=new ImageIcon("temp.jpg");
//created by me
JButton Badd=new JButton("Add");
JButton Brem=new JButton("Clear");
JButton Bclose= new JButton("Close");
JTextField NumAdd=new JTextField(10);
JTextField NumRem=new JTextField(10);
JLabel Lfig=new JLabel(fig);
int numAdd,numRem;
//antigo
FileWriter writer;
PrintWriter saida;
public class Node{
public int key;
Node left,right;
Node(int k){
key=k;
left=right=null;
}
void setKey(int k){
key=k;
}
int getKey(){
return key;
}
}
Node root;
private Tree(){
root=null;
//novo
//definimos o titulo da janela
janela.setTitle("Exemplo de um BoderLayout");
//definimos a largura e a altura da janela
janela.setSize(800, 650);
//define a posicao da janela na tela
janela.setLocation(50, 30);
//define que ao fechar a janela, encerre a aplicacao
janela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//definimos o layout do painel
painel.setLayout(new FlowLayout(FlowLayout.CENTER, 30, 20));
//adicionamos os botoes ao painel
Pnorte.setLayout(new FlowLayout(FlowLayout.CENTER, 30, 20));
Pnorte.add(Badd);
Pnorte.add(NumAdd);
Pnorte.add(Brem);
Pnorte.add(NumRem);
Pnorte.add(Bclose);
painel.add("North", Pnorte);
Pcentro.add(Lfig);
painel.add("Center", Pcentro);
//adicionamos o painel a janela
janela.add(painel);
//mostramos a janela
Badd.addActionListener(this);
Brem.addActionListener(this);
Bclose.addActionListener(this);
janela.setVisible(true);
}
void insert(int k){
if(root==null){
root=new Node(k);
return;
}
Node step=root;
Node next=null;
do{
if(k>step.key){
next=step.right;
}else{
if(k<step.key){
next=step.left;
}else{
JOptionPane.showMessageDialog(null, "Ja existe um no com o numero "+k);
return;
}
}
if(next!=null){
step=next;
}
}while(next!=null);
if(k>step.key){
step.right=new Node(k);
}else{
step.left=new Node(k);
}
}
Node busca(int k){
Node step=root;
while(step!=null){
if(step.key==k){
break;
}
if(k>step.key){
step=step.right;
}else{
step=step.left;
}
}
return step;
}
void deleteMerge(int k){
Node aux,aux2,prev,node,step;
if(root==null){
JOptionPane.showMessageDialog(null,"Nao existe um no "+k);
return;
}
if(root.key==k){
if(root.left==null){
aux=root;
root=root.right;
aux=null; //delete aux , java tem coletor
return;
}
if(root.right==null){
aux=root;
root=root.left;
aux=null;//delete aux;
return;
}
aux=root;
aux2=root.right;
root=root.left;
aux=null;//delete aux;
aux=root;
while(aux.right!=null){
aux=aux.right;
}
aux.right=aux2;
return;
}
prev=root;
do{
if(k>prev.key){
node=prev.right;
}else{
node=prev.left;
}
if(node==null){
JOptionPane.showMessageDialog(null, "Nao existe um elemento "+k);
return;
}
if(node.key!=k){
prev=node;
}
}while(node.key!=k);
if(node.left==null){
if(k>prev.key){
prev.right=node.right;
node=null;//delete node;
return;
}else{
prev.left=node.right;
aux=null;//delete aux;
return;
}
}
if(node.right==null){
if(k>prev.key){
prev.right=node.left;
node=null;//delete node;
return;
}else{
prev.left=node.left;
node=null;//delete node;
return;
}
}
aux=node.right;
if(k>prev.key){
prev.right=node.left;
}else{
prev.left=node.left;
}
step=node.left;
node=null;//delete node;
while(step.right!=null){
step=step.right;
}
step.right=aux;
return;
}
void desenhar() throws IOException{
writer= new FileWriter(new File("arquivo.dot"));
saida= new PrintWriter(writer,true);
saida.println("digraph t{ ");
print(root);
saida.println(" }");
saida.close();
writer.close();
Runtime.getRuntime().exec("dot arquivo.dot -Tjpg -ofig.jpg");
//devo entao atualizar a imagem
janela.setVisible(false);
//janela.remove(painel);
//painel.remove(Pnorte);
//painel.remove(Pcentro);
//Pnorte.remove(Badd);
//Pnorte.remove(NumAdd);
//Pnorte.remove(Brem);
//Pnorte.remove(NumRem);
//Pnorte.remove(Bclose);
//Pcentro.remove(Lfig);
if(root!=null){
fig=new ImageIcon("fig.jpg");
}else{
fig=new ImageIcon("temp.jpg");
}
Lfig.setIcon(fig);
Lfig.repaint();
janela.repaint();
janela.setVisible(true);
//Pcentro.add(Lfig);
//Pnorte.add(Badd);
//Pnorte.add(NumAdd);
//Pnorte.add(Brem);
//Pnorte.add(NumRem);
//Pnorte.add(Bclose);
//painel.add("North", Pnorte);
//painel.add("Center", Pcentro);
//janela.add(painel);
//janela.setVisible(true);
}
void print(Node n){
if(root==null){
return;
}
if(n.left==null){
saida.println(" T"+n.key+"L[label=\"n\" color=white fontcolor=white] "+n.key+"->T"+n.key+"L ");
}else{
saida.println(" "+n.key+"->"+(n.left).key+" ");
print(n.left);
}
if(n.right==null){
saida.println(" T"+n.key+"R[label=\"n\" color=white fontcolor=white] "+n.key+"->T"+n.key+"R ");
}else{
saida.println(" "+n.key+"->"+(n.right).key+" ");
print(n.right);
}
}
public void actionPerformed(ActionEvent e){
if (e.getSource() == Badd){
JOptionPane.showMessageDialog(null, "Botao Badd clicado");
numAdd=Integer.parseInt(NumAdd.getText());
insert(numAdd);
try {
desenhar();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
JOptionPane.showMessageDialog(null, "Excessao levantada");
}
NumAdd.setText(null);
}
if (e.getSource() == Brem){
JOptionPane.showMessageDialog(null, "Botao Brem clicado");
numRem=Integer.parseInt(NumRem.getText());
deleteMerge(numRem);
try {
desenhar();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
JOptionPane.showMessageDialog(null, "Excessao levantada");
}
NumRem.setText(null);
}
if (e.getSource() == Bclose){
//JOptionPane.showMessageDialog(null, "Botao Bclose clicado");
System.exit(0);
}
}
public static void main(String args[]){
new Tree();
}
}
basicamente, represento a minha arvore em um arquivo.dot
depois, crio uma imagem a partir do arquivo.
depois, atualizo a imagem no hd.
depois, aqui esta o problema, atualizo a imagem no programa.
Poste seu código usando a tag code:
[code]//package pacote;
import java.awt.CardLayout;
import java.awt.FlowLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTree;
public class Tree implements ActionListener{
//criamos um JFrame chamado 'janela'
JFrame janela = new JFrame();
//criamos um JPanel chamado painel
JPanel painel = new JPanel();
//criamos um JButton chamado sul
JPanel Pnorte = new JPanel();
//criamos um JButton chamado leste
JPanel Pcentro = new JPanel();
//antigamente Icon
ImageIcon fig=new ImageIcon("temp.jpg");
ImageIcon temp;//=new ImageIcon("temp.jpg");
//created by me
JButton Badd=new JButton("Add");
JButton Brem=new JButton("Clear");
JButton Bclose= new JButton("Close");
JTextField NumAdd=new JTextField(10);
JTextField NumRem=new JTextField(10);
JLabel Lfig=new JLabel(fig);
int numAdd,numRem;
//antigo
FileWriter writer;
PrintWriter saida;
public class Node{
public int key;
Node left,right;
Node(int k){
key=k;
left=right=null;
}
void setKey(int k){
key=k;
}
int getKey(){
return key;
}
}
Node root;
private Tree(){
root=null;
//novo
//definimos o titulo da janela
janela.setTitle("Exemplo de um BoderLayout");
//definimos a largura e a altura da janela
janela.setSize(800, 650);
//define a posicao da janela na tela
janela.setLocation(50, 30);
//define que ao fechar a janela, encerre a aplicacao
janela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//definimos o layout do painel
painel.setLayout(new FlowLayout(FlowLayout.CENTER, 30, 20));
//adicionamos os botoes ao painel
Pnorte.setLayout(new FlowLayout(FlowLayout.CENTER, 30, 20));
Pnorte.add(Badd);
Pnorte.add(NumAdd);
Pnorte.add(Brem);
Pnorte.add(NumRem);
Pnorte.add(Bclose);
painel.add("North", Pnorte);
Pcentro.add(Lfig);
painel.add("Center", Pcentro);
//adicionamos o painel a janela
janela.add(painel);
//mostramos a janela
Badd.addActionListener(this);
Brem.addActionListener(this);
Bclose.addActionListener(this);
janela.setVisible(true);
}
void insert(int k){
if(root==null){
root=new Node(k);
return;
}
Node step=root;
Node next=null;
do{
if(k>step.key){
next=step.right;
}else{
if(k<step.key){
next=step.left;
}else{
JOptionPane.showMessageDialog(null, "Ja existe um no com o numero "+k);
return;
}
}
if(next!=null){
step=next;
}
}while(next!=null);
if(k>step.key){
step.right=new Node(k);
}else{
step.left=new Node(k);
}
}
Node busca(int k){
Node step=root;
while(step!=null){
if(step.key==k){
break;
}
if(k>step.key){
step=step.right;
}else{
step=step.left;
}
}
return step;
}
void deleteMerge(int k){
Node aux,aux2,prev,node,step;
if(root==null){
JOptionPane.showMessageDialog(null,"Nao existe um no "+k);
return;
}
if(root.key==k){
if(root.left==null){
aux=root;
root=root.right;
aux=null; //delete aux , java tem coletor
return;
}
if(root.right==null){
aux=root;
root=root.left;
aux=null;//delete aux;
return;
}
aux=root;
aux2=root.right;
root=root.left;
aux=null;//delete aux;
aux=root;
while(aux.right!=null){
aux=aux.right;
}
aux.right=aux2;
return;
}
prev=root;
do{
if(k>prev.key){
node=prev.right;
}else{
node=prev.left;
}
if(node==null){
JOptionPane.showMessageDialog(null, "Nao existe um elemento "+k);
return;
}
if(node.key!=k){
prev=node;
}
}while(node.key!=k);
if(node.left==null){
if(k>prev.key){
prev.right=node.right;
node=null;//delete node;
return;
}else{
prev.left=node.right;
aux=null;//delete aux;
return;
}
}
if(node.right==null){
if(k>prev.key){
prev.right=node.left;
node=null;//delete node;
return;
}else{
prev.left=node.left;
node=null;//delete node;
return;
}
}
aux=node.right;
if(k>prev.key){
prev.right=node.left;
}else{
prev.left=node.left;
}
step=node.left;
node=null;//delete node;
while(step.right!=null){
step=step.right;
}
step.right=aux;
return;
}
void desenhar() throws IOException{
writer= new FileWriter(new File("arquivo.dot"));
saida= new PrintWriter(writer,true);
saida.println("digraph t{ ");
print(root);
saida.println(" }");
saida.close();
writer.close();
Runtime.getRuntime().exec("dot arquivo.dot -Tjpg -ofig.jpg");
//devo entao atualizar a imagem
janela.setVisible(false);
//janela.remove(painel);
//painel.remove(Pnorte);
//painel.remove(Pcentro);
//Pnorte.remove(Badd);
//Pnorte.remove(NumAdd);
//Pnorte.remove(Brem);
//Pnorte.remove(NumRem);
//Pnorte.remove(Bclose);
//Pcentro.remove(Lfig);
if(root!=null){
fig=new ImageIcon("fig.jpg");
}else{
fig=new ImageIcon("temp.jpg");
}
Lfig.setIcon(fig);
Lfig.repaint();
janela.repaint();
janela.setVisible(true);
//Pcentro.add(Lfig);
//Pnorte.add(Badd);
//Pnorte.add(NumAdd);
//Pnorte.add(Brem);
//Pnorte.add(NumRem);
//Pnorte.add(Bclose);
//painel.add("North", Pnorte);
//painel.add("Center", Pcentro);
//janela.add(painel);
//janela.setVisible(true);
}
void print(Node n){
if(root==null){
return;
}
if(n.left==null){
saida.println(" T"+n.key+"L[label=\"n\" color=white fontcolor=white] "+n.key+"->T"+n.key+"L ");
}else{
saida.println(" "+n.key+"->"+(n.left).key+" ");
print(n.left);
}
if(n.right==null){
saida.println(" T"+n.key+"R[label=\"n\" color=white fontcolor=white] "+n.key+"->T"+n.key+"R ");
}else{
saida.println(" "+n.key+"->"+(n.right).key+" ");
print(n.right);
}
}
public void actionPerformed(ActionEvent e){
if (e.getSource() == Badd){
JOptionPane.showMessageDialog(null, "Botao Badd clicado");
numAdd=Integer.parseInt(NumAdd.getText());
insert(numAdd);
try {
desenhar();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
JOptionPane.showMessageDialog(null, "Excessao levantada");
}
NumAdd.setText(null);
}
if (e.getSource() == Brem){
JOptionPane.showMessageDialog(null, "Botao Brem clicado");
numRem=Integer.parseInt(NumRem.getText());
deleteMerge(numRem);
try {
desenhar();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
JOptionPane.showMessageDialog(null, "Excessao levantada");
}
NumRem.setText(null);
}
if (e.getSource() == Bclose){
//JOptionPane.showMessageDialog(null, "Botao Bclose clicado");
System.exit(0);
}
}
public static void main(String args[]){
new Tree();
}
}[/code]
procurando em meus arquivos, vi este código que é o mais atualizado
do que o postado acima
(só muda um layout,
altera o conteúdo do comando de gerar imagem
e tenta uma nova forma de atualização)
mas, ainda assim, não exibe a nova imagem
//package pacote;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Tree implements ActionListener{
//criamos um JFrame chamado 'janela'
JFrame janela = new JFrame();
//criamos um JPanel chamado painel
JPanel painel = new JPanel();
//criamos um JButton chamado sul
JPanel Pnorte = new JPanel();
//criamos um JButton chamado leste
JPanel Pcentro = new JPanel();
//antigamente Icon
ImageIcon fig;
JButton Badd=new JButton("Add");
JButton Brem=new JButton("Clear");
JButton Bclose= new JButton("Close");
JTextField NumAdd=new JTextField(10);
JTextField NumRem=new JTextField(10);
JLabel Lfig;
int numAdd,numRem;
FileWriter writer;
PrintWriter saida;
public class Node{
public int key;
Node left,right;
Node(int k){
key=k;
left=right=null;
}
void setKey(int k){
key=k;
}
int getKey(){
return key;
}
}
Node root;
private Tree(){
root=null;
fig=new ImageIcon("C:\Users\daniel\Desktop\Ed1Java\BTree\temp.jpg");
Lfig=new JLabel(fig);
//definimos o título da janela
janela.setTitle("Exemplo de um BoderLayout");
//definimos a largura e a altura da janela
janela.setSize(800, 650);
//define a posição da janela na tela
janela.setLocation(50, 30);
//define que ao fechar a janela, encerre a aplicação
janela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//definimos o layout do painel
painel.setLayout(new BorderLayout());
//adicionamos os botões ao painel
Pnorte.setLayout(new FlowLayout(FlowLayout.CENTER, 30, 20));
Pcentro.setLayout(new FlowLayout(FlowLayout.CENTER, 30, 20));
Pnorte.add(Badd);
Pnorte.add(NumAdd);
Pnorte.add(Brem);
Pnorte.add(NumRem);
Pnorte.add(Bclose);
painel.add("North", Pnorte);
Pcentro.add(Lfig);
painel.add("Center", Pcentro);
//adicionamos o painel à janela
janela.add(painel);
//mostramos a janela
Badd.addActionListener(this);
Brem.addActionListener(this);
Bclose.addActionListener(this);
janela.setVisible(true);
}
void insert(int k){
if(root==null){
root=new Node(k);
return;
}
Node step=root;
Node next=null;
do{
if(k>step.key){
next=step.right;
}else{
if(k<step.key){
next=step.left;
}else{
JOptionPane.showMessageDialog(null, "Ja existe um nó com o numero "+k);
return;
}
}
if(next!=null){
step=next;
}
}while(next!=null);
if(k>step.key){
step.right=new Node(k);
}else{
step.left=new Node(k);
}
}
Node busca(int k){
Node step=root;
while(step!=null){
if(step.key==k){
break;
}
if(k>step.key){
step=step.right;
}else{
step=step.left;
}
}
return step;
}
void deleteMerge(int k){
Node aux,aux2,prev,node,step;
if(root==null){
JOptionPane.showMessageDialog(null,"Nao existe um no "+k);
return;
}
if(root.key==k){
if(root.left==null){
aux=root;
root=root.right;
aux=null; //delete aux , java tem coletor
return;
}
if(root.right==null){
aux=root;
root=root.left;
aux=null;//delete aux;
return;
}
aux=root;
aux2=root.right;
root=root.left;
aux=null;//delete aux;
aux=root;
while(aux.right!=null){
aux=aux.right;
}
aux.right=aux2;
return;
}
prev=root;
do{
if(k>prev.key){
node=prev.right;
}else{
node=prev.left;
}
if(node==null){
JOptionPane.showMessageDialog(null, "Nao existe um elemento "+k);
return;
}
if(node.key!=k){
prev=node;
}
}while(node.key!=k);
if(node.left==null){
if(k>prev.key){
prev.right=node.right;
node=null;//delete node;
return;
}else{
prev.left=node.right;
aux=null;//delete aux;
return;
}
}
if(node.right==null){
if(k>prev.key){
prev.right=node.left;
node=null;//delete node;
return;
}else{
prev.left=node.left;
node=null;//delete node;
return;
}
}
aux=node.right;
if(k>prev.key){
prev.right=node.left;
}else{
prev.left=node.left;
}
step=node.left;
node=null;//delete node;
while(step.right!=null){
step=step.right;
}
step.right=aux;
return;
}
void desenhar() throws IOException{
writer= new FileWriter(new File("arquivo.dot"));
saida= new PrintWriter(writer,true);
saida.println("digraph t{ ");
print(root);
saida.println(" }");
saida.close();
writer.close();
Runtime.getRuntime().exec(" cmd.exe /C start dot arquivo.dot -Tjpg -ofig.jpg");
//aqui esta o problema !!!
//devo atualizar a imagem
Pcentro.setVisible(false);
if(root!=null){
fig=new ImageIcon("C:\Users\daniel\Desktop\Ed1Java\BTree\fig.jpg");
}else{
fig=new ImageIcon("C:\Users\daniel\Desktop\Ed1Java\BTree\temp.jpg");
}
Pcentro.repaint();
Pcentro.setVisible(true);
}
void print(Node n){
if(root==null){
return;
}
if(n.left==null){
saida.println(" T"+n.key+"L[label=\"n\" color=white fontcolor=white] "+n.key+"->T"+n.key+"L ");
}else{
saida.println(" "+n.key+"->"+(n.left).key+" ");
print(n.left);
}
if(n.right==null){
saida.println(" T"+n.key+"R[label=\"n\" color=white fontcolor=white] "+n.key+"->T"+n.key+"R ");
}else{
saida.println(" "+n.key+"->"+(n.right).key+" ");
print(n.right);
}
}
public void actionPerformed(ActionEvent e){
if (e.getSource() == Badd){
JOptionPane.showMessageDialog(null, "Botao Badd clicado");
numAdd=Integer.parseInt(NumAdd.getText());
insert(numAdd);
try {
desenhar();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
JOptionPane.showMessageDialog(null, "Excessão levantada");
}
NumAdd.setText(null);
}
if (e.getSource() == Brem){
JOptionPane.showMessageDialog(null, "Botao Brem clicado");
numRem=Integer.parseInt(NumRem.getText());
deleteMerge(numRem);
try {
desenhar();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
JOptionPane.showMessageDialog(null, "Excessão levantada");
}
NumRem.setText(null);
}
if (e.getSource() == Bclose){
//JOptionPane.showMessageDialog(null, "Botao Bclose clicado");
System.exit(0);
}
}
public static void main(String args[]){
new Tree();
}
}
Quem estiver com o windows, após instalar o graphviz,
deve configurar a variavel de ambiente path, adicionando no conteúdo
dela o endereço do graphviz no seu pc.
Isto serve quando se vai utilizar o:
Runtime.getRuntime().exec(" cmd.exe /C start dot arquivo.dot -Tjpg -ofig.jpg");
quem estiver com linux, a linha muda para:
Runtime.getRuntime().exec(" dot arquivo.dot -Tjpg -ofig.jpg");
minha “solução” encontrada:
ao ver que as imagens não eram alteradas no programa, apenas na pasta do arquivo,
tive a idéia de, após a criação da imagem com esta linha:
Runtime.getRuntime().exec("dot arquivo.dot -Tjpg -ofig.jpg");
esperar um pouco para utilizá-la…
ou seja, não setar a imagem imediatamente…
e deu certo.
pode-se usar algum método de pausa, ou for’s encadeados.
a questão é, quanto tempo demorará para que a sua imagem já esteja disponível…
testem diferentes valores, encontrem os menores possíveis.
creio que este tempo esta diretamente ligado a quantidade de processos rodando simultaneamente
flw.
ou ainda uma thread…