【问题标题】:gif animation flickering in appletgif动画在小程序中闪烁
【发布时间】:2014-06-11 19:50:21
【问题描述】:

我的 gif 动画闪烁太多。我听说过双缓冲可以提供帮助,但我该如何对我的 gif 动画执行此操作?或者有没有更好更快的捷径。这只是我为了好玩而做的一个小测试小程序,但会在课堂上实施课程。

上下文:

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

}

    }

【问题讨论】:

  • 1) 为什么要编写小程序?如果是由于规范。老师请发给Why CS teachers should stop teaching Java applets。 2) 为什么选择 AWT 而不是 Swing?请参阅我在 Swing extras over AWT 上的回答,有很多放弃使用 AWT 组件的充分理由。
  • @Andrew 我是个初学者,我想使用小程序很有趣 LOLOL 感谢您提供有趣阅读的链接。

标签: java animation applet gif flicker


【解决方案1】:

你想:

  • 创建一个扩展 JPanel(或 JComponent)的绘图类
  • 覆盖类的paintComponent(Graphics g) 方法。
  • 在这个方法中先调用super的方法
  • 然后用这种方法绘制动画。
  • 这将为您提供 Swing 的自动双缓冲,这将有助于平滑您的动画。
  • 通过将绘图 JPanel 添加到小程序的 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();
      }
   }
}

【讨论】:

    【解决方案2】:

    嗯,哈哈,我通过使用更新让它停止闪烁 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);
    }
    
    }
    

    【讨论】:

    • 你们对你们没有多大帮助。一定要自力更生哟!
    猜你喜欢
    • 2013-09-26
    • 2010-10-28
    • 2019-11-30
    • 1970-01-01
    • 2012-01-12
    • 2019-12-19
    • 2011-12-02
    • 2012-03-12
    • 2011-03-30
    相关资源
    最近更新 更多