Results 1 to 8 of 8
  1. #1
    mDennis10 is offline Member
    Join Date
    Jul 2011
    Posts
    76
    Rep Power
    0

    Default 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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  3. #3
    mDennis10 is offline Member
    Join Date
    Jul 2011
    Posts
    76
    Rep Power
    0

    Default Re: how change the update images

    thanks

  4. #4
    mDennis10 is offline Member
    Join Date
    Jul 2011
    Posts
    76
    Rep Power
    0

    Default 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

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  6. #6
    mDennis10 is offline Member
    Join Date
    Jul 2011
    Posts
    76
    Rep Power
    0

    Default Re: how change the update images

    wait, so how do i paint it without using the paintComponent method in the first place

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: how change the update images

    Quote Originally Posted by mDennis10 View Post
    wait, so how do i paint it without using the paintComponent method in the first place
    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.

  8. #8
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

Similar Threads

  1. Replies: 0
    Last Post: 12-16-2010, 05:14 PM
  2. Replies: 4
    Last Post: 12-16-2010, 04:32 PM
  3. Update the JFrame after change the Content Pane
    By alisonchan30 in forum AWT / Swing
    Replies: 1
    Last Post: 04-26-2010, 06:22 AM
  4. Replies: 0
    Last Post: 02-21-2010, 11:51 AM
  5. Replies: 3
    Last Post: 11-10-2009, 10:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •