用Java绘图 - 为什么我的代码太慢了?

时间:2013-07-01 10:38:56

标签: performance graphics java-7

背景

我试图通过一些表盘惯性模拟来创建花哨,平滑和快速的模拟仪表。如果可能,我想避免使用OpenGL。

问题

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

我希望我的刻度盘在最短值(0)到最大值(1024,我可以改变它,但我需要平滑度)的时间短于0.5秒。

我试图测量重绘和paintComponent方法花费的时间来查找问题。

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

它似乎足够快(每秒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中即可编译。

1 个答案:

答案 0 :(得分:0)

您可以尝试以下方法:

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