Title:

Java Game Programming Developement Tutorial

Home
deutsch
  
ISBN: 3446216375   ISBN: 3446216375   ISBN: 3446216375   ISBN: 3446216375 
 
  Wir empfehlen:       
 

Using pictures in applets

Just like "adding" sound files to an applet you can add pictures. Now I want to explain to you how to do this by adding a background picture to our "ball bounce" applet. Again we have to declare a instance variable, an Image object called backImage:

    Image backImage;

Then we load a picture to our applet and store it in the object reference backImage (import java.applet.* and java.net.*!!). This works very similar to loading sound files, please add these lines to your init() - method:

    // load file Land.jpg
    backImgage = getImage (getCodeBase (), "Land.jpg");

As a last step we have to paint this picture in the paint() - method of the applet:

    // drawing the image: g.drawImage (name, x- value, y - value, frame)
    g.drawImage (backImage, 0, 0, this);

That's it! I used a *.jpg - file in this example but that is not that good. Normaly you should use *.gif - files, because their size is not that big (makes the applet run faster) and they are more common in the internet. An other reason to use *.gif - files is that you can define the background of a *.gif - file transparent. So you can use them to overlay for example a ball object (which is just a colored circle at the moment) with a real good looking picture. You can see a example for this in the game Streethockey and maybe there will be a "advanced topic" for this technique someday!

Sourcecode download
Take a look at the applet

Next chapter

Handling mouse events

  
Programmieren in Java (Broschiert)
von Fritz Jobst
Siehe auch:
Grundkurs Programmieren in Java 1. Der Einstieg in Programmierung und Objektorientierung
von Dietmar Ratz
Java 5 in 21 Tagen. Schritt für Schritt zum Profi
von Rogers Cadenhead
Jetzt lerne ich Java
von Dirk Louis
Das Einsteigerseminar Objektorientierte Programmierung in Java. Der methodische und ausführliche Einstieg
von Alexander Niemann
Beyond the C++ Standard Library. An Introduction to Boost
von Björn Karlsson
 
    
     

Back to the topic sites:
StudyPaper.com/Startseite/Computer/Informatik/Programmieren/Java
StudyPaper.com/Startseite/Computer/Spiele

External Links to this site are permitted without prior consent.
   
  Home  |  deutsch  |  Set bookmark  |  Send a friend a link  |  Copyright ©  |  Impressum