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...
loading images...
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);
}
}
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.