Results 1 to 5 of 5
  1. #1
    Coold0wn is offline Member
    Join Date
    Aug 2011
    Posts
    71
    Rep Power
    0

    Default changing image of a label

    hi guys,
    in my program i have to change the ImageIcon in a label by clicking on the screen.
    it works fine, but takes a long time cause i do it like this:
    Class Editor:
    Java Code:
    public void mouseClicked(MouseEvent event)
         {
            layer.tileSetzen(20, 1);         //sets the 20th label in the arraylist labels of the class layer to the image number 1
            DrawFrame.setVisible(false);
            DrawFrame.setVisible(true);  // this is what i want to get rid of.
            }
    Class layer:
    Java Code:
        public void tileSetzen(int tilenumber, int id)
        {
            tiles.get(tilenumber).setImageNummer(id);   //ImageNummer is the image that the label shows (20 is another image than 5 for example)
            redrawImage(tilenumber);
        }
    Java Code:
        public void redrawImage(int nr)
        {
            MainFrame.getContentPane().remove(labels.get(nr)); //remove the label from the container
            labels.remove(nr);            // remove the label from the arraylist
            imageIcons.remove(nr);   // remove the image from the arraylist with the imageIcons
    
            imageIcons.add(nr, tileset[tiles.get(nr).gibImageNummer()]);  //add the new image at the place where the other one was deleted
            labels.add(nr, new JLabel(imageIcons.get(nr))); //add a new label at the place where the label was deleted
            labels.get(nr).setBounds(32*(nr % x), 16*(nr-( nr % x))/y, 32, 32); // sets the new label at the right position
            MainFrame.getContentPane().add(labels.get(nr)); // adds the new label to the container
        }
    sum up:
    - i remove the label from the container labels
    - i remove the label from the arraylist imageIcons
    - i remove the imageIcon from the arraylist

    - i add a new imageIcon to the arraylist imageIcons (at the same place as the other one was)
    - i add a new label to the arraylist labels (at the same place as the other one was)
    - i setBounds of the new label
    - i add the label to the container

    - i set the MainFrame to unvisible
    - i set the MainFrame to visible
    Last edited by Coold0wn; 08-31-2011 at 02:08 AM.

  2. #2
    Coold0wn is offline Member
    Join Date
    Aug 2011
    Posts
    71
    Rep Power
    0

    Default

    okay... got a way better solution now...
    is it normal that i cant change many icons fast after each other?
    i always have to wait .. like a second befor i can change the next
    Java Code:
         public void mouseClicked(MouseEvent event)
         {
            int x = event.getX()-8,y = event.getY();
            int tilenr = (x-(x % 32))/32 + 40*((y-(y % 32))/32) - 40;
            layer.get(pickedLayerNummer).tileSetzen(tilenr , pickedTile);
         }
    Java Code:
        public void tileSetzen(int TileNummer, int id)
        {
            tiles.get(TileNummer).setImageNummer(id);        
            labels.get(TileNummer).setIcon(tileset[tiles.get(TileNummer).gibImageNummer()]);
        }
    this takes about 1 sekond?
    Last edited by Coold0wn; 08-31-2011 at 02:38 AM.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Coold0wn View Post
    i always have to wait .. like a second befor i can change the next
    Unless you are not working on with multiple threads. Since you have a single method UI updates and other stuff cannot handle at the same time.

  4. #4
    Coold0wn is offline Member
    Join Date
    Aug 2011
    Posts
    71
    Rep Power
    0

    Default

    hey, thanks for your help.
    i just found out what the problem was. :)

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Display image via label
    By mike_ledis in forum Java Applets
    Replies: 1
    Last Post: 06-02-2011, 06:28 PM
  2. Changing Image color
    By Frecow in forum Java 2D
    Replies: 0
    Last Post: 04-04-2011, 10:16 AM
  3. POI changing text to a form label
    By natt in forum Advanced Java
    Replies: 2
    Last Post: 09-19-2008, 10:49 AM
  4. Creating a label with an image
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-11-2008, 04:40 PM
  5. Label image Question
    By Soda in forum New To Java
    Replies: 3
    Last Post: 12-10-2007, 03:38 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
  •