Results 1 to 7 of 7
Thread: Updating my GUI
- 08-31-2009, 06:17 PM #1
Member
- Join Date
- Aug 2009
- Posts
- 8
- Rep Power
- 0
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
- 08-31-2009, 06:44 PM #2
How do u draw this image? is it in a custom paintComponent(...)? Do you call repaint() when you call setImage?
My Hobby Project: LegacyClone
- 08-31-2009, 06:56 PM #3
Member
- Join Date
- Aug 2009
- Posts
- 8
- Rep Power
- 0
heres the full class for the Component
thanksJava 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); } } }
- 08-31-2009, 07:04 PM #4
Next question... is the image null? i.e. is it drawing the black rectangle?
My Hobby Project: LegacyClone
- 08-31-2009, 07:16 PM #5
Member
- Join Date
- Aug 2009
- Posts
- 8
- Rep Power
- 0
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?
- 08-31-2009, 10:02 PM #6
You neglected to show how you are loading the image. That may be a clue to the problem.
db
- 09-01-2009, 05:09 PM #7
Member
- Join Date
- Aug 2009
- Posts
- 8
- Rep Power
- 0
I am using my imageLoader class :
Hope this helps moreJava 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; } }
Similar Threads
-
Updating a JProgressBar from another thread?
By Xiphias in forum AWT / SwingReplies: 3Last Post: 03-18-2009, 01:48 AM -
Prob with updating in jdbc
By venkatteshb in forum New To JavaReplies: 4Last Post: 08-21-2008, 06:08 PM -
Updating a progress bar from the UI thread
By Java Tip in forum SWTReplies: 0Last Post: 07-11-2008, 04:52 PM -
Updating Graphics
By Greedful in forum Java 2DReplies: 2Last Post: 07-20-2007, 07:12 PM -
Updating into 2 tables in the DB
By yuchuang in forum New To JavaReplies: 2Last Post: 05-12-2007, 06:54 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks