Results 1 to 13 of 13
Like Tree2Likes
  • 2 Post By Norm

Thread: My Run Method Does Not Work Witn Changing JLabel Images From The Other Class?

  1. #1
    Alerhau's Avatar
    Alerhau is offline Senior Member
    Join Date
    Jun 2011
    Location
    Sweden
    Posts
    158
    Rep Power
    3

    Default My Run Method Does Not Work Witn Changing JLabel Images From The Other Class?

    I guess the title says most of it. What I am trying to do is changing a JLabels icon to another using only another classes public stuff. (Both images, JLabels). In order to do this I am using this piece of code (Note, this is in a class which implements Runnable, so this is into the run(); method):

    Java Code:
    Thread.sleep(1000);
    
    Engine Eng = new Engine();
    
    Eng.Grid[1][1].setIcon(Eng.Sprite[0])
    Why is it not working? Any soloution, please help!
    Last edited by Alerhau; 08-14-2011 at 10:56 PM.

  2. #2
    Alerhau's Avatar
    Alerhau is offline Senior Member
    Join Date
    Jun 2011
    Location
    Sweden
    Posts
    158
    Rep Power
    3

    Default

    I really need quick help.
    Currently Developing a Game Called Loaie, You Can Download It From "The Loaie Website".

  3. #3
    kammce's Avatar
    kammce is offline Senior Member
    Join Date
    Dec 2010
    Location
    California
    Posts
    194
    Rep Power
    4

    Default

    What are your errors? I am guessing that the Engine class is one of your own design, no?
    My API:
    Java Code:
    cat > a.out || cat > main.class

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default

    Are you using the GUI's thread to do it? Or on your own thread? Have you told the GUI it has new stuff to show?

    You are creating a new Engine object. Is there an old one still around that you are looking at expecting it to be changed vs the new one where the changes are being made.

    Perhaps a small program (SSCCE) that shows the problem

  5. #5
    Alerhau's Avatar
    Alerhau is offline Senior Member
    Join Date
    Jun 2011
    Location
    Sweden
    Posts
    158
    Rep Power
    3

    Default

    What are your errors? I am guessing that the Engine class is one of your own design, no?
    No errors at all. It goes through the run method just fine, I've tested the run method, which loops at the same time as I move my character over the giant grid. In the end of the loop I have a JOptionPane which tells me each time it's finished & that works. So then I know that it is going through the code I have in my first post, but not using it. Well I don't know if it's using it. But I know that the JOptionPane pops up.

    Are you using the GUI's thread to do it? Or on your own thread? Have you told the GUI it has new stuff to show?
    It's another class, which I call "Run" that is the thread class. Then I just create a thread object for that class & I do "RunThread.start();".

    You are creating a new Engine object. Is there an old one still around that you are looking at expecting it to be changed vs the new one where the changes are being made.
    How do you mean?
    Currently Developing a Game Called Loaie, You Can Download It From "The Loaie Website".

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default

    Is there an old one still around that you are looking at expecting it to be changed vs the new one where the changes are being made.
    How many different places in your code do you create an Engine object?
    Has one been created and is being shown when your posted code is executed which creates a new one.

    Otherwise make a small program (SSCCE) that compiles and executes and shows the problem.

  7. #7
    Alerhau's Avatar
    Alerhau is offline Senior Member
    Join Date
    Jun 2011
    Location
    Sweden
    Posts
    158
    Rep Power
    3

    Default

    How many different places in your code do you create an Engine object?
    Only that place. (The "Place" is in a different class, called Run which has the Runnable implemented)

    Has one been created and is being shown when your posted code is executed which creates a new one.
    What do you mean?
    Currently Developing a Game Called Loaie, You Can Download It From "The Loaie Website".

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default

    Can you explain what "is not working" means?

    What should the result of the setIcon method call be?


    Java Code:
    Eng.Grid[1][1].setIcon(Eng.Sprite[0])
    Why do you do this outside of the Eng class?
    Why isn't it done in the Eng class's constructor?
    Java Code:
    Grid[1][1].setIcon(Sprite[0]);

  9. #9
    Alerhau's Avatar
    Alerhau is offline Senior Member
    Join Date
    Jun 2011
    Location
    Sweden
    Posts
    158
    Rep Power
    3

    Default

    I want this to work because I need Java to change one icon each second that goes, I also need the movement of my game to work at the same time.

    So that I can both move my character around on the Grid & change icons every at the same time.

    If I just call the Thread.sleep() into the main class (Engine) Everything in the class will sleep.

    If there any way to do it into my main class with just making some stuff to sleep at the side of my movement?
    Currently Developing a Game Called Loaie, You Can Download It From "The Loaie Website".

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default

    Use a Timer to do something every time period.

    It is almost impossible to suggest what to change in your code after seeing 3 lines of code.

    If you could make a SSCCE to demonstrate the problem we could make better suggestions.
    DarrylBurke and Alerhau like this.

  11. #11
    Alerhau's Avatar
    Alerhau is offline Senior Member
    Join Date
    Jun 2011
    Location
    Sweden
    Posts
    158
    Rep Power
    3

    Default

    How would I make the Timer? Does the timer update, do something every time period? Like every 10 seconds it changes a JLabel icon?
    Currently Developing a Game Called Loaie, You Can Download It From "The Loaie Website".

  12. #12
    Alerhau's Avatar
    Alerhau is offline Senior Member
    Join Date
    Jun 2011
    Location
    Sweden
    Posts
    158
    Rep Power
    3

    Default

    Got it to work! Thanks!
    Currently Developing a Game Called Loaie, You Can Download It From "The Loaie Website".

  13. #13
    Alerhau's Avatar
    Alerhau is offline Senior Member
    Join Date
    Jun 2011
    Location
    Sweden
    Posts
    158
    Rep Power
    3

    Default

    Thanks for all help, the timer worked awsomely amazing!
    Currently Developing a Game Called Loaie, You Can Download It From "The Loaie Website".

Similar Threads

  1. Modifying JLabel within separate class
    By the_rider9 in forum New To Java
    Replies: 6
    Last Post: 07-06-2011, 06:07 PM
  2. Updating a JLabel from another class.
    By Fumomo in forum AWT / Swing
    Replies: 6
    Last Post: 05-04-2011, 04:31 PM
  3. Replies: 5
    Last Post: 12-04-2010, 12:30 AM
  4. How to resize images to fit JLabel?
    By Krooger in forum New To Java
    Replies: 3
    Last Post: 11-24-2010, 01:22 PM
  5. Changing images by clicking arrow buttons. help?
    By ashton in forum New To Java
    Replies: 3
    Last Post: 02-08-2009, 11:29 AM

Tags for this Thread

Posting Permissions

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