Results 1 to 8 of 8
Thread: how change the update images
- 12-10-2011, 09:01 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 76
- Rep Power
- 0
how change the update images
i am writing a program that every time the user selects an item from a combo box the image in a panel changes.The problem im having is that i want to know how to update the image when the person selects the a new item using the paintComponent method. this is the code for the class that draws the image
Java Code:import java.io.*; import java.awt.image.*; import java.awt.*; import javax.imageio.ImageIO; import javax.swing.*; public class DrawImage extends JPanel { private String imgUrl; //constructor public DrawImage (String imgUrl) { setImageUrl (imgUrl); }//end constructor //constructor public DrawImage () { }//end constructor /** * setImageUrl method * * this method sets the url of image * which will be drawn to the screen. */ public void setImageUrl (String imgUrl) { this.imgUrl = imgUrl; }//end setImageUrl method /** * getImageUrl method * * gets the url of image */ public String getImageUrl () { return imgUrl; }//end getImageUrl method //Draws image to panel public void paintComponent (Graphics g) { super.paintComponents (g); BufferedImage img = null; try //creates bufferedImage { img = ImageIO.read(new File (imgUrl)); } catch (IOException e) { e.printStackTrace (); }//end catch g.drawImage (img,0,0,null); }//end paintComponent method }//end DrawImage class
-
Re: how change the update images
Never read in an image or any other type of file from within a paint or paintComponent method as this at best will slow down your application's ability to paint itself or freeze your application at worst. Much better is to read in the BufferedImage from within the setImageUrl method, and then once done, to call repaint.
- 12-10-2011, 09:42 PM #3
Member
- Join Date
- Jul 2011
- Posts
- 76
- Rep Power
- 0
Re: how change the update images
thanks
- 12-10-2011, 09:49 PM #4
Member
- Join Date
- Jul 2011
- Posts
- 76
- Rep Power
- 0
Re: how change the update images
the repaint method which class does that belong or better yet give me example has how to repaint, this were im at now:
Java Code:public void setImageUrl (String imgUrl) { this.imgUrl = imgUrl; BufferedImage img = null; try { img= ImageIO.read(new File (imgUrl)); } catch (IOException e) { e.printStackTrace(); } }//end setImageUrl method
-
Re: how change the update images
You'll want to repaint object that draws the image, your DrawImage class. You'll need to draw the image in the paintComponent method after checking first that it's not null.
- 12-10-2011, 09:57 PM #6
Member
- Join Date
- Jul 2011
- Posts
- 76
- Rep Power
- 0
Re: how change the update images
wait, so how do i paint it without using the paintComponent method in the first place
-
Re: how change the update images
You use paintComponent, and in fact draw your BufferedImage in this method using the Graphics object, g's, drawImage method, but only first after checking that the image is not null. What you do is make your BufferedImage variable, img, a class field, then read in the BufferedImage into img in your setImageUrl method. Then call repaint(). This will make the JVM call paintComponent and thereby draw your image.
- 12-11-2011, 12:28 AM #8
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Re: how change the update images
See: How to Use Combo Boxes (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components) for a working example.
Similar Threads
-
how to mark on 2 images at a time,both images are on different JPanel
By smitharavi in forum AWT / SwingReplies: 0Last Post: 12-16-2010, 05:14 PM -
how to scroll 2 images at a time(synchronisation),both images are on different panels
By smitharavi in forum AWT / SwingReplies: 4Last Post: 12-16-2010, 04:32 PM -
Update the JFrame after change the Content Pane
By alisonchan30 in forum AWT / SwingReplies: 1Last Post: 04-26-2010, 06:22 AM -
update a change in a Jtable directly to the mysql table
By Muffel in forum JDBCReplies: 0Last Post: 02-21-2010, 11:51 AM -
How to update the large amount of thumbnail images in background process
By Theesh in forum AWT / SwingReplies: 3Last Post: 11-10-2009, 10:24 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks