繁体   English   中英

gif动画在applet中闪烁

[英]gif animation flickering in applet

我的gif动画闪烁太多。 我听说过双缓冲可以提供帮助,但是如何对gif动画进行缓冲呢? 还是有更快更好的捷径。 这只是我为取乐而做的一个小测试applet,但是将在课堂上实施这些课程。

语境:

import java.net.*;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.*;
public class HiroshimaBlock extends Applet implements ActionListener {

TextField distanceText = new TextField(10);
TextField accelerationText = new TextField(10);
Button security = new Button("Account Manager");
Button launch = new Button("LAUNCH!");
Button Reportl = new Button("Report Logs");
Image dancer;
URL base;
MediaTracker mt;
Timer tm = new Timer(10, this);

TextArea answers = new TextArea("I am ready for your first trip.", 4, 20,
        TextArea.SCROLLBARS_NONE);

Image image;

@Override
public void init() {
    setSize(550, 500);
    // Some messages for the top of the Applet:
    addHorizontalLine(Color.orange);

    addNewLine();
    // JOptionPane.showMessageDialog(null, "HiroshimaBlock",
    // "Welcome to HiroshimaBlock", JOptionPane.PLAIN_MESSAGE);
    // The two text fields and the launch button:
    Frame c = (Frame) this.getParent().getParent();
    c.setTitle("HiroshimaBlock");

    mt = new MediaTracker(this);
    try {
        base = getDocumentBase();
    } catch (Exception e) {
    }
    dancer = getImage(base, "dancer1.gif");
    mt.addImage(dancer, 9);
    try {
        mt.waitForAll();
    } catch (InterruptedException e) {
    }

    loadImage();
    // add(distanceText);
    // add(new Label("Distance of trip in light years"));
    addNewLine();
    addNewLine();
    // add(accelerationText);
    // add(new Label("Acceleration of rocket in g's"));
    addNewLine();
    add(launch);
    addNewLine();
    add(security);
    addNewLine();
    add(Reportl);

    // A text area for printing the answers:
    // answers.setEditable(false);
    // add(answers);
    addNewLine();
    addNewLine();
    addHorizontalLine(Color.orange);

}

public void loadImage() {

    URL url = getClass().getResource("hsblock.png");
    image = getToolkit().getImage(url);
}

public void paint(Graphics g) {
    g.drawImage(image, 20, 20, this);

    this.security.setLocation(25, 200);
    addNewLine();
    this.launch.setLocation(25, 230);
    this.Reportl.setLocation(25, 260);
    this.security.setSize(100, 25);
    this.launch.setSize(100, 25);
    this.Reportl.setSize(100, 25);

    g.drawImage(dancer, 150, 200, this);
    tm.start();
}

private void addHorizontalLine(Color c) {
    // Add a Canvas 10000 pixels wide but only 1 pixel high, which acts as
    // a horizontal line to separate one group of components from the next.
    Canvas line = new Canvas();
    line.setSize(10000, 1);
    line.setBackground(c);

    add(line);
}

private void addNewLine() {
    // Add a horizontal line in the background color. The line itself is
    // invisible, but it serves to force the next Component onto a new line.
    addHorizontalLine(getBackground());
}

@Override
public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub

}

    }

你想要:

  • 创建一个扩展JPanel(或JComponent)的图形类
  • 重写您的类的paintComponent(Graphics g)方法。
  • 首先在该方法中调用super的方法
  • 然后使用这种方法绘制动画。
  • 这将为您提供Swing的自动双缓冲功能,这应有助于使动画平滑。
  • 通过将其添加到小程序的contentPane中,在JApplet中显示图形JPanel。

例如:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.lang.reflect.InvocationTargetException;
import java.nio.Buffer;

import javax.swing.*;

public class SimpleAnimation extends JApplet {
   @Override
   public void init() {
      try {
         SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
               DrawPanel drawPanel = new DrawPanel();
               getContentPane().add(drawPanel);
            }
         });
      } catch (InvocationTargetException | InterruptedException e) {
         e.printStackTrace();
      }
   }
}

class DrawPanel extends JPanel {
   private static final int I_WIDTH = 20;
   private static final int I_HEIGHT = 20;
   private static final int TIMER_DELAY = 15;
   private int x = 0;
   private int y = 0;
   private BufferedImage img = new BufferedImage(I_WIDTH, I_HEIGHT, BufferedImage.TYPE_INT_ARGB);

   public DrawPanel() {
      Graphics2D g2 = img.createGraphics();
      g2.setColor(Color.red);
      g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
      g2.fillOval(1, 1, I_WIDTH - 2, I_HEIGHT - 2);
      g2.dispose();

      new Timer(TIMER_DELAY, new TimerListener()).start();
   }

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      if (img != null) {
         g.drawImage(img, x, y, this);
      }
   }

   private class TimerListener implements ActionListener {
      @Override
      public void actionPerformed(ActionEvent e) {
         x++;
         y++;
         repaint();
      }
   }
}

好吧,大声笑我通过使用更新LOLOLOL使其停止闪烁

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class Test extends Applet {

Image img;

public void init() {
    setSize(700, 700);

    img = getImage(getDocumentBase(), "dancer1.gif");

}

public void update(Graphics g) {
    g.drawImage(img, 140, 200, this);
}

public void paint(Graphics g) {
    update(g);
}

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM