延迟在java图形中不起作用 [英] Delay is not working in java graphics

查看:137
本文介绍了延迟在java图形中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是通过Bresenham算法在计算位置上绘制点的代码:

This is a code for drawing points on calculated locations by Bresenham's algorithm:

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()); 
         }
    }
}

'bresenham'列表包含所有在Bresenham线绘制算法的帮助下预先计算的点。我想在'for'循环内设置1秒的延迟,以便在1秒的间隔后绘制每个点。 延迟部分中列出的部分不起作用。如何使'延迟'工作?
更具体地说,我希望在屏幕上以1秒的间隔逐个显示所有点。

The list 'bresenham' contains all the points which are pre-calculated with the help of Bresenham's line drawing algorithm. I want to set a delay of 1 second inside the 'for' loop so that each and every point is drawn after an interval of 1 second. The portion listed in the 'delay' section doesn't work. How to make 'delay' work? More specifically, I want to see all the points being drawn one by one on the screen in an interval of 1 second.

推荐答案

我假设您在 paint / paintComponent 方法中调用此方法。

I'm assuming you're calling this method in a paint/paintComponent method.

Just一个指针:永远不会睡觉油漆过程

Just a pointer: Never ever ever sleep the paint process

而是使用 javax.swing.Timer 会做的是


  • 有两个列表。您的列表< Point> bresenham 和另一个列表< Point> paintList bresenham 将保留您的数据, paintList 最初将为空。

  • Have two Lists. Your List<Point> bresenham and another List<Point> paintList. The bresenham will hold your data, and the paintList will be initially empty.

使用 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

  • Though there's nothing initially in the paintList, you will add a new Point 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();
    

    构造函数的基本计时器是延迟,这是iterations之间延迟的时间,以及侦听器中实际侦听每个延迟毫秒。那么上面的代码基本上是将 Point 添加到从 bresenham取得的 paintList list,然后删除重绘的项目,该项目调用 paintComponent 。当列表为空时,计时器将停止。

    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 every delay milliseconds. So what the code above basically does is add a Point to the paintList taken from the bresenham list, then removes the item the repaint which calls the paintComponent. 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);
                }
            });
        }
    }
    

    这篇关于延迟在java图形中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆