如何制作小程序动画?
我尝试为数组制作小程序,包括插入/删除/搜索操作。
对于插入和删除,很简单:一旦用户单击“插入”或“删除”按钮,只需更新数组,然后调用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为 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".