Home · Examples 

Code for Qt Jambi Generator Example

This is the Java code for the generator example. The the typesystem specification and C++ files are found in the generator_example directory in the directory where Qt Jambi was installed.

** Copyright (C) 1992-2009 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** This file is part of Qt Jambi.
** Commercial Usage
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
** In addition, as a special exception, Nokia gives you certain
** additional rights. These rights are described in the Nokia Qt LGPL
** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
** package.
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE

package com.trolltech.examples;

import java.util.List;

import com.trolltech.examples.generator.AbstractGameObjectInterface;
import com.trolltech.examples.generator.Game;
import com.trolltech.examples.generator.GameAction;
import com.trolltech.examples.generator.GameAnimation;
import com.trolltech.examples.generator.GameObject;
import com.trolltech.examples.generator.GameScene;
import com.trolltech.examples.generator.Point3D;
import com.trolltech.qt.core.QPointF;
import com.trolltech.qt.core.QRectF;
import com.trolltech.qt.gui.QApplication;
import com.trolltech.qt.gui.QGraphicsView;
import com.trolltech.qt.gui.QIcon;
import com.trolltech.qt.gui.QImage;
import com.trolltech.qt.gui.QPainterPath;

class InventoryAction extends GameAction
    public GameAction clone() {
        return new InventoryAction();

    public boolean perform(GameScene scene) {
        if (objects().isEmpty()) {
            List<AbstractGameObjectInterface> inventory = scene.egoInventory();

            if (inventory.isEmpty()) {
                scene.message("Your pockets are empty");
            } else {
                String msg = "You are carrying:\n";
                for (AbstractGameObjectInterface gameObject : inventory) {
                    if (gameObject instanceof GameObject) {
                        msg += ((GameObject) gameObject).description() + "\n";


            return true;
        } else {
            return false;

    public InventoryAction() {

class ChickenObject extends GameObject {

    public ChickenObject(GameScene scene, String name) {
        super(scene, name);

        used.connect(this, "youCantUseTheChicken()");

    private void youCantUseTheChicken() {
        gameScene().message("You can't use the rubber chicken for anything");

@QtJambiExample(name = "Generator Example")
public class GeneratorExample extends GameScene {

    private static final String resourcesLocation = "classpath:com/trolltech/examples/generator/images/";

    public GeneratorExample()
        int w = 0; int h = 0;

        // Set up scene
            setDescription("You are standing next to a restaurant and it is sunny outside.");

            QImage img = new QImage(resourcesLocation + "background.png");

            w = img.width(); h = img.height();
            setSceneRect(new QRectF(0.0, 0.0, w, h));

        // Player avatar
        setEgoObject(makeEgo(w, h));

        // Inventory command
        grammar().addVerb("inventory", new InventoryAction());
        grammar().addVerb("inv", new InventoryAction());

        // Make boundary
            QPainterPath path = new QPainterPath(new QPointF(0, 305));
            path.lineTo(new QPointF(306, 305));
            path.lineTo(new QPointF(326, 293));
            path.lineTo(new QPointF(357, 293));
            path.lineTo(new QPointF(378, 305));
            path.lineTo(new QPointF(635, 305));
            path.lineTo(new QPointF(635, 200));
            path.lineTo(new QPointF(0, 200));

            GameObject boundary = new GameObject(this, "boundary");


        // Make chicken
            GameObject chicken = new ChickenObject(this, "a rubber chicken with a pulley in the middle");

            chicken.setDescription("It's a rubber chicken with a pulley in the middle.");
            chicken.addName("rubber chicken");
            chicken.addName("rubber chicken with a pulley in the middle");
            chicken.addName("rubber chicken with pulley");
            chicken.addName("rubber chicken with pulley in the middle");
            chicken.addName("rubber chicken with pulley in middle");
            chicken.addName("chicken with a pulley in the middle");
            chicken.addName("chicken with pulley");
            chicken.addName("chicken with pulley in the middle");
            chicken.addName("chicken with pulley in middle");
            chicken.setPosition(new Point3D(100.0, 330.0, 0.0));
            chicken.setFlags(Game.ObjectFlag.Blocking, Game.ObjectFlag.CanPickUp);

            GameAnimation a = new GameAnimation(Game.AnimationType.NoAnimation);
            a.addFrame(new QImage(resourcesLocation + "chicken.png"));


        setWindowIcon(new QIcon("classpath:com/trolltech/images/qt-logo.png"));
        setWindowTitle(tr("Generator Example"));

        message("Press any letter to write a command and enter when you are done. Use the arrow keys to move around."
                     +" Hit enter when you are done reading this message.");

    private GameAnimation makeAnimation(Game.AnimationType type, String nameTemplate, int startIdx, int endIdx) {
        GameAnimation a = new GameAnimation(type);


        for (int i=startIdx; i<=endIdx; ++i)
            a.addFrame(new QImage(resourcesLocation + nameTemplate.replace("#", new Integer(i).toString())));

        return a;

    private GameObject makeEgo(int w, int h) {
        GameObject ego = new GameObject(this);

        ego.setPosition(new Point3D(w / 2.0, 350.0, 0.0));

        ego.setAnimation(makeAnimation(Game.AnimationType.WalkingHorizontally, "walk#.png", 1, 4));
        ego.setAnimation(makeAnimation(Game.AnimationType.StandingStill, "walk#.png", 2, 2));
        ego.setAnimation(makeAnimation(Game.AnimationType.WalkingFromScreen, "walkaway#.png", 1, 2));
        ego.setAnimation(makeAnimation(Game.AnimationType.WalkingToScreen, "walktowards#.png", 1, 2));

        return ego;

    public static void main(String[] args) {
        GeneratorExample ex = new GeneratorExample();



Copyright © 2009 Nokia Corporation and/or its subsidiary(-ies) Trademarks
Qt Jambi 4.5.2_01