Guten Tag,
ich bin momentan dabei mir die Grafikprogrammierung mit swing und awt anzueignen und bin da auch direkt auf ein Problem gestoßen.
Mein derzeitiges Projekt ist die Visualisierung der "Diffusion Limited Aggregation".
Es werden je nach Wunsch n-Teilchen erzeugt welche sich durch Brownsche Bewegung in ein Fraktal einfinden sollen.
Die Animation ist jedoch sehr rucklig/"flackerig". Wie könnt ich dies Beheben?
MfG Aljosha
DLA-Class
Dot-Class
ich bin momentan dabei mir die Grafikprogrammierung mit swing und awt anzueignen und bin da auch direkt auf ein Problem gestoßen.
Mein derzeitiges Projekt ist die Visualisierung der "Diffusion Limited Aggregation".
Es werden je nach Wunsch n-Teilchen erzeugt welche sich durch Brownsche Bewegung in ein Fraktal einfinden sollen.
Die Animation ist jedoch sehr rucklig/"flackerig". Wie könnt ich dies Beheben?
MfG Aljosha
DLA-Class
Code:
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.util.List;
/**
* DLA = Diffusion Limited Aggregation
*
* @author Aljosha Kubon
* @version 03.04.2018
*/
public class DLA extends JFrame
{
private int numberDots = 100;
private int radius = 20;
private List<Dot> dots = new ArrayList<Dot>();
private boolean first = true;
private Random r;
/**
* Konstruktor für Objekte der Klasse DLA
*/
public DLA()
{
setSize(500,500);
setLocationRelativeTo(null);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
for(int i = 0; i < numberDots; i++){
dots.add(new Dot(this.getWidth(),this.getHeight()));
}
// System.out.println("test");
}
public void paint(Graphics g){
g.setColor(Color.black);
g.fillRect(0,0, this.getWidth(), this.getHeight());
g.setColor(Color.blue);
for(int i = 0; i < numberDots; i++){
g.fillOval(dots.get(i).x, dots.get(i).y, radius, radius);
collision(dots.get(i));
movement(dots.get(i));
}
repaint();
}
public void movement(Dot dot){
if(dot.active){
int deltaX = (int)(Math.round((Math.random())*2)-1);
int deltaY = (int)(Math.round((Math.random())*2)-1);
if( (dot.getX() + deltaX) > 0 && (dot.getX() + deltaX) < this.getWidth()){
dot.setX(dot.getX() + deltaX);
}
if( (dot.getY() + deltaY) > 0 && (dot.getY() + deltaY) < this.getHeight()){
dot.setY(dot.getY() + deltaY);
}
}
System.out.println("X: "+dot.getX()+" Y: "+dot.getY());
}
public void collision(Dot dot){
for(int i = 0; i < numberDots; i++){
if(dot != dots.get(i)){
int D = (int)Math.sqrt(( (dots.get(i).x - dot.x)^2 + (dots.get(i).y - dot.y)^2 ));
if(D < radius){
dot.active = false;
}
}
}
}
}
Dot-Class
Code:
/**
* Beschreiben Sie hier die Klasse Dot.
*
* @author Aljosha Kubon
* @version 03.04.2018
*/
public class Dot
{
public int x;
public int y;
public boolean active = true;
/**
* Konstruktor für Objekte der Klasse Dot
*/
public Dot(int width, int height)
{
x = ((int) (Math.random()*width));
y = ((int) (Math.random()*height));
}
public int getX(){
return x;
}
public int getY(){
return y;
}
public void setX(int newX){
x = newX;
}
public void setY(int newY){
y = newY;
}
}