[java] applet doet het niet goed onder ie

Pagina: 1
Acties:

  • Darius
  • Registratie: Juli 2001
  • Nu online
Applet :
code:
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package spel;

import java.awt.*;
import java.applet.*;

class Const {
  static final int STEPS=5;
  static final int MANLENGTE=20;
}

public class Applet2 extends Applet implements Runnable {
  private Achtergrond achtergrond;
  private Mannetje man, vijand;
  private int appBreedte, appHoogte;
  private Image speler, ball, buffer;
  private Graphics gBuffer;
  private Bal balletje;
  private Score stand;
  private Thread thread;

  public void init()
  {
    ball=getImage(getDocumentBase(),"ball.gif");
    speler= getImage( getDocumentBase(),"mallow.gif");
    appBreedte=getSize().width;
    appHoogte=getSize().height;

    stand=new Score(appBreedte,appHoogte);
    achtergrond = new Achtergrond(appBreedte,appHoogte);
    man = new Mannetje(50,150,Const.MANLENGTE,Const.MANLENGTE,speler,this);
    vijand = new Mannetje(340,150,Const.MANLENGTE,Const.MANLENGTE,speler,this);
    balletje=new Bal(appBreedte,appHoogte,Const.MANLENGTE,achtergrond.xgrens(),50,50,6,ball,this);
    balletje.waarMan(man.x(),man.y());
    balletje.waarVijand(vijand.x(),vijand.y());
    buffer=null;
  }

  public void start() {
    balletje.start();
    if(thread==null) {
    thread=new Thread(this);
    thread.start();
    }
  }

  public void run() {
    while(true) {

    if( (balletje.baly() < vijand.y()) && (vijand.y()-vijand.lengte()>=achtergrond.ygrens1()) )
      vijand.sety(-Const.STEPS);
    else
      if(vijand.y()+vijand.lengte()<=achtergrond.ygrens2()) {
        vijand.sety(Const.STEPS);
      }
    if( (balletje.balx() < vijand.x()) && (vijand.x()>=achtergrond.xgrens()) )
      vijand.setx(-Const.STEPS);
    else
      if(man.x()+man.breedte()<=appBreedte) {
        vijand.setx(Const.STEPS);
      }

    try{Thread.sleep(75);}
    catch(InterruptedException e){}
    }
  }

  public void stop() {
    balletje.stop();
    if(thread !=null) {
    thread.stop();
    thread=null;
    }
  }

  public boolean keyDown(Event e, int key) {
    switch(key) {
    case Event.DOWN:  if(man.y()+man.lengte()<=achtergrond.ygrens2()) {
                  man.sety(Const.STEPS);
                  balletje.waarMan(man.x(),man.y());
                }
                break;
    case Event.UP:    if(man.y()-man.lengte()>=achtergrond.ygrens1()) {
                  man.sety(-Const.STEPS);
                  balletje.waarMan(man.x(),man.y());
                }
                break;
    case Event.RIGHT: if(man.x()+man.breedte()<=achtergrond.xgrens()) {
                  man.setx(Const.STEPS);
                  balletje.waarMan(man.x(),man.y());
                }
                break;
    case Event.LEFT:  if(man.x()-man.breedte()>=0) {
                  man.setx(-Const.STEPS);
                  balletje.waarMan(man.x(),man.y());
                }
                break;
    }
    return true;
    }

  public void update(Graphics g) {
    paint(g);
  }

  public void paint(Graphics g) {
    if(buffer == null) {
    buffer=createImage(getSize().width,getSize().height);
    gBuffer=buffer.getGraphics();
    }

    if(balletje.speler1) {
    stand.score1++;
    balletje.speler1=false;
    }
    if(balletje.speler2) {
    stand.score2++;
    balletje.speler2=false;
    }

    achtergrond.paint(gBuffer);
    stand.paint(gBuffer);
    man.paint(gBuffer);
    vijand.paint(gBuffer);
    balletje.paint(gBuffer);

    g.drawImage(buffer,0,0,this);
  }
}

class Score extends Canvas {
  private int appBreedte,appHoogte;
  private int x;
  public int score1, score2;
  private Font font;

  public Score(int appBreedte,int appHoogte) {
    this.appHoogte=appHoogte;
    this.appBreedte=appBreedte;
    font=new Font("Arial",Font.BOLD,30);
    x=appBreedte-30;
  }

  public void paint(Graphics g) {
    g.setFont(font);
    g.drawString(""+score1,30,30);
    g.drawString(""+score2,x,30);
  }
}


class Achtergrond extends Canvas {
  private int breedte, hoogte;

  public Achtergrond(int breedte, int hoogte) {
    this.breedte=breedte;
    this.hoogte=hoogte;
  }

  public void paint(Graphics g) {
    g.setColor(Color.blue);
    g.fillRect(0,0,breedte,150); // De Lucht
    g.setColor(Color.yellow);
    g.fillRect(0,hoogte-190,breedte,150); // Het zand
    g.setColor(Color.orange);
    g.fillRect(0,hoogte-230,breedte,40);
    g.fillRect(0,hoogte-40,breedte,40); // Rand
    g.setColor(Color.white);
    g.fillRect( (breedte/2)-5,hoogte-230,10,230); // Paal
  }

  public int xgrens() {
    return ((breedte/2)-5);
  }

  public int ygrens1() {
    return (hoogte-190);
  }

  public int ygrens2() {
    return (hoogte-40);
  }
}

class Mannetje extends Canvas {
  private int x, y, lengte, breedte;
  private Image afbeelding;
  Applet theApplet;

  public Mannetje(int x, int y, int lengte, int breedte, Image afbeelding, Applet theApplet) {
    this.x=x;
    this.y=y;
    this.breedte=breedte;
    this.lengte=lengte;
    this.afbeelding=afbeelding;
    this.theApplet=theApplet;
  }

  public void paint(Graphics g) {
    g.drawImage(afbeelding, x, y, lengte, breedte,theApplet);
  }

  public void setx(int temp) {
    x +=temp;
  }

  public void sety(int temp) {
    y +=temp;
  }

  public int lengte() {
    return lengte;
  }

  public int breedte() {
    return breedte;
  }

  public int x() {
    return x;
  }

  public int y() {
    return y;
  }
}

class Bal extends Canvas implements Runnable {
  private int grootte,x,y,dx,dy,appBreedte,appHoogte,beginx,beginy,manLengte,xgrens;
  private int manx,many,vijandx,vijandy;
  private Image ball;
  private Applet theApplet;
  private Thread thread;
  public boolean speler1,speler2;

  public Bal(int appBreedte, int appHoogte, int manLengte, int xgrens, int x, int y, int grootte, Image ball, Applet theApplet) {
    this.appBreedte=appBreedte;
    this.appHoogte=appHoogte;
    this.manLengte=manLengte;
    this.xgrens=xgrens;
    this.x=x;
    this.y=y;
    this.grootte=grootte;
    this.ball=ball;
    this.theApplet=theApplet;
    beginx=x;
    beginy=y;
  }

  public void start() {
    if (thread==null) {
    thread=new Thread(this);
    thread.start();
    }
  }

  public void run() {
    dy=1;

    while(true) {
    if(y<=0)
      dy=1;
    if( (y>=many) &&  ((x+grootte>=manx) && (x<=manx+manLengte)) ) {
      dy=-1;
      dx=1;
    }
    if( (y>=vijandy) && ((x+grootte>=vijandx) && (x<=vijandx+manLengte)) ) {
      dy=-1;
      dx=-1;
    }
    if( (y>many) && ((x+grootte<=manx) || (x>=manx+manLengte)) && (x<xgrens) ) {
      speler2=true;
      x=beginx;
      y=beginy;
      dx=0;
      dy=1;
    }
    if( (y>vijandy) && ((x+grootte<=vijandx) || (x>=vijandx+manLengte)) && (x>xgrens) ) {
      speler1=true;
      x=beginx;
      y=beginy;
      dx=0;
      dy=1;
    }
   //   if(x+grootte>=appBreedte)
   //     dx=-1;
    y=y+dy;
    x=x+dx;
    theApplet.repaint();
    sleep(10);
    }
  }

  public void stop() {
    if(thread!=null) {
    thread.stop();
    thread=null;
    }
  }

  public void waarMan(int x,int y) {
    manx=x;
    many=y;
  }

  public void waarVijand(int x,int y) {
    vijandx=x;
    vijandy=y;
  }

  public int balx() {
    return x;
  }

  public int baly() {
    return y;
  }

  public void sleep(int millisec) {
    try{ Thread.sleep(millisec); }
    catch( InterruptedException e) {}
  }

  public void paint(Graphics g) {
    g.drawImage(ball, x, y, grootte, grootte, theApplet);
  }
}

Met de appletviewer die bij de JDK meegeleverd wordt doet deze programma het perfect. Allen bij internet explorer kijkg ik in plaats van mannetjes stipjes en krijg ik helemaal geen balletje te zien. Programma moet ik overmorgen op school presenteren onder ie (woohoo). Weet iemand waar het aan kan liggen of hoe ik dit kan fixen ? Waar doet ie moeijlijk over ?

Alvast bedankt.

  • .oisyn
  • Registratie: September 2000
  • Laatst online: 00:42

.oisyn

Moderator Devschuur®

Demotivational Speaker

gebruik je de java plugin?

Give a man a game and he'll have fun for a day. Teach a man to make games and he'll never have fun again.


  • Darius
  • Registratie: Juli 2001
  • Nu online
Ja, maar ook al gebruik ik de java enviroment die bij JDK zit blijft ie raar doen. Waar zou dit aan kunnen liggen?

  • mbravenboer
  • Registratie: Januari 2000
  • Laatst online: 06-11-2025
Zorg ervoor dat in IE ook echt de plugin wordt gebruikt van de Java 2 Runtime Environment. Dit was tot voor kort niet het geval. Sinds de laatste 1.3.1 en 1.4.0 beta 3 wel.

Als je het zeker wilt weten dat ook oudere versies van de JRE worden gebruikt moet je even de HTMLconverter over je html file heen halen. Deze zet applet tags om naar object en embed tags die direct verwijzen naar de Java Plugin :) .

Blog, Stratego/XT: Program Transformation, SDF: Syntax Definition, Nix: Software Deployment


  • mbravenboer
  • Registratie: Januari 2000
  • Laatst online: 06-11-2025
Zie ook dit topic:

[topic=363051/2/25]

Hier staat precies hoe je alles moet aangeven om expliciet naar de Java Plugin te verwijzen.

Ik vermoed namelijk sterk dat je hem toch niet gebruikt...

Blog, Stratego/XT: Program Transformation, SDF: Syntax Definition, Nix: Software Deployment


  • Darius
  • Registratie: Juli 2001
  • Nu online
Bedankt voor de tip. HTMLConverter eroverheen gegooid en hij loopt nu als een trein. :)
Pagina: 1