这是用于通过布雷森纳姆算法在计算出的位置上绘制点的代码:
public void drawBresenhamPoints(Graphics2D g2, List<Point> bresenham) throws InterruptedException
{
Graphics2D g = (Graphics2D) g2;
if(bresenham == null)
return;
g.setColor(Color.DARK_GRAY);
for(int i = 0; i < bresenham.size(); i = i+20)
{
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);
// delay
try
{
Thread.sleep(10);
}
catch(Throwable e)
{
System.out.println(e.getMessage());
}
}
}
列表“布雷森汉姆”包含所有借助布雷森汉姆线描算法预先计算的点。我想在“ for”循环中设置1秒的延迟,以便在间隔1秒后绘制每个点。 “延迟”部分中列出的部分无效。如何使“延迟”工作?
更具体地说,我希望每隔1秒在屏幕上一一绘制所有点。
最佳答案
我假设您正在paint/paintComponent
方法中调用此方法。
只是一个指针:从来没有睡过油漆过程
而是将javax.swing.Timer
用于重复的任务。我会做的是
有两个Lists
。您的List<Point> bresenham
和另一个List<Point> paintList
。 bresenham
将保存您的数据,而paintList
最初将为空。
使用paintList
绘制点
@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);
}
}
尽管
paintList
最初没有任何内容,但是每次触发计时器事件时,您都会在列表中添加一个新的Point
。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();
构造函数的基本计时器是
delay
,它是“迭代”之间的时间延迟,它是侦听器中的第二个参数,它实际上侦听每delay
毫秒触发的计时器事件。因此,上面的代码基本上是将Point
添加到从paintList
列表中获取的bresenham
中,然后删除称为repaint
的paintComponent
项。当列表为空时,计时器将停止。更新
这是一个完整的运行示例
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);
}
});
}
}