如何制作小程序动画?

发布于 2024-08-23 06:19:40 字数 1756 浏览 4 评论 0原文

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

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

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

    }

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

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

好菇凉咱不稀罕他 2024-08-30 06:19:40

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

阅读 Swing 教程。从“并发”(了解 EDT 如何工作)和“如何使用计时器”部分开始。

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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文