摇摆形式被冻结

时间:2014-03-17 21:06:56

标签: java swing

根据http://docs.oracle.com/javase/7/docs/technotes/guides/2d/flags.html#opengl
我将系统属性设置如下,以便JVM使用gpu opengl hw加速(如果可用) 没有崩溃,并且详细消息表明管道已激活OpenGL pipeline enabled for default config on screen 0

        public static void main(String args[]) {
        System.setProperty("sun.java2d.opengl", "True");
        try {
            UIManager.setLookAndFeel(new WindowsClassicLookAndFeel());
        } catch (Exception ex) {}
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new RootForm().setVisible(true);
            }
        });
        //no difference
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                new RootForm().setVisible(true);
//            }
//        });
        //no difference
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                new RootForm().setVisible(true);                
//            }
//        }).start();
    }

但是挥杆形式被冻结了!即使repaint()电话也无效 问题是什么?
平台:Win7X64,GPU:AMD HD5870(更新驱动程序) 提前谢谢。
修改
我试了好几次,然后就搞定了。没有新问题。它随机工作。很奇怪。随时工作。

1 个答案:

答案 0 :(得分:1)

对我来说很好......

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestOpenGL {

    public static void main(String args[]) {
        System.setProperty("sun.java2d.opengl", "True");
        new TestOpenGL();
    }

    public TestOpenGL() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private int yPos = 0;
        private int yDelta = 2;
        private JLabel label;

        public TestPane() {
            setLayout(new GridBagLayout());
            label = new JLabel("Bouncy, Bouncy...");
            add(label);
            Timer timer = new Timer(40, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    yPos += yDelta;
                    if (yPos > getHeight()) {
                        yPos = getHeight();
                        yDelta *= -1;
                    } else if (yPos < 0) {
                        yPos = 0;
                        yDelta *= -1;
                    }
                    label.setText("Bouncy, Bouncy...@ " + yPos);
                    repaint();
                }
            });
            timer.start();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setColor(Color.GREEN);
            int x = (getWidth() - 2) / 2;
            g2d.drawOval(x, yPos - 2, 4, 4);
            g2d.dispose();
        }
    }
}