• Post Reply Bookmark Topic Watch Topic
  • New Topic

Import Enemy file

 
Alaric Mustoe
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you animate an imported file ex("enemy1") and import it multiple times.
 
Stevens Miller
Bartender
Posts: 1421
29
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a kind of confusing question, Alaric. Maybe tell us more about what you're trying to do.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12340
38
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by "import"?
What do you mean by "animate"?
What do you mean by "multiple times"?

I'm GUESSING the file is some kind of image. You want the image to move. And you want it to appear many times. But those are all guesses.
 
Alaric Mustoe
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I will be more clear I have an image file designated as a jpg and would like to import the file and show it in a JFrame. But do this multiple times and get them to move right till specified border down one and left till border then down one and loop that.
 
Stevens Miller
Bartender
Posts: 1421
29
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you want the image to move right and left, back and forth between two specified borders, and down one pixel each time the image changes direction from left to right and from right to left, is that it?

Okay, there are a number of ways one can do this, but I want to offer a tip and a question first.

Tip: In Java, the word "import" has a very specific meaning, which has nothing to do with what you have described. So, let's use "read" (or "open," or "load") instead of "import," when you are talking about gaining access to the contents of the image file.

Question: What do you have in mind for controlling the rate of motion of the moving image? If you do this with no control over the rate (what animators call the "frame rate"), you'll probably never see any motion at all, because your Java code will either hold off updating the screen until all the looping is done, or because the looping will be so fast that the animation frame rate will be faster than the refresh rate of your monitor (with the result being that the image will appear to jump from one random place to the next, about 60 times each second).

Most likely, you're going to need some kind of timer class and a separate thread. Updates to your GUI shouldn't be done on threads of their own, so you're also going to need a way for your thread to ask the Event Dispatcher Thread to update your screen for you. None of this is very hard, but you will need to understand some basics about multi-threaded code (which comes naturally to some people, while causing others to weep quietly). Have you ever done any multi-threaded coding?
 
Alaric Mustoe
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well then could you give me an example of how to import and define movement for the image?
 
Stevens Miller
Bartender
Posts: 1421
29
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, this may not be quite the help you're looking for, but tell us if you can get this program to run. First, you need an instance of a class derived from JFrame, in which you are going to enclose an instance of a class derived from JPanel. Here, I'm calling that top-most class "MainFrame," defined like this:

You'll want to replace the path in Line 17 with a path to your own image file.

The class derived from JPanel handles the image display:


That doesn't animate the image, but it does open the image file and put it on the screen. Also, it will handle repainting the image whenever it has been partly or totally hidden, and then revealed again. Give that a try and see if you can at least get a picture on your screen. Until you get that far, we're not ready to discuss animating it.
 
Campbell Ritchie
Marshal
Posts: 52516
118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stevens Miller wrote:. . .. . .
Java8 users would probably write
 
Stevens Miller
Bartender
Posts: 1421
29
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
True, and again at Lines 16-23 of the MyPanel class. I'm guessing Alaric might find lambdas a bit cryptic, for now. (I certainly do .)
 
Campbell Ritchie
Marshal
Posts: 52516
118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hadn't noticed lines 16‑23. Do you want a new Thread in a Swing app like that, or should you use a Timer?
 
Campbell Ritchie
Marshal
Posts: 52516
118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler “knows” that invokeLater takes a Runnable. Runnable is a Functional Interface which means it has one method needing implementation. So the compiler “presumes” that you are implementing its single method. There is no need to write the name of the interface and method because those are all inferred. You write the arguments first. The run() method takes no parameters so you write no arguments inside (). You can miss out the () only if you have one argument not needing a type specified.
Then you write the arrow token -> which seems probably to be pronounced “goes to”.
Then you write the body of the method, and close the () of the method call [invokeLater()].
 
Stevens Miller
Bartender
Posts: 1421
29
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Do you want a new Thread in a Swing app like that, or should you use a Timer?


The invokeLater method will run the Runnable's run method on the EDT. As far as I know, that's the only safe way to do this in a Swing app, since you can't guarantee that some non-EDT thread won't ever call setImg. In fact, when Alaric gets around to animation, it's virtually certain that a non-EDT thread will be calling it, or some other method that will want to call MyPanel's repaint method. You want that call made on the EDT.
 
Campbell Ritchie
Marshal
Posts: 52516
118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stevens Miller wrote:. . .
The invokeLater method will run the Runnable's run method on the EDT.
. . .
Yes, of course it will. What a silly mistake of mine!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!