Results 1 to 7 of 7

Thread: Updating my GUI

  1. #1
    Catkill is offline Member
    Join Date
    Aug 2009
    Posts
    8
    Rep Power
    0

    Default Updating my GUI

    I have a GUI ready set up and added to a frame using the setContentframe(a JPanel) function

    When a user clicks a button i want one of the components(well my class extending component) to change its image - its image is changed via a setImage(Image) function.

    Although when i set its new image the GUI doesnt update to the new image - how would i go about this?

    Oh yeah the components are stored in an array and im just using an component[i].setImage(imageload.getimage()); - could be important

    thanks in advance
    Catkill

  2. #2
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    5

    Default

    How do u draw this image? is it in a custom paintComponent(...)? Do you call repaint() when you call setImage?
    My Hobby Project: LegacyClone

  3. #3
    Catkill is offline Member
    Join Date
    Aug 2009
    Posts
    8
    Rep Power
    0

    Default

    heres the full class for the Component

    Java Code:
    package Version1;
    
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Image;
    
    import javax.swing.JButton;
    import javax.swing.JComponent;
    import javax.swing.JPanel;
    
    public class PanelBorder extends JPanel{
    	
    	private final int cardHeight = 40;
    	private final int cardWidth = 30;
    	
    	private Image card = null;
    	
    	public void setImage(Image image){
    		this.card = image;
    		draw();
    	}
    	
    	public PanelBorder(){
    		this.setPreferredSize(new Dimension(cardWidth + 5,cardHeight+10));
    	}
    	
    	public PanelBorder(Image image){
    		this.card = image;
    		this.setPreferredSize(new Dimension(cardWidth,cardHeight));
    	}
    	
    	public Image getImage(){
    		return card;
    	}
    	
    	public void draw(){
    		repaint();
    	}
    	
    	public void paintComponent(Graphics g){
    		super.paintComponent(g);
    		
    		if(card == null){
    			g.setColor(Color.black);
    			g.drawRect(0, 0, cardWidth, cardHeight);
    		}
    		if(card != null){
    			g.drawImage(getImage(),0,0,null);
    		}
    		
    		
    		
    	}
    	
    }
    thanks

  4. #4
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    5

    Default

    Next question... is the image null? i.e. is it drawing the black rectangle?
    My Hobby Project: LegacyClone

  5. #5
    Catkill is offline Member
    Join Date
    Aug 2009
    Posts
    8
    Rep Power
    0

    Default

    It draws the black rectangle when i create the component and add its to its JPanel inside a main JPanel inside a frame, as it should, but when i give it an image it still draws the rectangle - or doesnt change from it?

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    You neglected to show how you are loading the image. That may be a clue to the problem.

    db

  7. #7
    Catkill is offline Member
    Join Date
    Aug 2009
    Posts
    8
    Rep Power
    0

    Default

    I am using my imageLoader class :

    Java Code:
    package Version1;
    
    import java.awt.GraphicsConfiguration;
    import java.awt.GraphicsEnvironment;
    import java.awt.Image;
    import java.awt.Rectangle;
    import java.awt.Transparency;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import java.util.HashMap;
    
    import javax.imageio.ImageIO;
    
    public class ImageLoader {
    	
    	private static ImageLoader single = new ImageLoader();
    	
    	public static ImageLoader get(){
    		return single;
    	}
    	
    	private HashMap<String,Image> Images = new HashMap<String,Image>();
    	//private HashMap<String,CardHolder> Images = new HashMap<String,CardHolder>();
    	
    	public Image getCard(String ref,String i){
    		
    		if(Images.get(ref + ".jpg" + i) != null){
    			System.out.println("Image found");
    			return  Images.get(ref);	
    		}
    		
    		System.out.println("Cannot find the image");
    		return null;
    	}
    	
    	public Image getCard(String ref,int x,int y,int x1,int x2,int y1,int y2,int loop){
    		Rectangle region = new Rectangle(x,y);
    		
    		if(Images.get(ref) != null){
    			return Images.get(ref);
    		}
    		
    		BufferedImage sourceImage = null;
    		
    		try{
    			sourceImage = ImageIO.read(new File(ref));
    		}
    		catch(IOException e){
    			System.out.print("Unable to Load: " + ref);
    		}
    		
    		GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
    		Image image = gc.createCompatibleImage(sourceImage.getWidth(),sourceImage.getHeight(),Transparency.BITMASK);
    		
    		image.getGraphics().drawImage(sourceImage, region.x, region.y, region.x + region.width, region.y + region.height, x1, y1, x2, y2, null);
    		
    		
    		Images.put(ref + (loop+2), image);
    		System.out.println(ref + (loop+2));
    		
    		return image;
    	}
    	
    	
    
    }
    Hope this helps more

Similar Threads

  1. Updating a JProgressBar from another thread?
    By Xiphias in forum AWT / Swing
    Replies: 3
    Last Post: 03-18-2009, 01:48 AM
  2. Prob with updating in jdbc
    By venkatteshb in forum New To Java
    Replies: 4
    Last Post: 08-21-2008, 06:08 PM
  3. Replies: 0
    Last Post: 07-11-2008, 04:52 PM
  4. Updating Graphics
    By Greedful in forum Java 2D
    Replies: 2
    Last Post: 07-20-2007, 07:12 PM
  5. Updating into 2 tables in the DB
    By yuchuang in forum New To Java
    Replies: 2
    Last Post: 05-12-2007, 06:54 AM

Posting Permissions

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