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

查看:44
本文介绍了延迟在 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.

只是一个指针:永远不要让绘画过程休眠

改为使用 javax.swing.Timer 用于重复任务.要做的是

  • 有两个Lists.您的 Listbresenham 和另一个 List油漆列表.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();
    

    构造函数的基本定时器首先是 delay,它是 迭代" 和实际监听定时器的侦听器中的第二个参数之间的延迟时间每 delay 毫秒触发的事件.所以上面的代码基本上做的是将一个 Point 添加到取自 bresenham 列表的 paintList 中,然后删除 repaint 项 调用 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.

    更新

    这是一个完整的运行示例

    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天全站免登陆