We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,124 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

displaying images stored in mySql database as a grid

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?

4
Contributors
5
Replies
22 Hours
Discussion Span
1 Year Ago
Last Updated
6
Views
Question
Answered
nidheeshkumar.r
Newbie Poster
15 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

when i tried to add more images it fails..

Please explain what fails.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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?

nidheeshkumar.r
Newbie Poster
15 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

DavidKroukamp
Master Poster
Team Colleague
735 posts since Dec 2011
Reputation Points: 279
Solved Threads: 181
Skill Endorsements: 4

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.

JamesCherrill
... trying to help
Moderator
8,519 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,455
Skill Endorsements: 30

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);             
            } 
    }
    }
nidheeshkumar.r
Newbie Poster
15 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 1 Year Ago by JamesCherrill, NormR1 and DavidKroukamp

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0735 seconds using 2.75MB