Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

trying to load the image in a JPanel

  Asked By: James    Date: Feb 24    Category: Java    Views: 1291
  

i'm trying to load an image in a JPanel.
The way i'm trying to do this is that i have
a JFrame and i have declared two JPanels
one for Buttons and one for the iameg to be displayed

when i click the Load button it's not loaded.

i'm sending the code with the message kindly help me out
i'm a beginner :-)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import com.sun.image.codec.jpeg.*;
import java.io.*;


public class LoadandSaveimage extends JFrame
{

JButton b1 , b2 ;
JPanel button_panel , image_panel ;
Image img ;
myListener newHandler ;


public LoadandSaveimage()
{

super ( "Load and Save Image" );

newHandler = new myListener() ;

getContentPane().setLayout( new BorderLayout() );

button_panel = new JPanel() ;

b1 = new JButton( "Load" );
b1.addActionListener( newHandler );

b2 = new JButton ( "Save" );
b2.addActionListener ( newHandler );

button_panel.add ( b1 );
button_panel.add ( b2 );

image_panel = new JPanel();


getContentPane().add ( button_panel ,
BorderLayout.NORTH );
getContentPane().add ( image_panel ,
BorderLayout.CENTER );

addWindowListener
(
new WindowAdapter()
{
public void windowClosing( WindowEvent e )
{
System.exit ( 0 ) ;
}
}
);


setSize ( 600 , 500 );
show();

}// Constructor ended.

public static void main ( String args[] )
{

new LoadandSaveimage();

}


public class myListener implements ActionListener
{
public void actionPerformed( ActionEvent e )
{
if ( e.getSource() == b1 )
{
load();
}

else
if ( e.getSource() == b2 )
{ //empty

}
}
}



public void load()
{
repaint();

}

public void paintComponent ( Graphics g )
{

img = Toolkit.getDefaultToolkit().getImage
( "Splash.jpg" );
System.out.println ("Image Loaded" );
try
{
MediaTracker mediaTracker = new MediaTracker( new
Container() );
mediaTracker.addImage ( img , 0 ) ;
mediaTracker.waitForID( 0 ) ;
}
catch ( Exception ex )
{}
int imageWidth = img.getWidth( null ) ;
int imageHeight = img.getHeight ( null );

BufferedImage thumbnail = new BufferedImage ( imageWidth ,
imageHeight , BufferedImage.TYPE_INT_RGB ) ;

Graphics2D graphics2d = ( Graphics2D ) g ;

graphics2d = thumbnail.createGraphics();

graphics2d.setRenderingHint(
RenderingHints.KEY_INTERPOLATION ,
RenderingHints.VALUE_INTERPOLATION_BILINEAR );

graphics2d.drawImage ( img , 0 , 0 , imageWidth ,
imageHeight ,
this );

}


}

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Fabia Ferrrari     Answered On: Feb 24

try change your code  in method load  to :


public void  load()
{
paintComponents(image_panel.getGraphics());
// the argument is a graphics from panel object
}

public void paintComponents ( Graphics g )
{
//img =
Toolkit.getDefaultToolkit().getImage("Splash.jpg");
img =
Toolkit.getDefaultToolkit().createImage(this.getClass().getResource("Splash.jpg"\
));
System.out.println ("Image Loaded" );

try
{
MediaTracker mediaTracker = new MediaTracker(
new
Container() );
mediaTracker.addImage ( img  , 0 ) ;
mediaTracker.waitForID( 0 ) ;
}
catch ( Exception ex )
{}

int imageWidth = img.getWidth( null ) ;
int imageHeight = img.getHeight ( null );
g.drawImage(img,0,0,imageWidth,imageHeight,null);

/* I don't know what the rest of your code for, but from
this code (
g.drawImage(img,0,0,imageWidth,imageHeight,null);
the image  already loaded..

BufferedImage thumbnail = new BufferedImage (
imageWidth ,
imageHeight , BufferedImage.TYPE_INT_RGB ) ;

Graphics2D graphics2d = ( Graphics2D ) g ;

graphics2d = thumbnail.createGraphics();

graphics2d.setRenderingHint(
RenderingHints.KEY_INTERPOLATION ,
RenderingHints.VALUE_INTERPOLATION_BILINEAR );

graphics2d.drawImage ( img , 0 , 0 ,
imageWidth , imageHeight , this );*/

}

 
Didn't find what you were looking for? Find more on trying to load the image in a JPanel Or get search suggestion and latest updates.




Tagged: