Hi,
ich habe gerade ein Spiele-Tutorial kompiliert und da wollte ich auch mal ein paar Sterne hinzufügen.
Wenn die Anzahl der Sterne bei x<=125 bleibt, kommt diese Fehlermeldung nicht und die Sterne werden auch gezeichnet, aber wenn ich die Anzahl auf 130 erhöhe, kommt die oben genannte Fehlermeldung.
Hier der Code:
Liebe Grüße
Reality
ich habe gerade ein Spiele-Tutorial kompiliert und da wollte ich auch mal ein paar Sterne hinzufügen.
Wenn die Anzahl der Sterne bei x<=125 bleibt, kommt diese Fehlermeldung nicht und die Sterne werden auch gezeichnet, aber wenn ich die Anzahl auf 130 erhöhe, kommt die oben genannte Fehlermeldung.
Applet Main besitzt keinen öffentlichen Konstruktor Main()
Hier der Code:
Code:
import java.awt.*;
import java.util.*;
public class Stars {
int posX[] = new int[130];
int posY[] = new int[130];
Stars(){
Random r = new Random();
for(byte i = 0; i<posX.length; i++){
posX[i] = r.nextInt(400);
posY[i] = r.nextInt(300);
}
}
public void drawStars(Graphics g){
g.setColor(Color.white);
for(byte i = 0; i<posX.length; i++)
g.fillOval(posX[i], posY[i], 1, 1);
}
}
Code:
import java.applet.*;
import java.awt.*;
public class Main extends Applet implements Runnable
{
// variables
private Thread th;
private Player player;
private Shot [] shots;
// speed constants
private final int shotSpeed = -2;
private final int playerLeftSpeed = -2;
private final int playerRightSpeed = 2;
// move flags
private boolean playerMoveLeft;
private boolean playerMoveRight;
private Stars stars = new Stars();
// double buffering
private Image dbImage;
private Graphics dbg;
public void init()
{
setBackground (Color.black);
player = new Player(150, 280);
shots = new Shot[5];
}
public void start ()
{
th = new Thread(this);
th.start ();
}
public void stop()
{
th.stop();
}
public void destroy()
{
th.stop();
}
public void run ()
{
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
while (true)
{
// do operations on shots in shots array
for(int i=0; i<shots.length; i++)
{
if(shots[i] != null)
{
// move shot
shots[i].moveShot(shotSpeed);
// test if shot is out
if(shots[i].getYPos() < 0)
{
// remove shot from array
shots[i] = null;
}
// other operations
// ...
// test for collisions with enemies
// ...
}
}
// move player
if(playerMoveLeft)
{
player.moveX(playerLeftSpeed);
}
else if(playerMoveRight)
{
player.moveX(playerRightSpeed);
}
// repaint applet
repaint();
try
{
Thread.sleep(10);
}
catch (InterruptedException ex)
{
// do nothing
}
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
}
}
public boolean keyDown(Event e, int key)
{
if(key == Event.LEFT)
{
playerMoveLeft = true;
}
else if(key == Event.RIGHT)
{
playerMoveRight = true;
}
else if(key == 32)
{
// generate new shot and add it to shots array
for(int i=0; i<shots.length; i++)
{
if(shots[i] == null)
{
shots[i] = player.generateShot();
break;
}
}
}
return true;
}
public boolean keyUp(Event e, int key)
{
if(key == Event.LEFT)
{
playerMoveLeft = false;
}
else if(key == Event.RIGHT)
{
playerMoveRight = false;
}
return true;
}
public void update (Graphics g)
{
if (dbImage == null)
{
dbImage = createImage (this.getSize().width, this.getSize().height);
dbg = dbImage.getGraphics ();
}
dbg.setColor (getBackground ());
dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);
dbg.setColor (getForeground());
paint (dbg);
g.drawImage (dbImage, 0, 0, this);
}
public void paint (Graphics g)
{
// draw player
player.drawPlayer(g);
stars.drawStars(g);
// draw shots
for(int i=0; i<shots.length; i++)
{
if(shots[i] != null)
{
shots[i].drawShot(g);
}
}
}
}
Liebe Grüße
Reality