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

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

问题描述

我尽量让小程序阵列,包括插入/删除/搜索操作。

有关插入和取出,很容易:一旦用户点击插入或删除按钮,只需更新数组,然后调用重绘重绘阵列结果。
但是搜索是不同的,这是一个动画,一旦搜索按钮后,我想从数组中的第一个元素开始检查由高光的元素的值。我有code如下,但只高光在最后一步(当元素被发现)的元素,它没有高光的每个元素为我所料,我不是很熟悉的动画小程序,任何人都可以帮忙吗?谢谢你。

  //索引搜索。
私人searchIndex = -1; 公共布尔搜索(INT编号){
  布尔发现= FALSE;  的for(int i = 0; I< arr.getSize();我++){
   sea​​rchIndex = I;   重绘();
   重新验证();   如果(arr.getElement(ⅰ)==数){
    发现= TRUE;
    打破;
   }   尝试{
    视频下载(3000);
   }赶上(InterruptedException的E){
    e.printStackTrace();
   }
  }  返回找到;
 }    公共无效的paintComponent(图形G){        super.paintComponent方法(G);
        Graphics2D的G2;
        G2 =(Graphics2D的)克;
        g2.setRenderingHint(
                RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);        INT XPOS = START_X,yPos = START_Y;
        INT宽度= CELL_WIDTH,身高= CELL_HEIGHT;        字体的字体=新Font(衬线,Font.BOLD,12);
        g2.setFont(字体);        //画数组
        的for(int i = 0; I< arr.getSize();我++){
 INT元= arr.getElement(I) g2.setColor(Color.BLUE);
 g2.drawString(Integer.toString(元素),XPOS + OFFSET - 5,yPos + OFFSET + 5); g2.setColor(Color.BLACK);
 g2.drawRect(XPOS,yPos,宽度,高度); XPOS + =宽度;
        }        //高亮度的单元格
         如果(searchIndex -1个){
  XPOS = START_X +(runSearch * CELL_WIDTH);  g2.setColor(Color.blue);
  g2.fillRect(XPOS,yPos,宽度,高度);  g2.setColor(Color.white);
  g2.drawString(Integer.toString(arr.getElement(searchIndex)),XPOS + OFFSET - 5,yPos + OFFSET + 5);
 }    }


解决方案

由于该Thread.sleep()方法是造成美国东部时间睡觉,这意味着GUI无法绘制本身,直到循环结束。相反,你应该使用Swing的计时器,这样安排的动画。

阅读 Swing指南。与并发的部分启动(要了解EDT如何运作)和如何使用定时器。

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

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

    }

解决方案

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.

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