Results 1 to 10 of 10

Thread: Loading Images

  1. #1
    TacoManStan is offline Member
    Join Date
    Sep 2011
    Posts
    50
    Rep Power
    0

    Default Loading Images

    Hey guys,

    So basically, I am making a pretty basic 2D RPG game. I am discovering that my code is incredibly inefficient. I was wondering, (I have sort of asked this before in other threads), how should this be done?

    Right now I have what is essentially a JComponent loaded at every tile in the current "room". That would be... 62,500 JComponents at once, each with an image loaded to it.

    So clearly this is not the way to go. I am using a JPanel, not a canvas. I was told originally on some forum to stay away from anything in the AWT package, but I am quickly finding that that statement is false... So should I be drawing to a canvas instead? What is the difference?

    Also, I had another idea. I was going to add a bunch of images together onto one giant JComponent to form the "texture" of the terrain. Then I would add in the trees and such to the same JComponent, then I would add in the units. Then I would simply create my own classes which would hold the least amount of data possible to animate and control all of the images. Also, I was going to only load what is currently visible, to save memory.

    Would that be an effective method? If you don't feel like answering anything else, please just respond to the last question. That is the most important one.

    Also, I would have a thread on each object for the image swapping, moving the images on the screen, and then one for controlling the actual commands. Each would be added to a thread pool, for a total of 3 thread pools. Would that be acceptable?

    Thanks again.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default Re: Loading Images

    I was told originally on some forum to stay away from anything in the AWT package, but I am quickly finding that that statement is false...
    The only thing false about it is that you should stay away form AWT Components. Swing is build on top of AWT, so there's no way you can stay away from "anything" in the java.awt package.

    a total of 3 thread pools. Would that be acceptable?
    Generally speaking, no. Or at least, not without understanding Swing's single threaded rule (<-- google that and you'll get two very good articles about it). And go through Lesson: Concurrency in Swing (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    db

  3. #3
    TacoManStan is offline Member
    Join Date
    Sep 2011
    Posts
    50
    Rep Power
    0

    Default Re: Loading Images

    Single threaded rule... So basically, one a Swing object is created, you aren't supposed to edit anything unless it is all in the same thread? So should I be only modifying the BufferedImages drawn on the JPanel?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Loading Images

    Quote Originally Posted by TacoManStan View Post
    Single threaded rule... So basically, one a Swing object is created, you aren't supposed to edit anything unless it is all in the same thread? So should I be only modifying the BufferedImages drawn on the JPanel?
    You shouldn't be making any Swing method calls off of the EDT, the main Swing thread, unless you know for a fact that that call is thread safe. That means that you can fiddle with BufferedImages in a background thread all you'd like, but if you're going to use it in a Swing method call, such as after adding it to an ImageIcon (which I believe can be done off of the EDT), if you add that Icon to a JLabel, you'd better do that specific call on the EDT.

  5. #5
    TacoManStan is offline Member
    Join Date
    Sep 2011
    Posts
    50
    Rep Power
    0

    Default Re: Loading Images

    Ah, so I can do anything off screen that I want, but once I edit the actual layout I have to do that within the main Swing thread? Also, if I want to just move the BufferedImages around, would I also have to do that from the main thread as well? How would I manage to make all the units look like they are, well, separate threads?

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default Re: Loading Images

    In the very first response to your question I gave you a link to a tutorial lesson that explains all that and more. Why should people here be expected to reproduce what's in that lesson?

    Note that the "main' thread and the EDT are separate entities.

    db

  7. #7
    TacoManStan is offline Member
    Join Date
    Sep 2011
    Posts
    50
    Rep Power
    0

    Default Re: Loading Images

    Thank you, sorry I didn't notice that. My bad :(

  8. #8
    TacoManStan is offline Member
    Join Date
    Sep 2011
    Posts
    50
    Rep Power
    0

    Default Re: Loading Images

    Ok so I have looked into this a little bit more, as you suggested. It appears that the best way (and the correct way) of going about Swing threading is to use background threads, which they call SwingWorkers. The have a method doInBackground(), which is what I understand does the actual work. Then they also have methods to, during processing, send data to the EDT, the publish() method I believe. (Sorry I'm saying this, if any of you have forgotten any of this I thought I might as well refresh your memories). Then there is the done() method, which I believe tells the EDT to execute the code. There is also an execute() method, that I believe does the work and then signals done() from inside.

    Ok, so that is my understanding. My understanding is that also, SwingWorkers are sent to a thread pool of a maximum of 10. First question is, are those 10 threads separate SwingWorkers, or can there be 10 instances of the same SwingWorker queued at the same time? If so, is it possible to send more thread pools to the thread pool, so that only one type of SwingWorker can be running at a time? So for example, I can tell the game that moving a unit is more important than updating the animation?

    I'm sorry, I really am a noob at this. I think I understand thread pools also, but I'm not sure... There are 10 threads. Only one of them can be executed at once, and it is chosen at random. When a thread is executed, another one, if present, from the queue is then added at random?

  9. #9
    Join Date
    Aug 2011
    Posts
    2
    Rep Power
    0

    Default Re: Loading Images

    can u give me the code how to read the image from folder to graphics ? ? thanks in advance...

  10. #10
    TacoManStan is offline Member
    Join Date
    Sep 2011
    Posts
    50
    Rep Power
    0

    Default Re: Loading Images

    bump... still could use some help with that last question, if possible.

Similar Threads

  1. Loading files(namely images)
    By jammas615 in forum New To Java
    Replies: 1
    Last Post: 07-11-2011, 11:06 AM
  2. Loading images from an Array
    By pinkette in forum New To Java
    Replies: 5
    Last Post: 04-15-2011, 12:37 PM
  3. Loading images in seperate packages?
    By Bibendum in forum New To Java
    Replies: 0
    Last Post: 03-02-2008, 11:43 PM
  4. Loading Images - Imp
    By Thulasiraman in forum Advanced Java
    Replies: 0
    Last Post: 01-28-2008, 10:33 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
  •