1.11M Members

displaying images stored in mySql database as a grid

 
0
 

hey,

i have stored a few images in mySQL database as BLOB.Now i want to retrieve and display the images on frame as a gallery/grid view.I have displayed one image in a frame but when i tried to add more images it fails...Can anyone help with sample code,coz im new to the language....

I've tried the following code,bt it didnt worked:-

public class ImageShow extends JFrame{
  Image image;
  public int x = 30,y=30; 
  ResultSet r;
  public ImageShow(){
  setTitle("Image Retrieved");
  setSize(1000,1000);
  addWindowListener(new WindowAdapter(){
  public void windowClosing(WindowEvent we){
  setVisible(false);
  }
  });
  setVisible(true);
  }
  
  public void paint(Graphics g){
        try {
            r = ImageRetrieve.rs;           
            while (r.next())
                {                 
                    byte[] imagedata = r.getBytes("image_path") ;
                    image = Toolkit.getDefaultToolkit().createImage(imagedata);
                    //Toolkit tool = Toolkit.getDefaultToolkit();
                    g.drawImage(image,x,y,this);
                    x+=30;
                    y+=30;
                 } 
            } 
            catch (SQLException ex) {
                                    Logger.getLogger(ImageShow.class.getName()).log(Level.SEVERE, null, ex);
                                    }  
    }
}

Is there any better way?

 
0
 

when i tried to add more images it fails..

Please explain what fails.

 
0
 

Please explain what fails.

On execution i only gets a new window,its not displaying any images....i heard about grid layout,can i use that here?? wil u help me with some examples?

 
1
 

On execution i only gets a new window,its not displaying any images....i heard about grid layout,can i use that here?? wil u help me with some examples?

maybe see here:http://stackoverflow.com/questions/8500746/java-swing-displaying-multiple-images-dynamically-on-jpanel and here:http://answers.yahoo.com/question/index?qid=20100704125651AAN0AUm
[edit]and this too is helpful:http://docs.oracle.com/javase/7/docs/api/java/awt/GridLayout.html

 
1
 

Rather than overriding paint, you;ll be on easier ground by creating a gridlayout of JLabels and using ImageIcon to place your graphics in the JLabels. (just Google it)
If you must override paint then don't re-load the images from disk every time paint is called. It can get called very frequently and all that repeated IO will grind your GUI to snails-pace. Load them all into memory once when the program starts.

 
0
 

hey,
thanks to everyone who commented and helped me...
i've solved my problem...i used gridlayout for displaying the images and it worked...
code :-

public class ImageShow extends JFrame{
    
    ResultSet r;
    Image img;
   public ImageShow() throws SQLException
    {
        setTitle("Image retrieved");
        setSize(500, 500);
        setDefaultCloseOperation(HIDE_ON_CLOSE);
        Container pane = getContentPane();
        pane.setLayout(new GridLayout(3,3));
        r=ImageRetrieve.rs;
        while (r.next())
            {
                byte[] imagedata = r.getBytes("image_path") ;
                img = Toolkit.getDefaultToolkit().createImage(imagedata);
                img = img.getScaledInstance(200,200,Image.SCALE_SMOOTH);
                ImageIcon icon =new ImageIcon(img);
                JLabel Photo = new JLabel(icon) ;                                  
                pane.add(Photo) ;                
                this.pack();
                this.setVisible(true);             
            } 
    }
    }
Question Answered as of 2 Years Ago by JamesCherrill, NormR1 and DavidKroukamp
 
0
 

Hey What is ImageRetrieve

 
0
 

It looks like that's another of nidheeshkumar.r's classes that goes alongside ImageShow and handles getting the image ResultSet from the database.

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: