Hallo,
ich habe momentan 2 Bälle die sich auf einander zu bewegen.
Wie lässt sich die Farbe automatisch ändern wenn sich die Bälle überlappen und wie ändert sich die Farbe der Bälle in vorhergehenden Zunstand nach der Überlappung? Gibt es irgendwelche vorgefertige Funktion die eine Überlappung erkennt?
Viele Gruesse
ich habe momentan 2 Bälle die sich auf einander zu bewegen.
Code:
import java.awt.*;
import javax.swing.*;
public class BallRoom extends JApplet implements Runnable {
private final int WIDTH = 350;
private final int HEIGHT = 300;
private PaintSurface canvas;
Thread th;
public void init() {
this.setSize(WIDTH, HEIGHT);
canvas = new PaintSurface();
this.add(canvas, BorderLayout.CENTER);
}
public void start() {
th = new Thread(this);
th.start();
}
public void run() {
while (true){
repaint();
try {
Thread.sleep(100);
}
catch (InterruptedException ex){
}
}
}
public void update(Graphics g)
{
paint(g);
}
}
Code:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import javax.swing.JComponent;
class PaintSurface extends JComponent {
int ball1_x_pos = 0; // the starting X position
int ball1_y_pos = 150; // the starting Y position
int ball2_x_pos = 60; // the starting X position
int ball2_y_pos = 150; // the starting Y position
int d = 20; // the diameter of the ball
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
ball1_x_pos++; // move ball1 right one pixel
ball2_x_pos--; // move ball1 left one pixel
Shape ball1 = new Ellipse2D.Float(ball1_x_pos, ball1_y_pos, d, d);
Shape ball2 = new Ellipse2D.Float(ball2_x_pos, ball2_y_pos, d, d);
g2.setColor(Color.RED);
g2.fill(ball1);
g2.fill(ball2);
}
}
Wie lässt sich die Farbe automatisch ändern wenn sich die Bälle überlappen und wie ändert sich die Farbe der Bälle in vorhergehenden Zunstand nach der Überlappung? Gibt es irgendwelche vorgefertige Funktion die eine Überlappung erkennt?
Viele Gruesse