loading images...

RTHANGAVEL 0 Tallied Votes 108 Views Share

I wrote this code to load the image on mouse click...but it is not displaying as soon as i click the mouse...i need to resize the jframe to make that visible what is the problem with this.......please anybody ans for this....thank u in advance...

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.applet.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
class check extends JFrame
{
JPanel panel;
JButton button;
public static void main(String args[])
{
check obj=new check();
obj.go();
obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
obj.setVisible(true);
}
void go()
{
button=new JButton("hai..");
button.addActionListener(new listener(this));
Toolkit tool=Toolkit.getDefaultToolkit();
Dimension dim=tool.getScreenSize();
setSize(dim.width-100,dim.height-100);
getContentPane().add(BorderLayout.NORTH,button);
}
}

class listener implements ActionListener
{
myimage image1;
check obj;
listener(check obj)
{
this.obj=obj;
}
public void actionPerformed(ActionEvent ae)
{
image1=new myimage(obj);
System.out.println("called..");
obj.getContentPane().add(BorderLayout.CENTER,image1);
System.out.println("below..");
image1.repaint();
obj.repaint(0,0,obj.getSize().width,obj.getSize().height);
//obj.setBounds(10,10,obj.getSize().width-1 , obj.getSize().height-1);
//obj.setSize(obj.getSize().width+10,obj.getSize().height+10);
}


}
class myimage extends JPanel
{
Image image;
check obj;
myimage(check obj)
{
this.obj=obj;
try{       
image=ImageIO.read(new File("tests.jpg"));   }catch(IOException e){e.printStackTrace();}
System.out.println("called..1");
}
public void paintComponent(Graphics g)
{
System.out.println("executed...");
super.paintComponent(g);
g.drawImage(image,0,0,obj);
}

}
gangsta1903 2 Junior Poster

Firstly, I think you should obey java naming convention and indention rules. I didnt exactly understand your design but I can tell you why you will have to resize.

Its because you use paint() method. The frame calles this method whenever the frame resizes.

cbarton.a 0 Newbie Poster

RTHANGAVEL, I would not use the repaint() method to refresh the contents of the JFrame, I would use a pack() method to show and resize the window.

After cleaning the code up a little, here is what I believe you are wanting with comments to help you understand what is happening:

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;


class check extends JFrame
{
JPanel panel;
JButton button;
	public static void main(String args[])
	{
		check obj=new check();
		obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
		obj.setSize(dim.width-100,dim.height-100); 
		
		obj.setVisible(true);
	}
	
	// Instead of using a new method to set up the check, just use its constructor.
	public check(){
	{
		button = new JButton("hai..");
		
		getContentPane().add(button, BorderLayout.NORTH);
		
		// Add an actionListener to the button, on the click: create the new image, add the image to the window, 
		// make the button disappear, then resize to fit the image.
		// There is no need to pass through the parent component since you are creating a JPanel and can add that through here.
		button.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				myimage image = new myimage();
				getContentPane().add(image);
				button.setVisible(false);
				pack();
			}
		});
		
	}
}


class myimage extends JPanel
{
	private ImageIcon image;
	
	public myimage()
	{
		image= new ImageIcon("src/tests.jpg");
	}
	
	// Paint the image on the JPanel.
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		image.paintIcon(this, g, 0,0);
	}

	// Returns the image dimension so that when we pack() the JFrame, we know how large the image is.
	public Dimension getPreferredSize(){
		return new Dimension(image.getIconWidth(), image.getIconHeight());
	}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.