如何制作小程序动画? [英] How to make applet animation?

查看:30
本文介绍了如何制作小程序动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试为数组制作小程序,包括插入/删除/搜索操作.

I try to make applet for array including operations of insertion/removal/search.

插入和删除很简单:一旦用户单击插入"或删除"按钮,只需更新数组,然后调用 repaint 重新绘制数组.
但是搜索不同,它是一个动画,一旦单击搜索按钮,我想从数组中的第一个元素开始,通过突出显示该元素来检查值.我有如下代码,但它只在最后一步(找到元素时)突出显示元素,它没有像我预期的那样突出显示每个元素,我不太熟悉小程序动画,任何人都可以帮助?谢谢.

For insertion and removal, it's easy: once user click the 'insert' or 'remove' button, just update the array, and call repaint to redraw the array.
However search is different, it's an animation, once the search button is clicked, I want to start from the first element in the array to check the value by high-light that element. I had the code as below, but it only high light the element at the last step (when the element is found), it doesn't high light each elements as i expected, I'm not quite familiar with applet animation, anyone can help? Thanks.

// index for search.
private searchIndex = -1;

 public boolean  search(int number) {
  boolean found = false;

  for (int i = 0; i < arr.getSize(); i++) {
   searchIndex = i;

   repaint();
   revalidate();

   if (arr.getElement(i) == number) {
    found = true;
    break;
   }

   try {
    Thread.sleep(3000);
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
  }

  return found;
 }

    public void paintComponent(Graphics g) {

        super.paintComponent(g);       
        Graphics2D g2;
        g2 = (Graphics2D) g;
        g2.setRenderingHint(
                RenderingHints.KEY_ANTIALIASING, 
                RenderingHints.VALUE_ANTIALIAS_ON);

        int xPos = START_X, yPos = START_Y;
        int width = CELL_WIDTH, height = CELL_HEIGHT;

        Font font = new Font("Serif", Font.BOLD, 12);
        g2.setFont(font);

        // draw array
        for(int i = 0; i < arr.getSize(); i++) {
 int element = arr.getElement(i);

 g2.setColor(Color.BLUE);
 g2.drawString(Integer.toString(element), xPos + OFFSET - 5, yPos + OFFSET + 5);

 g2.setColor(Color.BLACK);
 g2.drawRect(xPos, yPos, width, height);

 xPos += width;   
        }

        // high light the cell
         if (searchIndex > -1) {  
  xPos = START_X + (runSearch * CELL_WIDTH);

  g2.setColor(Color.blue);
  g2.fillRect(xPos, yPos, width, height);

  g2.setColor(Color.white);
  g2.drawString(Integer.toString(arr.getElement(searchIndex)), xPos + OFFSET - 5, yPos + OFFSET + 5);
 }

    }

推荐答案

因为 Thread.sleep() 导致 EDT 休眠,这意味着 GUI 在循环完成之前无法重新绘制自身.相反,您应该使用 Swing Timer,以便安排动画.

Because the Thread.sleep() is causing the EDT to sleep which means the GUI can't repaint itself until the loop finishes. Instead you should be using a Swing Timer so schedule the animation.

阅读Swing 教程.从并发"(以了解 EDT 的工作原理)和如何使用计时器"部分开始.

Read the Swing tutorial. Start with the sections on "Concurrency" (to understand how the EDT works) and on "How to Use Timers".

这篇关于如何制作小程序动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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