How to change Images with ImageIcon

Hello everyone, this is my first post on the forums.
I am struggling at the moment to find a way to change images in my program. I am making a card game and when the user clicks a button it will replace the card he has with another card. I thought the following code would work but apparently it doesnt:
String cardPathA = "image1.gif";
ImageIcon imageCardA = new ImageIcon(cardPathA);   //cardPathA for example is image1.gif
//during the program the user changes cards so it should now show image2.gif
cardPathA = "image2.gif";
imageCardA.setImage(cardPathA);The error occurs when compiling and it reads: "+setImage(java.awt.Image) in javax.swing.ImageIcon cannot be applied to (java.langlString)+".
I have searched all over the internet and I haven't found anything. I would appreciate some help. Thanks :)

im not really sure i made a quick example to show how I would change the imageIcon in a button, just change the paths for cardPathA and cardPathB for your pictures to get it working.
The only other thing i can think of is maybe to call repaint() for the components you are trying to change the image?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class ImageChange extends JFrame implements ActionListener{
         final private String cardPathA;
        final private String cardPathB;
     private ImageIcon myImageIcon;
        private JButton changeCard;
        private int counter;
        private JPanel panel;
        private JButton imageButton;
     public ImageChange(){       
            changeCard = new JButton("Change Button");
            counter = 0;
            cardPathA = "1.gif";
            cardPathB = "2.gif";
            myImageIcon = new ImageIcon(cardPathA);
            imageButton = new JButton(myImageIcon);
        public void makeGui(){
            changeCard.addActionListener(this);
            panel = new JPanel(new BorderLayout());
            panel.add(changeCard,BorderLayout.NORTH);
            panel.add(imageButton,BorderLayout.CENTER);
            this.add(panel);
            this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
            this.pack();
            this.setVisible(true);
     public void actionPerformed (ActionEvent menuChoice){
            counter++;
            if(counter%2 ==0){             
                myImageIcon = new ImageIcon(cardPathA);
                imageButton.setIcon(myImageIcon);
            }else{
                myImageIcon = new ImageIcon(cardPathB);
                imageButton.setIcon(myImageIcon);
        public static void main(String[] args){
            ImageChange test = new ImageChange();
            test.makeGui();
}Calypso

Similar Messages

  • How to change image display of my podcast page in iTunes?

    How to change image display of my podcast page in iTunes?

    As far as I understand you need to update the image's link in your RSS XML file.  Be sure to change the link file name even if it links to a different image.
    The XML files associated with podcasts are checked for changes every 24 hours so don't expect this to happen instantly.

  • How to change images based on action.Even clicking changed images should ?

    how to change images based on action.
    Even clicking changed images should do respective actions.
    and while displaying only one image at a time sholud get displayed.
    I am using three images for a single column of a table in Jsp.
    Any clues.
    Any link for any good javascript html jsp site where i can find some good solution.
    vijendra

    In broad way if i say i have multiple situations in mutiple tables where i need such a logic to operate in for all tables.
    lets say in first time load of page by default one image will be displayed (one with sorted order for first column rest all non sorted)Now after every click to any image respective sort should get called.along with all images should get changed.Here with every function one/two images will get effect at least.
    As in first if i say sort by desc then this image will change rest all will remain in unsort form.
    when i move to other column and click on new image the previos will convert to unsort and new one will convert to sort by ascending. rest all will be in unsort form as it is.
    Now it seems like same logic is required with two three conditions.
    even for everyaction different parametrs has to be passed at differnt situations.
    Any good idea if someone can suggest.
    I will be very greatful for him.
    thanks
    vijendra

  • How to change images dynamically in jsp?

    Hi All,
    i Want to know how to change images dynamically in jsp, for example in any site if u look at advertise or any images, it will be changed after every few seconds.
    Please guide me
    Thanks
    Sandesh S

    sandeshas wrote:
    Hi BlusC,
    i was searching your mail Id to ask the question directly to u,i checked in your blog also, but i didnt get,I want to keep my email spam safe. Only trusted people and direct friends have it. Just use the forums for discussion.
    my question is suppose if i want to display and change the advertise(adds from different company ) in my web site,how can i do it by using jsp?
    Do i need to create animated gifs which contains all company adds??one might be from movie add,after few seconds next one migth be some other web site add like that, how can i achieve this in my servet,jsp or using struts.You create animated gifs with an [animated gif editor|http://google.com/search?q=animated+gif+editor]. There is nothing what JSP can do for you here.

  • Capture image with webcam: how to change image orientation

    Hi all,
    I have taken the code shown in thread http://forum.java.sun.com/thread.jspa?threadID=570463&tstart=0, and it works ok (many thanks for it).
    However, all webcam resolutions are in "landscape" mode (e.g. 320 x 240), but I would like to capture a "portrait" image (e.g. in 240 x 320).
    Do you know any easy way to do it (but not physically tilting the camera, so that the video in the screen is shown in its correct orientation)?
    Best regards.

    Hi Owen,
    in my current application I don't need right now to use any large image, so that I have decided to implement a "simple" solution as the first one that you appointed: to use a transparent rectangle above the image, and then crop the saved image withing the limits of this rectangle.
    In order to do it, I have modified your code so that the panel (visualContainer) where to show the image is using an OverlayLayout format:
    <address>visualContainer = new JPanel();</address>
    <address>OverlayLayout overlay = new OverlayLayout(visualContainer);</address>
    <address>visualContainer.setLayout(overlay);</address>
    I have added one class to create the transparent rectangle, using a code like this:
    class TopPanel extends JPanel
    Point loc;
    int width, height;
    public TopPanel()
    setOpaque(false);
    width = 50;
    height = 50;
    protected void paintComponent(Graphics g)
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D)g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
    RenderingHints.VALUE_ANTIALIAS_ON);
    if(loc == null)
    init();
    g2.setPaint(Color.blue);
    g2.drawRect(loc.x, loc.y, width, height);
    private void init()
    int w = getWidth();
    int h = getHeight();
    loc = new Point();
    loc.x = (w - width)/2;
    loc.y = (h - height)/2;
    In order to show the rectangle, what I have done is to add a TopPanel() to visualContainer just before adding the Player visualComponent.
    However, by doint in this way, the rectangle is not shown, but only the webcam image. I don't know if I'm doing something wrong, so any help on this issue is welcome.
    Best regards,
    izk

  • How to Change Images in fla file

    I have Macromedia Flash MX 2004.  I need to change the images in an fla file for inclusion in a new Web site.  I have inherited an fla file and am just learning the in-and-outs of using Flash.  I have been trying to figure out/learn what to do.  But I am now under a deadline.
    How to I replace the old images with new ones?   Thanks, Betsy

    As you have noticed on the timeline, there are 4 keyframes (black dots connected by a long arrow to the next dot) for each of the image tweens on different layers. Just select the 1st or 2nd keyframe of each layer, you will notice that it will select the image (blue rectangle around it), then double click it to get into the MovieClip's content (the bitmap image). You will see the MovieClip's timeline which is only one frame. Select and delete the Bitmap then drag the new Bitmap into it. Then position it and double click outside the stage to return to the root timeline. Do this for each of the layers and you are done. It is really that simple

  • Changing images with c#

    Hi,
    I am making a slotmachine. I have everything but only need an animation for changing the images. This is what i got(only the code for the animation). I can change 2 images but don't know how to do it with multiple images like 5.
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    namespace animatie
        public partial class Form1 : Form
            public Form1()
                InitializeComponent();
                Timer t = new Timer();
                t.Interval = 100;
                t.Tick += new EventHandler(t_Tick);
                t.Start();
            bool one;
            void t_Tick(object sender, EventArgs e)
                Image img;
                if (one) img = imageList1.Images[0];  
                else img = imageList1.Images[1];                      
                one = !one;
                pictureBox1.Image = img;

    Since it seems like you have it working for 2 images then expanding it to any # of images is straightforward. It looks like you're trying to animate the spinning wheel on the slot machine so that is the basis I'm working against.  I'm also assuming
    you want to stick with the image list but there are other approaches as well.
    Firstly make sure your images are all in the image list. Next I would recommend that you move the timer start logic out of your constructor.  You don't want it running when the form is created because it hasn't even been displayed yet.  If you
    want the spinning to start as soon as the form is displayed then override the OnLoad and start the timer at that point.  I would recommend that you move this functionality into a separate method because you'll probably want to be able to spin the wheels
    by clicking a button as well. 
    Since you're using an image list you'll need to keep track of the "current" image.  To do that a simple image index field should be fine.  Each time the timer fires you increment the index by 1.  If it gets to the end of the list
    (>= count) then reset it to 0.  Then set the PictureBox image to the image at that index.  This gives you the wrapping logic you want.  It won't give you a smooth transition image though, each image will basically just pop up. 
    If you want a smooth transition then you might need to look at using a single image with images on it and the timer is simply having the window viewport move "down" the image in a scrolling fashion.  When it gets to the bottom it loops back
    around.  For that kind of implementation a PictureBox is probably not a good idea, you'll want to use a simple custom control that handles its own painting instead.
    Since you'll need to do this 3 times (one for each window) you might consider moving the logic into a custom control that represents a single window, the timer logic, image selection and image list.  You can then use 3 instances on your form to emulate
    the slot machine.  Note that you'll want to add some randomness to the spinning because if each window spins at the same speed you'll always get the same behavior.  Therefore you should probably have each window randomly decide how long and how fast
    to spin to introduce randomness to the spinning.
    Michael Taylor
    http://blogs.msmvps.com/p3net

  • How to  change Image in Portal logon page

    Hi
    we installed the portal EP6SP16.can anybody tell how to change the Standard image with my Company's logo.can any provide the document how to do that.please help
    Regards
    Prasad

    Hi
    Goto System Administration => System Configuration => UM Configuration => Direct Editing.
    In the set of properties listed there, change the value of ume.logon.branding_image to the location of your new image. Don forget to restart the portal server after making changes.
    Place the image in the layout folder of your project which is nothing but the modification of the logon par file (com.sap.portal.runtime.logon.par.bak).
    Regards
    Yoga

  • How to change image position in PanelStrecthLayout  using css?

    Hi All,
    I am using JDeveloper 11.1.1.6.
    My Scenario is I need to show the image front of the Panel Stretch Layout .I tried to set like Style Class and change the Z-index but it's not working ,
    My Design Like :
    <af:panelStretchLayout id="psl3" inlineStyle="background-color:red" styleClass="ImageStyle">
                          <f:facet name="center">
                            <af:panelStretchLayout id="psl2" topHeight="40px"
                                                   inlineStyle="margin:20px;">
                              <f:facet name="center">
                                <af:panelGroupLayout id="pgl3" layout="vertical"
                                                     inlineStyle="background-color: Green">
                                  <af:panelGroupLayout layout="vertical" id="pgl4"   inlineStyle="margin-top:10px;margin-left:10px;background-color:Green">          
                                  </af:panelGroupLayout>
                                </af:panelGroupLayout>
                              </f:facet>
                            </af:panelStretchLayout>
                          </f:facet>
                        </af:panelStretchLayout>
    My CSS Codes :
    .ImageStyle{
    background-image: url("../img/sampleimage150x100.png");
    z-index:9999;
    background-repeat: no-repeat;
    How to show image in-front of the layout not it's hiding behind the layout ...
    Thanks..

    Hi,
    normally you would use skinning to skin the components. I don't think that layers make a different because with the stretch layout there are no overlapping layers. So maybe we should start with the use case you are trying to implement, I am not sure I understand the sentence: "I need to show the image front of the Panel Stretch Layout". What does this mean "in front" ? Do you want it to hide the panel stretch layout ? What is the use case?
    Frank

  • How to change hotkeys with an azerty keyboard ?

    Hello,
    I'm having troubles with changing hotkeys : I have an AZERTY keyboard, and the default configuration for hotkeys are written for QWERTY keyboards. And if I I want to change a hotkey, it considers I have a QWERTY keyboard, so I can't change some of the hotkeys.
    Typically, if I type "," (the default hotkey for one step back), it steps back to the frame before. OK.
    But if I want to enter "," for "one step back" in the "change hotkey" feature, it enters "M", although it is already set to ",".  Impossible to change it to ",". NOT OK.
    The problem is that for "one step forward", it is a bullet point, and it doesn't work on AZERTY devices, because it is behind a SHIFT key ! So I need to change this.
    Once again, it worked in the earlier version of Flash.....
    Do you have an idea of how to change this ?
    Thanks (I searched and didn't find anything, but I'm sure I'm not the first one).

    Ok, I really needed this to work so I searched for the disposition of a QWERTY keyboard, and entered the key I wanted for "one step forward" : the "M" in AZERTY is the ";" for QWERTY. Exactly what I wanted...
    But this is a pretty annoying bug !
    Anyway, all the bugs are annoying.... Especially when all that worked before....

  • How show ONLY images with problems or needing metadata update, etc?

    In LR3, how do I show JUST the images that are waiting for a metadata update on disk?
    Or, more importantly, how do I show images that have a problem - the little box on the top right of the thumbnail with the exclamation point.
    If I have a dozen images out of 50,000 that have an error with "another process has updated the metadata" I want to see JUST THOSE IMAGES
    so I can fix the problem...
    How do I do this?

    Dave, there are two related issues:
    - LR appears to have a number of problems correctly identifying which images have metadata conflicts between the catalog and the files proper:
    http://feedback.photoshop.com/photoshop_family/topics/lr_3_5rc_reproducible_spurious_metad ata_file_needs_to_be_updated
    - LR has no mechanism that allows you to identify just those images with metadata conflicts:
    http://feedback.photoshop.com/photoshop_family/topics/metadata_collisions_between_catalogu e_and_files
    Please visit these feedback topics and add your vote and opinion.  Adobe pays closer attention to the feedback forum than this one.

  • How to change password with commadmin command?

    I am using JES 2004Q2, Directory shema2.
    HOw to change password for email account ?
    Email account created in identity server with commadmin comand.
    I can't find this information in :
    "Sun Java System Communications Services 6 2004Q2 User Management Utility Administration Guide"

    http://docs.sun.com/source/817-5703/commcli_ref.html#wp1021656
    commadmin doesn't seem to offer a password modify/replace option.
    You can use the Directory Console to replace a password, or ldapmodify, or webmail/comms express.

  • How to Mask Image with different Shape

    Hi all,
    In my flex application , i am displaying a image which is in
    rectangle shape. but i want to show that image in a rounded
    rectangle shape, for that i want to mask the image with rounded
    rectangle, .
    anyone please tell me how to do this,
    i also tried by setting the mask property in the image, but i
    am not getting.
    please help me.
    thanks in advance
    regards
    avanthika

    Hi all
    i am trying in this way still i did not get it
    i have placed image in a canvas.
    <mx:Canvas id="c1" x="10" y="10" width="200"
    height="200">
    <mx:Image id="img2" source="image1.jpg" />
    </mx:Canvas>
    to mask the above image , i need shape to mask i have written
    following code it is not working
    import flash.display.*;
    public function init(){
    var square:Sprite = new Sprite();
    square.graphics.beginFill(0xFF0000);
    square.graphics.drawRect(0, 0, 200, 200);
    c1.addChild(square);
    img2.mask=square;
    please help me how to get this,
    Actually i don't know how to write code for drawing shapes in
    flex.
    Thanks in advance
    Regards
    Avanthika

  • How to change image in jsp in a web application

    I had created a web application in which I have given the option to change the image in a jsp, and I am saving that image in a folder.
    The image is saved during the run time.But I am not getting that changed image.
    If I restart my server the image is retrieved . What is the solution for getting the changed image during the runtime itself?

    use javascript
    example:
    <img id='img' src="Img1.gif" border=0
         onMouseover="document.getElementById('img').src='Img2.gif'"
         onMouseout="document.getElementById('img').src='Img1.gif'">

  • Change image with radio group

    I hope this is a simple one for someone out there.
    I need to swap images with a radio group.
    I'll post the code here so you have what I have for now.
    Obviously I haven't gotten anything working.
    Image names to be:
    Case_green.png
    Case_blue.png
    Case_blacklight.png
    Case_red.png
    <!--START RADIO BUTTONS-->
    <!--START RADIO BUTTON SCRIPT-->
    <script type="text/javascript">
    </script>
    <!--END RADIO BUTTON SCRIPT-->
    <form name="Colors" method="post" action="">
    <table width="358" align="left">
    <tr>
    <td width="70"><label>
    <input name="ColorRadios" type="radio" id="ColorRadios_0"
    value="radio_green" checked>
    Green</label></td>
    <td width="57"><label>
    <input name="ColorRadios" type="radio" id="ColorRadios_1"
    value="radio_blue">
    Blue</label></td>
    <td width="105"><label>
    <input type="radio" name="ColorRadios"
    value="radio_blacklight" id="ColorRadios_2">
    Black Light</label></td>
    <td width="54"><label>
    <input type="radio" name="ColorRadios" value="radio_red"
    id="ColorRadios_3">
    Red</label></td>
    </tr>
    </table>
    </form>
    <p align="left">
    <!--END RADIO BUTTONS-->

    Mick,
    Thanks
    William - sorry about the missing quote.
    Paul Davis
    http://www.kaosweaver.com/
    Visit us for dozens of useful Dreamweaver Extensions.
    http://www.communitymx.com/
    Partner at Community MX - Extend your knowledge
    Mick White wrote:
    > Paul Davis wrote:
    >> this would be something like:
    >> <input name="ColorRadios" type="radio"
    id="ColorRadios_1"
    >> value="radio_blue"
    >>
    onclick="document.getElementById('swapimageid').src='Case_blue.jpg'>
    >
    > onclick=
    > "document.getElementById('swapimageid').src=
    > 'Case_'+this.value.split('_')[1]+'.jpg'"
    >
    > Mick
    > Note: You forgot closing quotes.
    >>
    >> The image that is getting swapped will need a unique
    ID, but that
    >> should do it.
    >>
    >>
    >> Paul Davis
    >>
    http://www.kaosweaver.com/
    >> Visit us for dozens of useful Dreamweaver
    Extensions.
    >>
    >>
    http://www.communitymx.com/
    >> Partner at Community MX - Extend your knowledge
    >>
    >> William Rickert wrote:
    >>> I hope this is a simple one for someone out
    there.
    >>>
    >>> I need to swap images with a radio group.
    >>>
    >>> I'll post the code here so you have what I have
    for now. Obviously I
    >>> haven't gotten anything working.
    >>>
    >>> Image names to be:
    >>> Case_green.png
    >>> Case_blue.png
    >>> Case_blacklight.png
    >>> Case_red.png
    >>>
    >>>
    >>> <!--START RADIO BUTTONS-->
    >>> <!--START RADIO BUTTON SCRIPT-->
    >>> <script type="text/javascript">
    >>>
    >>>
    >>> </script>
    >>> <!--END RADIO BUTTON SCRIPT-->
    >>>
    >>> <form name="Colors" method="post"
    action="">
    >>> <table width="358" align="left">
    >>> <tr>
    >>> <td width="70"><label>
    >>> <input name="ColorRadios" type="radio"
    id="ColorRadios_0"
    >>> value="radio_green" checked>
    >>> Green</label></td>
    >>> <td width="57"><label>
    >>> <input name="ColorRadios" type="radio"
    id="ColorRadios_1"
    >>> value="radio_blue">
    >>> Blue</label></td>
    >>> <td width="105"><label>
    >>> <input type="radio" name="ColorRadios"
    >>> value="radio_blacklight" id="ColorRadios_2">
    >>> Black Light</label></td>
    >>> <td width="54"><label>
    >>> <input type="radio" name="ColorRadios"
    value="radio_red"
    >>> id="ColorRadios_3">
    >>> Red</label></td>
    >>> </tr>
    >>> </table>
    >>> </form>
    >>> <p align="left">
    >>> <!--END RADIO BUTTONS-->
    >>>

Maybe you are looking for