Hi,
ich muss eine auswählbare Anzahl von Bällen in einem Kasten hin und her springen lassen, jetzt hab ich volgende Probleme, erstens weiß ich nicht wie ich eine unendlich Schleife erzeugen soll, in der sich die bälle bewegen und ich dann trotzdem noch mehrere bälle erzeugt krieg;
2. wie kann ich das ganze passend abrechen lassen mit nem Knopf oder so, so dass man das nich mitm debugger zerstören muss;
und 3. der eine ball den ich erzeugt krieg springt öfters über die Ränder des Kastens raus und übermalt diese.
Ich hoffe das mir jemand dabei helfen kann.
Hier der source code:
java.BallDemo
[highlight=Java]
import java.awt.*;
import java.awt.geom.*;
import java.awt.Component;
import java.awt.event.*;
import java.util.*;
public class BallDemo
{
private Canvas myCanvas;
public BallDemo()
{
myCanvas = new Canvas("Ball Demo", 600, 500);
myCanvas.setVisible(true);
}
public void bounceBox(int numberBalls)
{
drawFrame();
bounceInTheBox current = new bounceInTheBox(myCanvas);
current.balls(numberBalls);
}
public void drawFrame()
{
myCanvas.setForegroundColor(Color.blue);
Rectangle rect = new Rectangle(20, 20,myCanvas.getSize().width-40,myCanvas.getSize().height-40);
myCanvas.fill(rect);
myCanvas.setForegroundColor(Color.white);
Rectangle rect2 = new Rectangle(25, 25,myCanvas.getSize().width-50,myCanvas.getSize().height-50);
myCanvas.fill(rect2);
}
}
[/highlight]
java.bounceInTheBox
[highlight=Java]
import java.awt.*;
import java.util.*;
public class bounceInTheBox
{
private Canvas canvas;
public bounceInTheBox(Canvas canvas)
{
this.canvas = canvas;
}
protected void balls(int number)
{
ArrayList<BoxBall> balls = new ArrayList<BoxBall>();
Random ran = new Random();
for (int i = 0; i<number;i++)
{
int xPos = 50,yPos = 50;
boolean finished = false;
while(!finished)
{
xPos = ran.nextInt(canvas.getSize().width-51);
if (xPos>=25)
{
finished = true;
}
}
finished = false;
while(!finished)
{
yPos = ran.nextInt(canvas.getSize().height-51);
if (yPos>=25)
{
finished = true;
}
}
finished = false;
int diameter = 111;
while (!finished)
{
diameter = ran.nextInt(30)+10;
if (diameter%4 == 0)
{
finished = true;
}
}
BoxBall current = new BoxBall(xPos,yPos,diameter,25,canvas.getSize().width-50,25,canvas.getSize().height-50,canvas);
balls.add(current);
}
for (BoxBall current : balls)
{
current.move();
}
}
}
[/highlight]
java.BoxBall
[highlight=Java]
import java.awt.*;
import java.awt.geom.*;
import java.util.*;
public class BoxBall
{
private int ballDegradation = 2;
private Ellipse2D.Double circle;
private Color color;
private int diameter;
private int xPos;
private int yPos;
private int x1Corn,x2Corn,y1Corn,y2Corn;
private Canvas canvas;
private int speed;
/**
* Constructor for objects of class BoxBall
*/
protected BoxBall(int xPos, int yPos, int ballDiameter, int x1Corn,
int x2Corn,int y1Corn,int y2Corn, Canvas drawingCanvas)
{
this.xPos = xPos;
this.yPos = yPos;
this.x1Corn = x1Corn;
this.x2Corn = x2Corn;
this.y1Corn = y1Corn;
this.y2Corn = y2Corn;
diameter = ballDiameter;
canvas = drawingCanvas;
Random current = new Random();
boolean finished = false;
while (!finished)
{
speed = current.nextInt(4);
if (speed > 0)
{
finished = true;
}
}
color = Color.getHSBColor( current.nextFloat(), 1.0F, 1.0F );
}
public void draw()
{
canvas.setForegroundColor(color);
canvas.fillCircle(xPos, yPos, diameter);
}
public void erase()
{
canvas.eraseCircle(xPos, yPos, diameter);
}
public void move()
{
boolean finished = false;
int a=2,b=1;
Random current = new Random();
while(!finished)
{
a = current.nextInt(speed+1)-current.nextInt(2*speed+1);
b= current.nextInt(speed+1)-current.nextInt(2*speed+1);
int result = Math.abs(a)+Math.abs(b);
if (result == speed)
{
finished = true;
}
}
while (speed != 0)
{
if (xPos < x2Corn)
{
erase();
}
else if (yPos < y2Corn)
{
erase();
}
else if (xPos < x2Corn && yPos < y2Corn)
{
erase();
}
xPos +=a;
yPos +=b;
if (xPos <= x1Corn)
{
erase();
xPos = x1Corn;
draw();
a *=-1;
}
if (yPos <= y1Corn)
{
erase();
yPos = y1Corn;
draw();
b *=-1;
}
if (xPos >= x2Corn)
{
erase();
xPos = x2Corn - diameter;
draw();
a *=-1;
}
if (yPos >= y2Corn)
{
erase();
yPos = y2Corn - diameter ;
draw();
b *=-1;
}
if (xPos <= x1Corn && yPos <= y1Corn)
{
erase();
xPos = x1Corn+diameter;
yPos = y1Corn+diameter;
draw();
a *=-1;
b *=-1;
}
if (xPos >= x2Corn+diameter && yPos <= y1Corn)
{
erase();
xPos = x2Corn+diameter;
yPos = y1Corn;
draw();
a *=-1;
b *=-1;
}
if (xPos <= x1Corn && yPos >= y2Corn+diameter)
{
erase();
xPos = x1Corn;
yPos = y2Corn+diameter;
draw();
a *=-1;
b *=-1;
}
if (xPos >= x2Corn+diameter && yPos >= y2Corn+diameter)
{
erase();
xPos = x2Corn+diameter;
yPos = y2Corn+diameter;
draw();
a *=-1;
b *=-1;
}
if (xPos < x2Corn)
{
draw();
}
else if (yPos < y2Corn)
{
draw();
}
else if (xPos < x2Corn && yPos < y2Corn)
{
draw();
}
canvas.wait(10);
}
}
public int getXPos()
{
return xPos;
}
public int getYPos()
{
return yPos;
}
}
[/highlight]
Die Klasse Canvas lass ich aus falls jemand die doch noch gerne hätte eben sagen.
ich muss eine auswählbare Anzahl von Bällen in einem Kasten hin und her springen lassen, jetzt hab ich volgende Probleme, erstens weiß ich nicht wie ich eine unendlich Schleife erzeugen soll, in der sich die bälle bewegen und ich dann trotzdem noch mehrere bälle erzeugt krieg;
2. wie kann ich das ganze passend abrechen lassen mit nem Knopf oder so, so dass man das nich mitm debugger zerstören muss;
und 3. der eine ball den ich erzeugt krieg springt öfters über die Ränder des Kastens raus und übermalt diese.
Ich hoffe das mir jemand dabei helfen kann.
Hier der source code:
java.BallDemo
[highlight=Java]
import java.awt.*;
import java.awt.geom.*;
import java.awt.Component;
import java.awt.event.*;
import java.util.*;
public class BallDemo
{
private Canvas myCanvas;
public BallDemo()
{
myCanvas = new Canvas("Ball Demo", 600, 500);
myCanvas.setVisible(true);
}
public void bounceBox(int numberBalls)
{
drawFrame();
bounceInTheBox current = new bounceInTheBox(myCanvas);
current.balls(numberBalls);
}
public void drawFrame()
{
myCanvas.setForegroundColor(Color.blue);
Rectangle rect = new Rectangle(20, 20,myCanvas.getSize().width-40,myCanvas.getSize().height-40);
myCanvas.fill(rect);
myCanvas.setForegroundColor(Color.white);
Rectangle rect2 = new Rectangle(25, 25,myCanvas.getSize().width-50,myCanvas.getSize().height-50);
myCanvas.fill(rect2);
}
}
[/highlight]
java.bounceInTheBox
[highlight=Java]
import java.awt.*;
import java.util.*;
public class bounceInTheBox
{
private Canvas canvas;
public bounceInTheBox(Canvas canvas)
{
this.canvas = canvas;
}
protected void balls(int number)
{
ArrayList<BoxBall> balls = new ArrayList<BoxBall>();
Random ran = new Random();
for (int i = 0; i<number;i++)
{
int xPos = 50,yPos = 50;
boolean finished = false;
while(!finished)
{
xPos = ran.nextInt(canvas.getSize().width-51);
if (xPos>=25)
{
finished = true;
}
}
finished = false;
while(!finished)
{
yPos = ran.nextInt(canvas.getSize().height-51);
if (yPos>=25)
{
finished = true;
}
}
finished = false;
int diameter = 111;
while (!finished)
{
diameter = ran.nextInt(30)+10;
if (diameter%4 == 0)
{
finished = true;
}
}
BoxBall current = new BoxBall(xPos,yPos,diameter,25,canvas.getSize().width-50,25,canvas.getSize().height-50,canvas);
balls.add(current);
}
for (BoxBall current : balls)
{
current.move();
}
}
}
[/highlight]
java.BoxBall
[highlight=Java]
import java.awt.*;
import java.awt.geom.*;
import java.util.*;
public class BoxBall
{
private int ballDegradation = 2;
private Ellipse2D.Double circle;
private Color color;
private int diameter;
private int xPos;
private int yPos;
private int x1Corn,x2Corn,y1Corn,y2Corn;
private Canvas canvas;
private int speed;
/**
* Constructor for objects of class BoxBall
*/
protected BoxBall(int xPos, int yPos, int ballDiameter, int x1Corn,
int x2Corn,int y1Corn,int y2Corn, Canvas drawingCanvas)
{
this.xPos = xPos;
this.yPos = yPos;
this.x1Corn = x1Corn;
this.x2Corn = x2Corn;
this.y1Corn = y1Corn;
this.y2Corn = y2Corn;
diameter = ballDiameter;
canvas = drawingCanvas;
Random current = new Random();
boolean finished = false;
while (!finished)
{
speed = current.nextInt(4);
if (speed > 0)
{
finished = true;
}
}
color = Color.getHSBColor( current.nextFloat(), 1.0F, 1.0F );
}
public void draw()
{
canvas.setForegroundColor(color);
canvas.fillCircle(xPos, yPos, diameter);
}
public void erase()
{
canvas.eraseCircle(xPos, yPos, diameter);
}
public void move()
{
boolean finished = false;
int a=2,b=1;
Random current = new Random();
while(!finished)
{
a = current.nextInt(speed+1)-current.nextInt(2*speed+1);
b= current.nextInt(speed+1)-current.nextInt(2*speed+1);
int result = Math.abs(a)+Math.abs(b);
if (result == speed)
{
finished = true;
}
}
while (speed != 0)
{
if (xPos < x2Corn)
{
erase();
}
else if (yPos < y2Corn)
{
erase();
}
else if (xPos < x2Corn && yPos < y2Corn)
{
erase();
}
xPos +=a;
yPos +=b;
if (xPos <= x1Corn)
{
erase();
xPos = x1Corn;
draw();
a *=-1;
}
if (yPos <= y1Corn)
{
erase();
yPos = y1Corn;
draw();
b *=-1;
}
if (xPos >= x2Corn)
{
erase();
xPos = x2Corn - diameter;
draw();
a *=-1;
}
if (yPos >= y2Corn)
{
erase();
yPos = y2Corn - diameter ;
draw();
b *=-1;
}
if (xPos <= x1Corn && yPos <= y1Corn)
{
erase();
xPos = x1Corn+diameter;
yPos = y1Corn+diameter;
draw();
a *=-1;
b *=-1;
}
if (xPos >= x2Corn+diameter && yPos <= y1Corn)
{
erase();
xPos = x2Corn+diameter;
yPos = y1Corn;
draw();
a *=-1;
b *=-1;
}
if (xPos <= x1Corn && yPos >= y2Corn+diameter)
{
erase();
xPos = x1Corn;
yPos = y2Corn+diameter;
draw();
a *=-1;
b *=-1;
}
if (xPos >= x2Corn+diameter && yPos >= y2Corn+diameter)
{
erase();
xPos = x2Corn+diameter;
yPos = y2Corn+diameter;
draw();
a *=-1;
b *=-1;
}
if (xPos < x2Corn)
{
draw();
}
else if (yPos < y2Corn)
{
draw();
}
else if (xPos < x2Corn && yPos < y2Corn)
{
draw();
}
canvas.wait(10);
}
}
public int getXPos()
{
return xPos;
}
public int getYPos()
{
return yPos;
}
}
[/highlight]
Die Klasse Canvas lass ich aus falls jemand die doch noch gerne hätte eben sagen.