Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (533)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  How to display a sprite in a Java application?  (Read 2522 times)
0 Members and 1 Guest are viewing this topic.
Offline Delnizz

Junior Newbie





« Posted 2005-10-28 17:31:09 »

Can anyone give me a code snippet or link to something like this?  I am just starting out with Java game development and I have been repeatedly frustrated to find that most of the tutorials out there will not work with java1.5.  Anything I can find about displaying an image is either in an applet, or wont work with 1.5

All the good tutorials I have found by searching this forum, like the Space Invaders101, or the Cursos de Tutoriales invaders page no longer work. 

All my java books from my classes are the same way, they only use applets or are too old to be usefull with 1.5.

Can anyone help?
Offline kevglass

JGO Kernel


Medals: 120
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #1 - Posted 2005-10-28 17:33:55 »

Um,  Space Invaders101 works fine with 1.5.

Kev

Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #2 - Posted 2005-10-28 19:50:18 »

I assume most of your drawings appears inside a JPanel, because that's what my sample code is on.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
//This class draws every element in the game

import java.awt.*;
import javax.swing.*;

public class View extends JPanel
{
    private ImageIcon pic;

    public View()
    {
        pic = new ImageIcon("images/guy.gif");
    }

    //Called by the model (the class controlling the entire game) every timestep
   public void update()
    {
        repaint();
    }

    //By overriding this method, you can add in your own things to draw
   public void paintComponent(Graphics g)
    {
        //Call the super method so that the screen is refreshed
       super.paintComponent(g);
        //Draw the image, but make sure it is loaded all the way first
       if (pic.getImageLoadStatus() == MediaTracker.COMPLETE)
            g.drawImage(pic.getImage(), xPos, yPos, width, height, pic.getImageObserver());
    }
}


And that will work, as long as you have your own values for xPos, yPos, width, height. If you don't understand something, let me know.

Basically, repaint() tells a JComponent to redraw everything to its new positions. This calls the screenUpdate (?) method (which you don't need to worry about) to erase the screen, then calls paintComponent to draw the background and foreground, etc. If you override this method, you can add in things to draw.

See my work:
OTC Software
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Delnizz

Junior Newbie





« Reply #3 - Posted 2005-10-28 20:42:02 »

Thanks Demonpants, you rock. Smiley

Kev, I don't get you.  You leave nasty replies to all my posts and never offer anything even remotely helpfull.  Why even bother to click that "reply" button???
Offline oNyx

JGO Coder


Medals: 1


pixels! :x


« Reply #4 - Posted 2005-10-28 22:24:10 »

>Kev, I don't get you.  You leave nasty replies to all my posts and never offer anything even remotely helpfull.

Huh?

the tutorial... and yes, it does work perfectly fine.

You look like someone who hasnt even learned the basics (yes, the syntax is wrong... your syntax actually [see the other thread])... and despise that kev wrote a perfectly fine reply... and you call that "nasty". No one gets you. Really.

Be thankful ffs. Tongue

弾幕 ☆ @mahonnaiseblog
Offline Delnizz

Junior Newbie





« Reply #5 - Posted 2005-10-28 23:21:23 »

Thanks for all the help guys.  Especially you oNyx, your reply was so full of constructive criticism, it gave me new hope.  It really helped me a lot.  I would also like to thank Kev for all the helpfull suggestions he made.  They really helped a lot too!  I can't wait to try some of the things you guys have suggested. Smiley

Offline oNyx

JGO Coder


Medals: 1


pixels! :x


« Reply #6 - Posted 2005-10-29 00:08:42 »

Well, its you loss. I wish you more luck in the next life. Smiley

Y'know everyone gets frustrated occasionally, but there is no reason vent your anger on others. And those who try to help you (and actually do help you - even if you dont realise that) are really a bad choice for that. Dont you see how silly that is?

弾幕 ☆ @mahonnaiseblog
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #7 - Posted 2005-10-29 01:12:52 »

Yeah, no problem with the help, but definitely don't be angry if you don't get any. Sometimes people don't have time. This forum works better if you ask advice and then give advice back when you can, as positively as possible. The only reason Kev didn't give such a detailed reply was probably because he wasn't getting bored in Assembly class like me. Smiley

See my work:
OTC Software
Offline kevglass

JGO Kernel


Medals: 120
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #8 - Posted 2005-10-29 10:01:28 »

Sorry if the reply was taken as offensive. I was just stating that it works for me (which I thought might help you carry on debugging and working out was going on - see other thread)

Sometimes I rush posts,

My apologies,

Kev

Offline Ask_Hjorth_Larsen

Junior Member




Java games rock!


« Reply #9 - Posted 2005-10-30 02:01:52 »

Quote
Kev, I don't get you.  You leave nasty replies to all my posts and never offer anything even remotely helpfull.  Why even bother to click that "reply" button???

What finishing school did you go to again?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline DanoruX

Senior Newbie




Java games rock!


« Reply #10 - Posted 2005-10-31 14:01:07 »

Whats the difference between using imageicons and bufferedimages?
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #11 - Posted 2005-10-31 22:10:57 »

A BufferedImage is much more complicated but allows for many more things to be done to the image, such as tinting or cutting out pieces of an image. ImageIcon is only a simple image that cannot be seriously altered – the only real things you can change are how it is drawn with g.drawImage(). If you are simply using sprites, ImageIcon is certainly the way to go because it is simpler (aside from the fact that this forum is for newbies), but if you are doing more complicated procedures like multi-layering an image or mapping to 3D textures, you definitely do not want to use ImageIcon.

For a 2D sprite-based game, however, it's (in my opinion) always the way to go.

See my work:
OTC Software
Offline g666

Junior Member





« Reply #12 - Posted 2005-10-31 23:12:18 »

An imageicon is an implementation of javax.swing.icon that displays an Image, on a label for example, it is not meant to be used in games.

The difference between an Image and a BufferedImage is that you can access the image data in a BufferedImage. You can use them both in games tho. I think they can both be accelerated too.

desperately seeking sanity
Offline Ask_Hjorth_Larsen

Junior Member




Java games rock!


« Reply #13 - Posted 2005-11-01 06:32:11 »

Yes, an ImageIcon is not a real image, but something containing an image. Therefore the rendering probably depends a lot on the properties of the internally stored image, which could easily be a BufferedImage or some other type.
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #14 - Posted 2005-11-02 10:05:18 »

Oh yeah, good calls, I forgot about this because I've typed it so much I don't even think about it.

When drawing an image icon, you need to say imageIcon.getImage(), because it needs to pull the actual instance of Image out. Doesn't ImageIcon itself actually extend Object only? It has its own paintIcon method as well, but I find doing it with drawImages gives more options.

One thing I did always like was that ImageIcon's also have their own ImageObserver, which simplifies having to worry about that.

See my work:
OTC Software
Offline Ask_Hjorth_Larsen

Junior Member




Java games rock!


« Reply #15 - Posted 2005-11-04 03:02:13 »

You can check all superclasses from the java API. For example,
1  
2  
3  
public class ImageIcon
extends Object
implements Icon, Serializable, Accessible

And there's a fancy superclass list above that too.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (24 views)
2014-07-24 01:59:36

Riven (24 views)
2014-07-23 21:16:32

Riven (18 views)
2014-07-23 21:07:15

Riven (21 views)
2014-07-23 20:56:16

ctomni231 (50 views)
2014-07-18 06:55:21

Zero Volt (45 views)
2014-07-17 23:47:54

danieldean (36 views)
2014-07-17 23:41:23

MustardPeter (39 views)
2014-07-16 23:30:00

Cero (55 views)
2014-07-16 00:42:17

Riven (55 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!