Java的慢2D性能 - 调整大小 [英] Java slow 2D performance - Resizing

查看:203
本文介绍了Java的慢2D性能 - 调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Windows 7的Aero,并有一个非常快速的图形卡(的Radeon 6870),我使用的游戏。

I am using Windows 7 with Aero, and have a very fast graphics card (Radeon 6870) that I use for gaming.

调整非常简单的程序让我用java的时候我有一些问题。例如,这个程序也绝对没有什么。它没有采取行动的听众,没有循环。它只是带有按钮的GUI界面。

I have some issues when resizing very simple programs I make with java. For instance, this program does absolutely nothing. It has no action listeners, no loops. It's simply a GUI interface with buttons.

[查看全屏]

[View fullscreen]

大约需要一秒钟来调整大小的组件。对我来说,这是非常明显的。

It takes about a second to resize the components. For me that's very noticeable.

我试图让OpenGL加速来解决这个问题。我编的JAR和 与运行的Java -Dsun.java2d.opengl =真-jar C:\ Test.jar的。其结果是略少黑色窗口周围地区,但更闪烁。事实上闪烁显示为灰色在上面的截图。

I have tried to enable OpenGl acceleration to solve this problem. I compiled the JAR and run it with java -Dsun.java2d.opengl=true -jar C:\Test.jar. The result is slightly less black areas around the window, but much more flickering. In fact the flickering shows up as grey in the screenshot above.

没有。 Eclipse中,Netbeans的,Chrome浏览器和其他应用程序进行了检验。无有这个问题。 Therfore我必须得出结论,必须有一些问题与code。很多人已经运行此code和表示,他们没有问题。如果你要测试它,请确保您调整从最小尺寸的窗口到屏幕的最大尺寸,同时移动鼠标的圆周运动。

No. Eclipse, Netbeans, Chrome and other applications have been tested. None have this issue. Therfore I must conclude that there must be some problem with the code. Various people have run this code and said they have "No issues". If you are going to test it, please make sure you resize the window from the smallest size to the largest size of the screen, whilst moving the mouse in circular motion.

import java.awt.*;
import javax.swing.*;

public class JFrameWithButtonsTest {
    private int iScreen = 25;  
    private int iLocation = 10;
    JFrame frame = new JFrame();
    Container contentPane = frame.getContentPane();

    public JFrameWithButtonsTest() {
        JPanel northButtonPanel = new JPanel();
        northButtonPanel.setLayout(new GridLayout(2,2));
        northButtonPanel.add(new JButton(" I do nothing"));
        northButtonPanel.add(new JButton(" I do nothing"));
        northButtonPanel.add(new JButton(" I do nothing"));
        northButtonPanel.add(new JButton(" I do nothing"));
        contentPane.add(northButtonPanel, BorderLayout.NORTH);

        JPanel southButtonPanel = new JPanel();
        southButtonPanel.setLayout(new GridLayout(2,2));
        southButtonPanel.add(new JButton(" I do nothing"));
        southButtonPanel.add(new JButton(" I do nothing"));
        southButtonPanel.add(new JButton(" I do nothing"));
        southButtonPanel.add(new JButton(" I do nothing"));
        contentPane.add(southButtonPanel, BorderLayout.SOUTH);

        JPanel eastButtonPanel = new JPanel();
        eastButtonPanel.setLayout(new GridLayout(2,2));
        eastButtonPanel.add(new JButton(" I do nothing"));
        eastButtonPanel.add(new JButton(" I do nothing"));
        eastButtonPanel.add(new JButton(" I do nothing"));
        eastButtonPanel.add(new JButton(" I do nothing"));
        contentPane.add(eastButtonPanel, BorderLayout.EAST);

        boolean packFrame = false;
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize = frame.getSize();
        frameSize.height = (int) (iScreen * screenSize.height / 100);
        frameSize.width = (int) (iScreen * screenSize.width / 100);
        frame.setSize(frameSize);
        frame.setLocation((screenSize.width - frameSize.width) / iLocation,
                (screenSize.height - frameSize.height) / iLocation);
        if (packFrame) {
            frame.pack();
            packFrame = true;
        } else {
            frame.validate();
        }
        frame.setVisible(true);
    }
    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new JFrameWithButtonsTest();
            }
        });
    }
}

请注意,这个问题仍是present没有这行: UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

Note that the issue is still present without this line: UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

推荐答案

这招提高重绘速度在Win7 +航空:调整大小设置为null,并提供自己的调整大小挂钩。其AINT完美的,但还是好多了。检查我的例子:

This trick improves repaint rate in Win7+Aero: setting resizable to null, and providing own resize hook. Its aint perfect, but still alot better.. check my example:

http://i.stack.imgur.com/IrPAb.png

import java.awt.event.*;
import java.awt.*;
import javax.swing.event.*;
import javax.swing.*;

class ResizeHookDemo extends JDialog {
  private final static int width = 580, height = 350;
  private final JFileChooser fc;
  private java.awt.geom.GeneralPath gp;

  public ResizeHookDemo() {
    super((JDialog)null, "Choose File", true);

    fc = new JFileChooser() {

     @Override
     public void paint(Graphics g) {
       super.paint(g);
       int w = getWidth();
       int h = getHeight();
       g.setColor(new Color(150, 150, 150, 200));
       g.drawLine(w-7, h, w, h-7);
       g.drawLine(w-11, h, w, h-11);
       g.drawLine(w-15, h, w, h-15);

       gp = new java.awt.geom.GeneralPath();      
       gp.moveTo(w-17, h);
       gp.lineTo(w, h-17);
       gp.lineTo(w, h);
       gp.closePath();
     }

    };
    fc.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("CancelSelection")) {
          setVisible(false);
          // action...
        }
        else if (e.getActionCommand().equals("ApproveSelection")) {
          setVisible(false);
          // action...
        }
      }
    });

    MouseInputListener resizeHook = new MouseInputAdapter() {
      private Point startPos = null;

      public void mousePressed(MouseEvent e) {
        if (gp.contains(e.getPoint())) 
          startPos = new Point(getWidth()-e.getX(), getHeight()-e.getY());
      }

      public void mouseReleased(MouseEvent mouseEvent) {
        startPos = null;
      }

      public void mouseMoved(MouseEvent e) {
        if (gp.contains(e.getPoint()))
          setCursor(Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR));
        else
          setCursor(Cursor.getDefaultCursor());
      }

      public void mouseDragged(MouseEvent e) {
        if (startPos != null) {

          int dx = e.getX() + startPos.x;
          int dy = e.getY() + startPos.y;

          setSize(dx, dy);
          repaint();
        }
      }         
    };

    fc.addMouseMotionListener(resizeHook);
    fc.addMouseListener(resizeHook);
    fc.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 20));
    add(fc);

    setResizable(false);

    setMinimumSize(new Dimension(width, height));
    setDefaultCloseOperation(HIDE_ON_CLOSE);
    setLocationRelativeTo(null);
  }

  public static void main(String args[]) {
    System.out.println("Starting demo...");
    SwingUtilities.invokeLater(new Runnable() {

      @Override
      public void run() {
        new ResizeHookDemo().setVisible(true);
      }
    });
  }
}

这篇关于Java的慢2D性能 - 调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆