用Java绘图——为什么我的代码如此缓慢



背景

我试图用一些表盘惯性模拟等来创建花哨、平滑、快速的模拟量表。如果可能的话,我想避免OpenGL。

问题

我的Java代码比我预期的要慢得多。

我希望我的表盘在0.5秒内从最小值(0)移动到最大值(1024,我可以更改,但我需要平滑度)。

我试图测量花在重新绘制和paintComponent方法上的时间来发现问题。

在我的机器上(Core Duo 2GHz,Windows 7),重新喷漆大约需要40 us,paintComponent需要300 us。

它似乎足够快(1/0.000340s=每秒约3000次"跑步")。

我认为视频卡是瓶颈,它减慢了我的代码,但我不知道该怎么办。

问题

如何使我的代码更快并尽可能保持动画流畅?

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Stroke;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import javax.swing.*;
public class Main extends JPanel {
   private static final Point2D CENTER = new Point2D.Double(PREF_W / 2.0,
         PREF_W / 2.0);
   private static final double RADIUS = PREF_W / 2.0;
   private static final Color LARGE_TICK_COLOR = Color.DARK_GRAY;
   private static final Color CENTER_HUB_COLOR = Color.DARK_GRAY;
   private static final Stroke LARGE_TICK_STROKE = new BasicStroke(4f);
   private static final Stroke LINE_TICK_STROKE = new BasicStroke(8f);
   private static final int LRG_TICK_COUNT = 18;
   private static final double TOTAL_LRG_TICKS = 24;
   private static final double LRG_TICK_OUTER_RAD = 0.9;
   private static final double LRG_TICK_INNER_RAD = 0.8;
   private static final int START_TICK = 10;
   private static final double CENTER_HUB_RADIUS = 10;
   private static final double DIAL_INNER_RAD = 0.00;
   private static final double DIAL_OUTER_RAD = 0.75;
   private static final Color DIAL_COLOR = Color.DARK_GRAY;
   private BufferedImage backgroundImg;

   private static final int PREF_W = 400; // 
   private static final int PREF_H = 400;
   private static final double INIT_VALUE = 0;
   public static final int MAX_VALUE = 1024; // resolution
   public static int delay = 1; // delay (ms) between value changes
   private double theta;
   private double cosTheta;
   private double sinTheta;
   private static long microtime;
   public Main() {
      setBackground(Color.white);
      backgroundImg = createBackgroundImg();
      setSpeed(INIT_VALUE);
   }
   public void setSpeed(double speed) {

     if (speed < 0) {
         speed = 0;
      } else if (speed > MAX_VALUE) {
         speed = MAX_VALUE;
      }
      this.theta = ((speed / MAX_VALUE) * LRG_TICK_COUNT * 2.0 + START_TICK)
            * Math.PI / TOTAL_LRG_TICKS;
      cosTheta = Math.cos(theta);
      sinTheta = Math.sin(theta);
      microtime = System.nanoTime()/1000;
      repaint();
      System.out.println("Repaint (us) = " + (System.nanoTime()/1000 - microtime));

   }
   private BufferedImage createBackgroundImg() {
      BufferedImage img = new BufferedImage(PREF_W, PREF_H,
            BufferedImage.TYPE_INT_ARGB);
      Graphics2D g2 = img.createGraphics();
      g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
      g2.setColor(LARGE_TICK_COLOR);
      g2.setStroke(LARGE_TICK_STROKE);
      for (double i = 0; i < LRG_TICK_COUNT; i++) {
         double theta = (i * 2.0 + START_TICK) * Math.PI / TOTAL_LRG_TICKS;
         double cosTheta = Math.cos(theta);
         double sinTheta = Math.sin(theta);
         int x1 = (int) (LRG_TICK_INNER_RAD * RADIUS * cosTheta + CENTER.getX());
         int y1 = (int) (LRG_TICK_INNER_RAD * RADIUS * sinTheta + CENTER.getY());
         int x2 = (int) (LRG_TICK_OUTER_RAD * RADIUS * cosTheta + CENTER.getX());
         int y2 = (int) (LRG_TICK_OUTER_RAD * RADIUS * sinTheta + CENTER.getY());
         g2.drawLine(x1, y1, x2, y2);
      }
      g2.setColor(CENTER_HUB_COLOR);
      int x = (int) (CENTER.getX() - CENTER_HUB_RADIUS);
      int y = (int) (CENTER.getY() - CENTER_HUB_RADIUS);
      int width = (int) (2 * CENTER_HUB_RADIUS);
      int height = width;
      g2.fillOval(x, y, width, height);

      g2.dispose();
      return img;
   }
   @Override
   protected void paintComponent(Graphics g) {
          System.out.println("Paint component (us) = " + (System.nanoTime()/1000 - microtime));
      super.paintComponent(g);
      if (backgroundImg != null) {
         g.drawImage(backgroundImg, 0, 0, this);
      }
      Graphics2D g2 = (Graphics2D) g;
      g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
      g2.setStroke(LINE_TICK_STROKE);

      g.setColor(DIAL_COLOR);
      int x1 = (int) (DIAL_INNER_RAD * RADIUS * cosTheta + CENTER.getX());
      int y1 = (int) (DIAL_INNER_RAD * RADIUS * sinTheta + CENTER.getY());
      int x2 = (int) (DIAL_OUTER_RAD * RADIUS * cosTheta + CENTER.getX());
      int y2 = (int) (DIAL_OUTER_RAD * RADIUS * sinTheta + CENTER.getY());
      g.drawLine(x1, y1, x2, y2);
      microtime = System.nanoTime()/1000;

   }
   @Override
   public Dimension getPreferredSize() {
      return new Dimension(PREF_W, PREF_H);
   }
   private static void createAndShowGui() {
      final Main mainPanel = new Main();
      JFrame frame = new JFrame("DailAnimation");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
      new Timer(delay, new ActionListener() {
         double speed = 0;
         @Override
         public void actionPerformed(ActionEvent evt) {
            speed ++;
            if (speed > Main.MAX_VALUE) {
                speed = 0;
            }
            mainPanel.setSpeed(speed);
         }
      }).start();
   }
   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

小代码描述:

有一个计时器,可以改变仪表值。计时器间隔由开头的delay变量定义。

这是完整的,一个文件的代码,你可以把它粘贴到IDE中并编译。

您可以尝试以下操作:

  • 从BufferedImage切换到VolatileImage
  • 预先计算sin和cos函数结果并将其存储在数组中
  • 切换到活动绘制而不是调用重新绘制

最新更新