I'm assuming you're calling this method in a paint/paintComponent
method.
Just a pointer: Never ever ever sleep the paint process
Instead use a javax.swing.Timer
for repeated tasks. What I would do is
Have two
Lists
. YourList<Point> bresenham
and anotherList<Point> paintList
. Thebresenham
will hold your data, and thepaintList
will be initially empty.Use the
paintList
to paint your points@override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; for (Point p : paintList) { int x = bresenham.get(i).x - pointWidth1/2; int y = bresenham.get(i).y - pointWidth1/2; int ovalW = pointWidth1; int ovalH = pointWidth1; g.fillOval(x, y, ovalW, ovalH); } }
Though there's nothing initially in the
paintList
, you will add a newPoint
to the list every firing of a timer event.Timer timer = new Timer(100, new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { if (bresenham.isEmpty()) { ((Timer)e.getSource()).stop(); } else { paintList.add(bresemham.get(0)); bresenham.remove(0); } repaint(); } }); timer.start();
The basic timer of the constructor is firs the
delay
, which is the time delayed between "iterations", and second argument in the listener that actually listens for the timer event that is fired everydelay
milliseconds. So what the code above basically does is add aPoint
to thepaintList
taken from thebresenham
list, then removes the item therepaint
which calls thepaintComponent
. When the list is empty, the timer will stop.
UPDATE
Here's a complete running example
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.Timer;
public class BresenhamPoints extends JPanel {
private static final int D_W = 500;
private static final int D_H = 500;
private List<Point> bresenhamList;
private List<Point> paintList;
public BresenhamPoints() {
bresenhamList = createRandomPoints();
paintList = new ArrayList<>();
Timer timer = new Timer(100, new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (bresenhamList.isEmpty()) {
((Timer) e.getSource()).stop();
} else {
paintList.add(bresenhamList.get(0));
bresenhamList.remove(0);
}
repaint();
}
});
timer.start();
}
private List<Point> createRandomPoints() {
Random rand = new Random();
List<Point> list = new ArrayList<>();
for (int i = 0; i < 100; i++) {
list.add(new Point(rand.nextInt(D_H), rand.nextInt(D_H)));
}
return list;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for (Point p : paintList) {
g.fillOval(p.x - 5, p.y - 5, 10, 10);
}
}
@Override
public Dimension getPreferredSize() {
return new Dimension(D_W, D_H);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame();
frame.add(new BresenhamPoints());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}