This week's book giveaway is in the Java in General forum.
We're giving away four copies of Helidon Revealed: A Practical Guide to Oracle’s Microservices Framework and have Michael Redlich on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Beginner game

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey, im trying to make an arcade subhunt game, but i have mastered only the basics of java (even though i know C++ & VB)
i know how to use the key event, but not how to move the image
also, how do you initialize a variable to contain the positions of the left, right, top,& bottom of the image?
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To do animation, you are going to need to have a component to draw the animation on, like a JPanel if you are doing this in Swing, or a Canvas if you're using AWT. Override the paintComponent() method of the JPanel, or the paint() method of the canvas and do all your drawing in this method. You also need a thread that is updating the position or states of your non-player-controlled items and calling calling repaint() on the component at some specified rate. Have variables for storing the x and y locations of the player controlled item, and base where this image is painted in the paint() or paintComponent() method on these variables. Update these variable in the keyListener, like you are probably doing now. Have the existing thread handle the repainting for you.

If the animation is too flickery, you can do things like override the update method and add double buffering.

Hopefully this gives you some idea of what to do... If you need more help on any of this, please post again.
 
Karl Nilsson
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i understand most of the concepts, but i need examples of code!
heres my current source, if you want to point out any flaws, and where to put the threads, canvas, etc
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So far, there is no animation required that is not controlled directly by the user (i.e. moving the ship), so a separate thread isn't needed. I just changed a few things to show the general form of moving the image around. Flickering is still pretty bad, so before you're done, you'll probably want to put double buffering in, but I just wanted to give you the 'bare bones' you would need to move the image.

 
Karl Nilsson
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you!
i understand most of the code
but, i don't get this:

what does a media traker do
if i had to guess, i would say it has something to do with the image
also, could i use a timer instead of a thread for moving the subs & depth charges?
but if i had to use a thread, would i need one for each object?
as you can tell, im pretty new to java (and frustrated! )
[ February 05, 2004: Message edited by: Karl Nilsson ]
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All MediaTracker does is make sure the image is loaded. Usually java tries to load an image from a file when it is first displayed. This can cause animation to start off badly if you have a big image and you are redrawing the screen. I used it because the image wasn't loaded yet in the init() method, and I needed to get the width and height of the image to calculate the size of the rectangle correctly in the lines afterward. If the image isn't loaded yet, you would just get 0 as the width and height of the image.

Yes, you can use a Timer (Timers actually use Threads behind the scenes). You don't need a thread for each object you want to animate (you could, but this would be overkill), you only need to have an update() type of method on each of these objects to move them one more "step", update their image, etc. Then just have one main thread (or timer) that calls the update() method on each object and then repaints the screen.

A classic "bouncing ball" animation is a good example of this -
 
Karl Nilsson
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i got it to compile so far, but the image doesn't appear
this is a giant problem, but i know i am overlooking something
here's the code so far:
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where is your image stored? The way you are loading it -
means "load the image named [i]ship.gif[/url] from the same directory as the HTML page that launched this applet."
 
Karl Nilsson
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i tried to se if the rectangle was outside of the applet area, and after a few minutes, i somehow managed to get the image to appear!
and even better, it moves!
the only problem i see (only an inconvenience) is you have to click on the applet before you can move the ships
i don't know if this is fixable
now i am trying to be able to launcha torpedo
the first question i have is what is the keycode for the ctrl key is
also, i am having trouble making an array of invisible torpedoes that will appear whenever i push the control key
here's the code so far:

is there any way to just add an attachement to the post, because it gets annoying having to copy & paste all the time
[ February 09, 2004: Message edited by: Karl Nilsson ]
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good job getting the image to display...

Having to click on the applet to get control should be taken care of by requestFocus(), however, requestFocus() has to be called after the component is visible, so you'll need to add a ComponentListener and call requestFocus() in the componentShown() method.
The keycode for the CTRL key is KeyEvent.VK_CONTROL. To show and hide torpedoes while the CTRL key is pressed, all you have to do is set a boolean called something like "drawTorpedoes" to true when the CTRL key is pressed( in keyPressed() ), and set it to false when it is released( in keyReleased() ). Then in your paint() method just test for "drawTorpedoes" and actually draw the images of the torpedoes only if "drawTorpedoes" is true.

Nope, sorry, with UBB there's not a way to do an attachment to a thread.
 
Karl Nilsson
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
now the problem is with one line of code
the compiler won't let me make the bomb visible!
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What class is your "bomb"? Only Components have the setVisible() method, and from your previous code it looks like "bomb" is an Image. You'll either need to make a separate class for Bomb that has a setVisible() method, or you'll need to use the set/unset a boolean in the listener, test "if(boolean)" in the paint() method approach I had in my last post.
 
Karl Nilsson
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ahh, so repaint() just calls the paint method over again! (Duh)
oh, by the way, have you ever tried making any games Nathan?
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pretty much repaint() just calls paint() again... there's some other stuff going on behind the scenes though... First, repaint() is really just a request for the system to paint, it's not like a direct method call. This means, if you call repaint() 5000 times in one second, but the system can only paint at a rate of 200 times per second, paint() is only going to be called 200 times, not 5000 times, which would take longer than a second, and would have tons of repaint requests backed up. Second, repaint() really calls update( Graphics g ), which then calls paint( Graphics g ). You can override both update() and paint() on your components, but not repaint(), because it is a final method. This is because it's setting up actual system resources to do the graphical manipulations needed to draw frames,etc. on the screen based on your OS.

I have programmed lots of simple games (arcade-type games, puzzle games), but haven't really made a website to put them on. A few years ago I used to have a free homepage that had a couple of games on it, but all the ad banners and pop-ups that finally got added by the company hosting it made me take it down. I recently got broadband access and have thought about putting some games up in the personal webspace that is provided with the account, or just going all out, getting a domain name and getting hosted by LunarPages.
 
Karl Nilsson
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
now, i have tried to make a thread to move the torpedo when the control key is pressed, but i am getting a crapload of errors
i think most of them are bacause the rectangles for the boundaries are declared private, but im not sure
here's the code so far:
 
Karl Nilsson
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey, got the thread to work!
now i just need to get enemy subs and a score and other stuff
ill post later, when i start experimenting!
 
Karl Nilsson
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
now i'm trying to make the ship "float" instead of making sharp translations
to do this, im using 2 variables to hold the position of the final destination, and using the thread to move the ship a few twips per cycle until the ship is at its destination.
i will post again tommorrow when i have problems (and i know i will)
 
Whoever got anywhere by being normal? Just ask this exceptional tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic