Basics of Java based animation programming (II.)

Source: Internet
Author: User
Tags sin

Draw each frame:

The rest is to draw the image of each frame. In the example above, the applet's repaint () is invoked to draw each frame image.

public void paint(Graphics g) {
 g.setColor(Color.black);
 g.drawString("Frame " + frame, 0, 30);
}

To generate a graphic:

Now let's draw something slightly more difficult. The following example draws a combination of a sine curve, which, for each x, draws a short vertical line, all of which form a graph, and each frame changes.

But unfortunately some flashes, in the future we will explain why Flash and how to avoid it.

public void paint(Graphics g) {
 Dimension d = size();
 int h = d.height / 2;
 for (int x = 0 ; x < d.width; x++) {
  int y1 = (int)((1.0 + Math.sin((x - frame)*0.05))*h);
  int y2 = (int)((1.0 + math.sin((x + frame)*0.05))*h);
  g.DrawLine(x, y1, x, y2);
 }
}

Avoid flashing:

There are two reasons for flashing in the example above: it takes too long to draw each frame (because the amount of computation required when redrawing), and the entire background is cleared before each call to Pait (), and when the next frame is calculated, the user sees the background.

The short time between the background and the drawing is visible to the user and is blinking. Flashing on some platforms, such as PCs, is more pronounced than on X windows, because the X window's image is cached so that the blink time is relatively short.

There are two ways to significantly weaken flicker: overload update () or use double buffering.

Overload update ()?

When AWT receives a redraw request from an applet, it invokes the applet's update ().

By default, update () clears the background of the applet and then invokes paint (). Overload update () to include the drawing code that was previously in paint () in update () to avoid clearing the entire area each time it is redrawn.

Since the background is not automatically cleared, we need to do it ourselves in update (). We wiped the vertical bar alone before drawing a new line, completely eliminating the flicker.

public void paint(Graphics g) {
 update(g);
}
public void update(Graphics g) {
 Color bg = getBackground();
 Dimension d = size();
 int h = d.height / 2;
 for (int x = 0; x < d.width; x++) {
  int y1 = (int)((1.0 + Math.sin((x - frame)*0.05))*h);
  int y2 = (int)((1.0 + Math.sin((x + frame)*0.05))*h);
  if (y1 > y2) {
   int t = y1;
   y1 = y2;
   y2 = t;
  }
 g.setColor(bg);
 g.drawLine(x, 0, x, y1);
 g.drawLine(x, y2, x, d.height);
 g.setColor(Color.black);
 g.drawLine(x, y1, x, y2);
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.