Hallo!
Ich habe mit folgenden Fehler zu kämpfen, wenn ich die Klasse aufrufe mit
Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError
at sun.misc.Unsafe.compareAndSwapLong(Native Method)
at java.util.concurrent.atomic.AtomicLong.compareAndSet(AtomicLong.java:121)
at java.util.Random.next(Random.java:139)
at java.util.Random.nextInt(Random.java:253)
at PRider.CRider.paintStep(CRider.java:65)
at PRider.CRider.paintStep(CRider.java:86)
at PRider.CRider.paintStep(CRider.java:76)
at PRider.CRider.paintStep(CRider.java:96)
usw...
Alles anzeigen
Vorher bin ich bei den 4 ifs immer über die Arraygrenzen geschossen. Problem gelöst und jetzt das... xD
Hat wer ne Tipp?
Gruß,
Max123
Ich habe mit folgenden Fehler zu kämpfen, wenn ich die Klasse aufrufe mit
Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError
at sun.misc.Unsafe.compareAndSwapLong(Native Method)
at java.util.concurrent.atomic.AtomicLong.compareAndSet(AtomicLong.java:121)
at java.util.Random.next(Random.java:139)
at java.util.Random.nextInt(Random.java:253)
at PRider.CRider.paintStep(CRider.java:65)
at PRider.CRider.paintStep(CRider.java:86)
at PRider.CRider.paintStep(CRider.java:76)
at PRider.CRider.paintStep(CRider.java:96)
usw...
Quellcode
- /*
- * CRider.java
- *
- * Created on 20. März 2007
- *
- * To change this template, choose Tools | Template Manager
- * and open the template in the editor.
- */
- package PRider;
- import java.awt.*;
- import java.util.*;
- import javax.swing.JPanel;
- /**
- *
- * @author Max123
- */
- public class CRider {
- int x;
- int y;
- Graphics g;
- boolean alive;
- int lastx;
- int lasty;
- boolean matrix[][];
- Random r;
- int richtung = 0;
- /** Creates a new instance of CRider */
- public CRider(JPanel area) {
- r = new Random();
- alive = true;
- g = area.getGraphics();
- x = area.WIDTH;
- y = area.HEIGHT;
- matrix = new boolean[x+2][y+2];
- for (int i=0; i<x+2; i++) {
- matrix[i][0] = false;
- matrix[i][y+1] = false;
- }
- for (int i=0; i<y+2; i++) {
- matrix[0][i] = false;
- matrix[x+1][i] = false;
- }
- lastx = r.nextInt(x)+1;
- lasty = r.nextInt(y)+1;
- while (alive)
- paintStep(0);
- }
- private void paintStep(int tiefe) {
- if (alive) {
- if(richtung == 0) {
- if(!(lastx+1 > x) || matrix[lastx+1][lasty]) {
- lastx += 1;
- } else {
- int old = richtung;
- while (old == richtung)
- richtung = r.nextInt(4);
- paintStep(tiefe+1);
- }
- }
- else if(richtung == 1) {
- if(!(lasty+1 > y) || matrix[lastx][lasty+1]) {
- lasty += 1;
- } else {
- int old = richtung;
- while (old == richtung)
- richtung = r.nextInt(4);
- paintStep(tiefe+1);
- }
- }
- else if(richtung == 2) {
- if(!(lastx-1 < x) || matrix[lastx-1][lasty]) {
- lastx -= 1;
- } else {
- int old = richtung;
- while (old == richtung)
- richtung = r.nextInt(4);
- paintStep(tiefe+1);
- }
- }
- else if(richtung == 3) {
- if(!(lasty-1 < y) || matrix[lastx][lasty-1]) {
- lasty -=1;
- } else {
- int old = richtung;
- while (old == richtung)
- richtung = r.nextInt(4);
- paintStep(tiefe+1);
- }
- }
- }
- g.drawRect(lastx-1,lasty-1,1,1);
- if (tiefe > 4)
- alive = false;
- }
- }
Vorher bin ich bei den 4 ifs immer über die Arraygrenzen geschossen. Problem gelöst und jetzt das... xD
Hat wer ne Tipp?

Gruß,
Max123