Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
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  
  Applet tile engine  (Read 708 times)
0 Members and 1 Guest are viewing this topic.
Offline erickm

Innocent Bystander





« Posted 2006-04-15 04:16:32 »

I am not trying to make a game with an applet, just do some simple tile rendering things. I have come up with a nice effect, animated tiles too. The only problem is sometimes it loads in my browser and sometimes it does not. I am currenly using firefox, if I open the applet with ie it loads more often but still not all the time. I posted my full source and was wondering if someone can tell me why this is happening. Thanks.

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  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class Tile_engine extends Applet implements KeyListener, Runnable {

  private int keyPressed = -1;
  private int screenX = 0;
  private int screenY = 0;
  private final int TILE_MAX = 11;
  private final int TILE_SIZE = 32;
  private final int APPLET_WIDTH = 640;
  private final int APPLET_HEIGHT = 480;
  private final int SKY = 0;
  private final int CLOUD_1 = 1;
  private final int CLOUD_2 = 2;
  private final int RED_BRICKS = 3;
  private final int GRASS = 4;
  private final int DIRT = 5;
  private final int STONE = 6;
  private final int QBOX1 = 7;
  private final int QBOX2 = 8;
  private final int QBOX3 = 9;
  private final int QBOX4 = 10;
  private final int MAP_ROWS = 15;
  private final int MAP_COLS = 65;
  private Image background = null;
  private Image buffer = null;
  private Graphics bgBuff;
  private Graphics bufferg;
  private Image[] tile = new Image[TILE_MAX];

  private final int map[][] =
    {{0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,0,0,0,0,0,0,0,0,0,0,0,0,0},
     {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
     {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,0,0,0,0,0,0,0,0,0,6,6,6,6,6,0,0,0,0,0,1,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,0,0,0,0,0},
     {0,0,1,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
     {0,0,0,0,0,0,0,0,0,0,0,0,7,7,7,7,0,0,0,0,0,0,0,0,0,0,1,2,0,0,0,0,0,0,0,0,0,6,0,0,0,0,0,0,0,0,1,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
     {0,0,0,0,0,0,0,1,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
     {0,0,0,0,0,0,0,0,0,0,0,0,0,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,6,4,4,4,4},
     {5,5,5,5,5,5,5,5,0,0,5,5,5,5,5,5,5,5,5,5,5,5,0,0,5,0,5,0,0,5,5,5,0,0,0,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,0,0,0,5,5,5,5}};
   
 
  public void init( ) {
    tile[SKY] = getImage(getDocumentBase(), "sky.png");
    tile[CLOUD_1] = getImage(getDocumentBase(), "cloud1.png");
    tile[CLOUD_2] = getImage(getDocumentBase(), "cloud2.png");
    tile[RED_BRICKS] = getImage(getDocumentBase(), "redBricks.png");
    tile[GRASS] = getImage(getDocumentBase(), "grass.png");
    tile[DIRT] = getImage(getDocumentBase(), "dirt.png");
    tile[STONE] = getImage(getDocumentBase(), "stone.png");
    tile[QBOX1] = getImage(getDocumentBase(), "qbox1.png");
    tile[QBOX2] = getImage(getDocumentBase(), "qbox2.png");
    tile[QBOX3] = getImage(getDocumentBase(), "qbox3.png");
    tile[QBOX4] = getImage(getDocumentBase(), "qbox4.png");

    buffer = createImage(TILE_SIZE * MAP_COLS, TILE_SIZE * MAP_ROWS);
    bufferg = buffer.getGraphics();

    addKeyListener(this);

    if(buffer != null) {
      int tile_id;
      for(int y=0;y<MAP_ROWS;y++) {
        for(int x=0;x<MAP_COLS;x++) {
          tile_id = map[y][x];
          bufferg.drawImage(tile[tile_id], x * TILE_SIZE, y * TILE_SIZE, this);
        }
      }
    }

  }

  public void start( ) {
    Thread th = new Thread(this);
    // start this thread
    th.start();
  }

  public void run( ) {
    Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
    while(true) {
      animate_tiles();
   
      try {
   Thread.sleep(200);
      } catch(InterruptedException ex) {
      }
      Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
    }
  }
 
  public void animate_tiles( ) {
    int tile_id;
   
    for(int y=0;y<MAP_ROWS;y++) {
      for(int x=0;x<MAP_COLS;x++) {
        tile_id = map[y][x];
     
        switch(tile_id) {
          case QBOX1: map[y][x] = QBOX2; break;
          case QBOX2: map[y][x] = QBOX3; break;
          case QBOX3: map[y][x] = QBOX4; break;
          case QBOX4: map[y][x] = QBOX1; break;
          default: break; //not needed  
        }
      }
    }
  }

  public void paint(Graphics g) {}

  public void update(Graphics g) {
    //animate_tiles();

    if(buffer != null) {
      int tile_id;
      for(int y=0;y<MAP_ROWS;y++) {
        for(int x=0;x<MAP_COLS;x++) {
          tile_id = map[y][x];
          bufferg.drawImage(tile[tile_id], x * TILE_SIZE, y * TILE_SIZE, this);
        }
      }
    }

    paint(bufferg);
    g.drawImage(buffer, screenX, screenY, this);
    repaint();
  }

  public void keyReleased( KeyEvent e ) { }
  public void keyPressed( KeyEvent e ) { }
  public void keyTyped( KeyEvent e ) {  
    char c = e.getKeyChar();
    if(c == 'z') { screenX+=5; }
    else if(c == 'c') { screenX-=5; }
  }  
}
Offline Serethos

Junior Devvie




Java games rock!


« Reply #1 - Posted 2006-04-18 10:52:51 »

you should post a more detailed description of the problem: does the applet stop with an error or is only nothing
visible? what does the java-console say (exception?) ?
my first guess is only that your loading several images in init() using no mediatracker or imageicon class. this can
lead to have some sideeffects of your applet, like images wont be displayed for quite a time while the programm is
yet running.
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.

rwatson462 (35 views)
2014-12-15 09:26:44

Mr.CodeIt (26 views)
2014-12-14 19:50:38

BurntPizza (54 views)
2014-12-09 22:41:13

BurntPizza (87 views)
2014-12-08 04:46:31

JscottyBieshaar (49 views)
2014-12-05 12:39:02

SHC (64 views)
2014-12-03 16:27:13

CopyableCougar4 (67 views)
2014-11-29 21:32:03

toopeicgaming1999 (127 views)
2014-11-26 15:22:04

toopeicgaming1999 (118 views)
2014-11-26 15:20:36

toopeicgaming1999 (34 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
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!