Hallo,
Ich versuche mich gerade an der Simulation "Bouncing Balls" in Java. Ich bin gerade dabei den Code für das JPanel vom Code für das JFrame zu trennen also in zwei unterschiedliche Klassen zu schreiben. Das Programm besitzt eine Kollisionserkennung aber noch keine Kollisionsantwort. Die soll noch folgen. Mein Problem liegt darin dass ich die Bälle nicht auf dem Panel gezeichnet bekomme.
Quellcode:
Ball.java
Board.java
BallPanel.java
Main.java
Vielen Dank für eure Hilfe,
Gruß,
Megara
Ich versuche mich gerade an der Simulation "Bouncing Balls" in Java. Ich bin gerade dabei den Code für das JPanel vom Code für das JFrame zu trennen also in zwei unterschiedliche Klassen zu schreiben. Das Programm besitzt eine Kollisionserkennung aber noch keine Kollisionsantwort. Die soll noch folgen. Mein Problem liegt darin dass ich die Bälle nicht auf dem Panel gezeichnet bekomme.
Quellcode:
Ball.java
Code:
package figures;
import gui.BallPanel;
import java.awt.Color;
import java.awt.Graphics2D;
/**
*
*/
public class Ball {
private final float coefficient = 100.0f;
private final float mapFactorX = BallPanel.panelWidth / coefficient;
private final float mapFactorY = BallPanel.panelHeight / coefficient;
private float centerX = 0.0f;
private float centerY = 0.0f;
private float radius = 0.0f;
private String name = null;
private Ball[] myBallArray = BallPanel.myBallArray;
private float dCenterX = 0.0f;
private float dCenterY = 0.0f;
private float dRadius = 0.0f;
private boolean collision = false;
public float speedX = 0.0f;
public float speedY = 0.0f;
public Ball(float centerX, float centerY, float radius, String name) {
this.centerX = centerX;
this.centerY = centerY;
this.radius = radius;
this.name = name;
}
/**
* @param speedX the speedX to set
*/
public void setSpeedX(float speedX) {
this.speedX = speedX;
}
/**
* @param speedY the speedY to set
*/
public void setSpeedY(float speedY) {
this.speedY = speedY;
}
public void drawBall(Graphics2D g2d){
g2d.setColor(Color.ORANGE);
g2d.fillOval((int)((centerX-radius)*mapFactorX), (int)((centerY-radius)*mapFactorY),
(int)(radius*mapFactorX*2), (int)(radius*mapFactorY*2));
g2d.setColor(Color.BLACK);
g2d.drawOval((int)((centerX-radius)*mapFactorX), (int)((centerY-radius)*mapFactorY),
(int)(radius*mapFactorX*2), (int)(radius*mapFactorY*2));
}
public void update() {
centerX += speedX;
centerY += speedY;
checkBorderCollision();
}
private void checkBorderCollision() {
if((centerX+radius) > 100) {
speedX = -Math.abs(speedX);
}
if((centerX-radius) < 0) {
speedX = Math.abs(speedX);
}
if((centerY+radius) > 100) {
speedY = -Math.abs(speedY);
}
if((centerY-radius) < 0) {
speedY = Math.abs(speedY);
}
}
public void checkBallCollision() {
for (int i = 0; i < myBallArray.length; i++) {
if((myBallArray[i] != this)){
dCenterX = myBallArray[i].centerX - this.centerX;
dCenterY = myBallArray[i].centerY - this.centerY;
dRadius = (myBallArray[i].radius * myBallArray[i].radius) + (this.radius * this.radius);
collision = ((dCenterX * dCenterX) + (dCenterY * dCenterY)) <= dRadius ? true : false;
if(collision)
System.out.println(this.name + " kolliderte mit " + myBallArray[i].name);
}
}
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Ball [name=" + name + " radius=" + radius + "]";
}
}
Board.java
Code:
/**
*
*/
package gui;
import javax.swing.JFrame;
public class Board extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1440466626287498855L;
private final float fps = 60.0f;
private float dt = 1/fps;
private long currentTime = System.currentTimeMillis();
private float accumulator = 0.0f;
private long newTime = 0;
private float frameTime = 0.0f;
private float sleepTime = 0.0f;
private BallPanel myBallPanel = null;
public final static int width = 500;
public final static int height = 500;
public Board(String title) {
this.setTitle(title);
this.setSize(width, height);
this.setLocation(100,100);
this.setResizable(false);
myBallPanel = new BallPanel();
this.add(myBallPanel);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
public void GameLoop() {
while(true) {
newTime = System.currentTimeMillis();
frameTime = (newTime - currentTime)/1000.0f;
sleepTime = (dt - frameTime > 0) ? (dt - frameTime)*1000 : 0;
if(frameTime > 0) {
currentTime = newTime;
accumulator += frameTime;
}
while(accumulator >= dt) {
accumulator -= dt;
for (int i = 0; i < BallPanel.myBallArray.length; i++)
BallPanel.myBallArray[i].update();
for (int i = 0; i < BallPanel.myBallArray.length; i++)
BallPanel.myBallArray[i].checkBallCollision();
}
System.out.println("Alles neu macht der Winter");
myBallPanel.repaint();
try {
Thread.sleep((long) sleepTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
BallPanel.java
Code:
/**
*
*/
package gui;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JPanel;
import figures.Ball;
public class BallPanel extends JPanel {
/**
*
*/
private static final long serialVersionUID = -4316082159776838848L;
private final static int maxBalls = 5;
private Graphics2D g2d = null;
private float centerX = 0.0f;
private float centerY = 0.0f;
private float radius = 0.0f;
private float speedX = 0.0f;
private float speedY = 0.0f;
public static Ball[] myBallArray = new Ball[maxBalls];
public static int panelWidth = 0;
public static int panelHeight = 0;
public BallPanel() {
setVisible(true);
Setup();
panelWidth = this.getWidth();
panelHeight = this.getHeight();
}
private double myRandom(double low, double high) {
return Math.random() * (high - low) + low;
}
public void Setup() {
for (int i = 0; i < myBallArray.length; i++) {
centerX = (float)(Math.round(10.0 * myRandom(0.0, 5.0)) / 10.0);
centerY = (float)(Math.round(10.0 * myRandom(0.0, 5.0)) / 10.0);
radius = (float)(Math.round(10.0 * myRandom(3.0, 5.0)) / 10.0);
speedX = (float)(Math.round(10.0 * myRandom(0.0, 1.0)) / 10.0);
speedY = (float)(Math.round(10.0 * myRandom(0.0, 1.0)) / 10.0);
myBallArray[i] = new Ball(centerX, centerY, radius, "Ball " + i);
myBallArray[i].setSpeedX(speedX);
myBallArray[i].setSpeedY(speedY);
System.out.println(myBallArray[i].toString() + " wurde erstellt");
}
}
@Override
public void paintComponent(Graphics g) {
g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.clearRect(0, 0, this.getWidth(), this.getHeight());
g2d.setColor(Color.WHITE);
g2d.fillRect(0, 0, this.getWidth(), this.getHeight());
System.out.println("Ich male");
for (int i = 0; i < myBallArray.length; i++) {
myBallArray[i].drawBall(g2d);
System.out.println(myBallArray[i].toString() + " wird gemalt");
}
}
}
Main.java
Code:
import gui.Board;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Board myBouncingBallsFrame = new Board("Bouncing Balls");
myBouncingBallsFrame.GameLoop();
}
}
Vielen Dank für eure Hilfe,
Gruß,
Megara