Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (475)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (530)
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  
  My first attempt... no game experience before  (Read 1508 times)
0 Members and 1 Guest are viewing this topic.
Offline JuddMan

Senior Member


Medals: 1


Your Ad Here


« Posted 2002-11-27 15:15:38 »

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
Offline cknoll

Junior Member




Flame On!


« Reply #1 - Posted 2002-11-27 18:24:15 »

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:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
    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
Offline JuddMan

Senior Member


Medals: 1


Your Ad Here


« Reply #2 - Posted 2002-11-28 02:09:52 »

Thanks. this has been really helpful!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline JuddMan

Senior Member


Medals: 1


Your Ad Here


« Reply #3 - Posted 2002-11-29 06:06:37 »

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 Smiley

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?

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  
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
}  
 
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.

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

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

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

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

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

Riven (42 views)
2014-07-14 18:02:53

OpenGLShaders (29 views)
2014-07-14 16:23:47

Riven (29 views)
2014-07-14 11:51:35

quew8 (26 views)
2014-07-13 13:57:52

SHC (63 views)
2014-07-12 17:50:04
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!