My first attempt... no game experience before

Hi.
I am new to these forums, i also read comp.lang.java.GUI and other Newsgroups.
i’m working on this game, a clone of Mario Bros, and, having no experience with anything else, i started using Swing things like JLabel to do the sprites and tiles… it was going fine… until it came to the scrolling.

Just thought though, that i would post link to it here (source and exe included)…

http://members.austarmetro.com.au/~juddman/files/JarioBros.zip

i was told by someone on ACLJ-G that it was fast for a swing thing, but it’s not fast (or smooth) enough to be really playable.

my problem is that i dont know any other way to do graphics and stuff, things like what’s an ImageObserver and how do i implement it? do i have to implement the Graphics class or can i just use a BufferedImage and createGraphics? how to i load a file into an image to draw this buffer?

i dont expect you to answer all of these questions, but does anyone know of a good tutorial that can help me get a basic tile based graphics system to work? or just one that can show me how to draw graphics onto a canvas or something?

-JuddMan

Well, I’m not saying it can’t be made to work, but Swing components have some pretty heavyweight features attached to them that I don’t really see a scroller game utilizing (tooltips for the sprites? A plugable look and feel for the interface?)

Here’s quick answers to your question: An ImageObserver is an interface that will recieve update events from a Image source (typically) about changes to the image (typically done during loading). To implement, just Create a class, implement the updateImage method of the interface, and do some work when the method is called based on the parameters…it will fire the event when the image is loaded, the height is known, etc. Nothing terribly useful that I can see, usually I just use a MediaTracker to monitor the loading of images.

You wouldn’t normally implement the Graphics class, you are fine to use the BufferedImage and get the graphics for it using createGraphics() as you said.

To load the image from a file, you can do the following:


    MediaTracker tracker = new MediaTracker(new Container());
    try
    {
      InputStream in = this.getClass().getResourceAsStream(fileName);
      ByteArrayOutputStream imageByteStream = new ByteArrayOutputStream();
      byte[] currentBytes = new byte[2048];
      int bytesRead = 0;
      while ((bytesRead = in.read(currentBytes)) != -1)
        imageByteStream.write(currentBytes,0,bytesRead);
      loadedImage = Toolkit.getDefaultToolkit().createImage(imageByteStream.toByteArray());
      tracker.addImage(loadedImage, 0);
    }
    catch (Exception e)
    {}
    
    try
    {tracker.waitForAll();}
    catch (InterruptedException e)
    {}
    
    // added following lines to support drawing onto loaded image
    returnImage = new BufferedImage(loadedImage.getWidth(null),loadedImage.getHeight(null),BufferedImage.TYPE_INT_ARGB);
    returnImage.getGraphics().drawImage(loadedImage,0,0,null);
 

We load the bytes of the image into a byte[] because the version of createImage(String) looks for a filename. Using getResourceAsStream is nice because you just put the image in the classpath (like in the JAR) somewhere, and the classloader will find it. We mae a new BufferedImage because the image that is produced from the Toolkit.createImage() is not modifyable, so we just draw the image from the Toolkit onto a BufferedImage.

I wish I had a good tutorial for you, I’ve only learned by doing myself, but I think there’s some code examples on the old javagaming site.

-Chris

Thanks. this has been really helpful!

Im all my newbieness, i managed to get that code to work well enough, though i was confused as hell.

Now this may seem like a dumb point for some obvious reason, but i took a look, and saw that you need to override Paint() to put the graphics directly onto the JPanel. it took me a while also to realize that a JPanel and a JFrame both implement ImageObserver :slight_smile:

so now i have to ask you.

i dont understand your code enough to know how it works, but i found that i could do the same thing using the code below…

what i really want to know is:
is there anything wrong with using ImageIcon().getImage() to ‘cheat’ to get the image? does it give a diferent image?

i don’t need to actually change the images, just draw them onto the JPanel. i noticed also that paint is called whenever the window is resized.

is there a problem with using a JPanel to hold the graphics?


import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import java.io.*; 
import java.awt.image.*; 
 
public class GFrame extends JFrame 
{ 
 Image img; 
 int i=0; 
 int j=0; 
 public GFrame() 
 { 
  super(); 
  img = new ImageIcon("tiles/brick.gif").getImage(); 
 } 
  
 public void paint(Graphics g) { 
  g.drawImage(img, 20, 30, this); 
 } 
 
 
 public static void main(String args[]) {
            GFrame mainFrame = new GFrame();
            mainFrame.setSize(520, 568);
            mainFrame.setTitle("JarioBros");
            mainFrame.setVisible(true);
      }//end main
}