Carregar imagens...[RESOLVIDO]

7 respostas
Rostery

eu tinha postado esse tópico no Java básico, mas resolvi postar ele aki novamente, acho que será melhor, e ficará no lugar certo, mas enfim, naum conseiguo carregar minha imagem no componente Image...

eu consigui carregar no Label o caminho dela, mas ela aparece na tela rapidão e já some,,,naum da nem pra ver direito...rs

como que faço pra ela fica parada nu lagar certo??

meu codigo :

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import sun.tools.jar.JarImageSource;


public class JnPrincipal extends JFrame implements ActionListener {
	ConectBanco bd = new ConectBanco();
	PicturePanel pp = new PicturePanel();
	private JTabbedPane tabs;
	private Image imfoto;
	private JButton btAnt;
	private JButton btProx;
	private JButton btAbrir;
	private Toolkit tk = Toolkit.getDefaultToolkit();
	private FileDialog fd = new FileDialog(this, "Selecione a Imagem: ");
	private JLabel lbcaminho = new JLabel();

	public JnPrincipal(){
		super("Janela Principal");
		//ABA 1
		tabs = new JTabbedPane();
		JPanel panel = new JPanel();
		ImageIcon icon = new ImageIcon("diam-verm.gif");
		JLabel lbnome = new JLabel("Nome: ");
			JTextField tfnome = new JTextField(20);
		JLabel lbcidade = new JLabel("Cidade: ");
			JTextField tfcidade = new JTextField(30);
		JLabel lbcontato = new JLabel("Contato: ");
			JTextField tfcontato = new JTextField(30);
		JLabel lbrg = new JLabel("RG: ");
			JTextField tfrg = new JTextField(10);
		JLabel lbfone = new JLabel("Telefone: ");
			JTextField tffone = new JTextField(11);
		JLabel lbemail = new JLabel("E-mail: ");
			JTextField tfemail = new JTextField(30);
		JLabel lbend = new JLabel("Endereço: ");
			JTextField tfend = new JTextField(30);
		JLabel lbfoto = new JLabel("Foto: ");
		
		//ImageIcon iifoto = new ImageIcon(imfoto);
		//lbcaminho = new JLabel(iifoto);
		

				
		/*
		 response.setContentType("image/png");
		 OutputStream os = response.getOutputStream();
		 ImageIO.write(buffer, "png", os);
		 os.close();  
*/
			
		//====ADD===
		panel.add(lbnome);
		panel.add(tfnome);
		panel.add(lbcidade);
		panel.add(tfcidade);
		panel.add(lbcontato);
		panel.add(tfcontato);	
		panel.add(lbrg);
		panel.add(tfrg);
		panel.add(lbfone);
		panel.add(tffone);
		panel.add(lbend);
		panel.add(tfend);
		panel.add(lbemail);
		panel.add(tfemail);
		panel.add(lbfoto);	
		panel.add(btAbrir = new JButton("Abrir"),"West");
		panel.add(lbcaminho);
		
 		

		btAbrir.addActionListener(this);
		//addWindowListener(new CloseAndExit());
			
		//ta.setLineWrap(true); ta.setWrapStyleWord(true);
		tabs.addTab("Entrada", new ImageIcon("diam-azul.gif"), panel);
		
		//ABA 2	
		JPanel panel2 = new JPanel();
		panel2.setBackground(Color.white);
		tabs.addTab("Saída", new ImageIcon("diam-lilas.gif"), panel2);
		//ABA 3
		panel = new JPanel();
		panel.add(new JLabel("Teste"));
		tabs.addTab("Relatorio", new ImageIcon("diam-azul.gif"), panel);
		//ABA 4
		JPanel panel4 = new JPanel();
		tabs.addTab("Localizar", new ImageIcon("diam-verde.gif"), panel4);
		getContentPane().add(tabs, "Center");
		//Painel de COntrole
		JPanel p1 = new JPanel(), p2 = new JPanel(new GridLayout(1,1,1,1));
		p2.add(btAnt = new JButton("Anterior"));
		p2.add(btProx = new JButton("Próximo"));
		p1.add(p2);
		getContentPane().add(p1, "North");
		btAnt.addActionListener(this);
		btProx.addActionListener(this);
		setSize(550,480);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				
	}
	
//public void actionPerformed(ActionEvent e) {
	 public void actionPerformed(ActionEvent e) {
		  	PicturePanel pp = new PicturePanel();
		  	fd.show();
		  	if(fd.getFile()!= null){ 		
		  			lbcaminho.setText(fd.getDirectory()+fd.getFile());
		  			imfoto = tk.getImage(lbcaminho.getText());
		  			repaint(); 	
		  	}

	/*
	int i = tabs.getSelectedIndex();
	if(e.getSource()==btAnt)
		tabs.setSelectedIndex(i>0?i-1:tabs.getTabCount()-1);
	else
		tabs.setSelectedIndex(i<tabs.getTabCount()-1?i+1:0);
		*/
}
	
public void paint(Graphics g){
	if(imfoto != null){
		Dimension apl = getSize();
		int w = imfoto.getWidth(this), h = imfoto.getHeight(this);
		g.drawImage(imfoto, (apl.width-w)/2, (apl.height-h)/2, this);
		}
	super.paint(g);
}

public static void main(String[] args){
	new JnPrincipal().show();
	

}
	
// TODO Auto-generated method stub
	
}

vlw galera..

7 Respostas

dudaskank

bem, nem prestei atenção, mas tenta colocar o seu super.paint() no começo do seu método paint().

flw

Rostery

cara, vc é foda!!!
muito obrigado velho…!!!
deu certo!!!

dudaskank

De nada

Só explicando o porque vc via sua imagem rapidamente e ela “sumia” depois.

Na sua rotina, você primeiro estava desenhando a imagem corretamente, até aí tudo certo, mas depois, você chama o método paint() original, e como seu componente é opaco, ele se desenha pintando um retângulo cinza na tela. Acredito que existe um método da classe que altera esse atributo, mas não estou lembrando agora… aí acho que iria aparecer também.

Se não me engano é isso rsrsrs…

flw

Rostery

rs, continuando com as imagens, cada vez gera um problema…
muito legal, consigui visualizar ela,blezinha, mais agora ele tampa toda a tela…procurei aki nu forum como redimensionar, mais o unico que achei estava eim modo web…e ainda naum consigui redimenciona-la, alguem sabe de algum metodo ow classe existente pa redimensionar ela:?

vlw.
:0

dudaskank

Bom, tem esses posts aqui, veja se ajuda:

http://www.guj.com.br/posts/list/34650.java
http://www.guj.com.br/posts/list/28714.java#210780

flw

Rostery

Eae, blz galera...

Consigui redimensionar rs, mas com um porém, agora ela aparece em toda a tela, e piscando...rs

vou colocar somente o met. paint. ae...

public void paint(Graphics g){
		super.paint(g);
		 if(imfoto != null){		
			Dimension apl = getSize();
			//int w = 150, h = 150;
			//int w = imfoto.getWidth(this), h = imfoto.getHeight(this);
			//g.drawImage(imfoto, (apl.width-w)/5, (apl.height-h)/1, this);
			imfoto = imfoto.getScaledInstance(
					this.getWidth(), this.getHeight(), 10);
			
			MediaTracker media = new MediaTracker(this);
	        media.addImage(imfoto, 0);
	        try {
	            media.waitForID(0);
	        } catch (InterruptedException ie) {
	        }
	        g.drawImage(imfoto, 0, 0, this.getBackground(), this);
	      
	        
	        repaint();  // SE TIRA ESSE REPAINT() ELA PARA DE PISCAR SÓ QUE AINDA CONTINUA EM TODA TELA...
                }
	}

Conforme muda-se as dimensões da tela a imagem tb muda, só q naum era bem isso que era pra acontecer, eu queria q ela tivesse tamanhos próprios tipo, 350x400, e quando selecionada pelo meu FileDialog...
já aparecesse com essas dimensoes na tela...

??da uma dica ae??!!! :cry:

Rostery

Aí Pessoal !!!
Consigui aki ta.
Valew a todos ae...
Vo deixa o código aí ta, se servir pra alguma coisa...
Obrigado a todos.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.Dimension; 
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Point; 
import java.awt.Toolkit; 
import java.awt.Graphics2D; 

import sun.tools.jar.JarImageSource;


public class JnPrincipal extends JFrame implements ActionListener {
	ConectBanco bd = new ConectBanco();
	PicturePanel pp = new PicturePanel();
	private JTabbedPane tabs;
	private Image imfoto;
	private JButton btAnt;
	private JButton btProx;
	private JButton btAbrir;
	private Toolkit tk = Toolkit.getDefaultToolkit();
	private FileDialog fd = new FileDialog(this, "Selecione a Imagem: ");
	private JLabel lbcaminho = new JLabel();
	private JLabel lbimagem = new JLabel(); 

	public JnPrincipal(){
		super("Janela Principal");
		this.setResizable(false);
		//ABA 1

		 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		         Dimension frameSize = getSize();
		         this.setLocation(new Point((screenSize.width - frameSize.width) / 5,
		                 (screenSize.height - frameSize.width) / 5));
		         

		         
		tabs = new JTabbedPane();
		JPanel panel = new JPanel();
		ImageIcon icon = new ImageIcon("diam-verm.gif");
		JLabel lbnome = new JLabel("Nome: ");
			JTextField tfnome = new JTextField(20);
		JLabel lbcidade = new JLabel("Cidade: ");
			JTextField tfcidade = new JTextField(30);
		JLabel lbcontato = new JLabel("Contato: ");
			JTextField tfcontato = new JTextField(30);
		JLabel lbrg = new JLabel("RG: ");
			JTextField tfrg = new JTextField(10);
		JLabel lbfone = new JLabel("Telefone: ");
			JTextField tffone = new JTextField(11);
		JLabel lbemail = new JLabel("E-mail: ");
			JTextField tfemail = new JTextField(30);
		JLabel lbend = new JLabel("Endereço: ");
			JTextField tfend = new JTextField(30);
		JLabel lbfoto = new JLabel("Foto: ");

					
		//====ADD==============================
		panel.add(lbnome);
		lbnome.setLocation(0, 15);
		panel.add(tfnome);
		panel.add(lbcidade);
		panel.add(tfcidade);
		panel.add(lbcontato);
		panel.add(tfcontato);	
		panel.add(lbrg);
		panel.add(tfrg);
		panel.add(lbfone);
		panel.add(tffone);
		panel.add(lbend);
		panel.add(tfend);
		panel.add(lbemail);
		panel.add(tfemail);
		panel.add(lbfoto);	
		panel.add(btAbrir = new JButton("Abrir"),"West");
		panel.add(lbcaminho);
		
		
		btAbrir.addActionListener(this);
		
		tabs.addTab("Entrada", new ImageIcon("diam-azul.gif"), panel);
		
		//ABA 2	
		JPanel panel2 = new JPanel();
		panel2.setBackground(Color.white);
		tabs.addTab("Saída", new ImageIcon("diam-lilas.gif"), panel2);
		//ABA 3
		panel = new JPanel();
		panel.add(new JLabel("Teste"));
		tabs.addTab("Relatorio", new ImageIcon("diam-azul.gif"), panel);
		//ABA 4
		JPanel panel4 = new JPanel();
		tabs.addTab("Localizar", new ImageIcon("diam-verde.gif"), panel4);
		getContentPane().add(tabs, "Center");
		
		//Painel de COntrole
		JPanel p1 = new JPanel(), p2 = new JPanel(new GridLayout(1,1,1,1));
		p2.add(btAnt = new JButton("Anterior"));
		p2.add(btProx = new JButton("Próximo"));
		p1.add(p2);
		getContentPane().add(p1, "North");
		btAnt.addActionListener(this);
		btProx.addActionListener(this);
		setSize(450,400);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				
	}
	
	
 public void actionPerformed(ActionEvent e) {
		    fd.show();
		  	if(fd.getFile()!= null){
		  			lbcaminho.setText(fd.getDirectory()+fd.getFile());
		  			imfoto = tk.getImage(lbcaminho.getText());
		  					  				  		 			 
	 }		 
	 }
 
public void paint(Graphics g){
	super.paint(g);
	if(imfoto != null){		
		Dimension apl = getSize();
		imfoto = imfoto.getScaledInstance(
				
		this.getWidth(), this.getHeight(), 10);
					
		MediaTracker media = new MediaTracker(this);
        media.addImage(imfoto, 0);
        try {
            media.waitForID(0);
        } catch (InterruptedException ie) {
        }
        g.drawImage(imfoto, 0, 280, 150, 150, this);        
	}		
	}	
	public static void main(String[] args){
	new JnPrincipal().show();			
	}
	
	}

Falta arrumar algumas coisinhas, tipo layout e implentação de ABAS, mas o foco era para o redimensionamento da imagem...tá. aí intaum!!!!
Flw!!! Valew!!
:-o :arrow:

Criado 24 de agosto de 2006
Ultima resposta 31 de ago. de 2006
Respostas 7
Participantes 2