加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 具有缓冲策略的OS X上的JFrame禁用圆角

发布时间:2020-12-14 19:20:50 所属栏目:Java 来源:网络整理
导读:我正在尝试在OS X中创建一个简单的JFrame窗口,并使用Graphics2d在其上呈现一个简单的黑色方块: public Start() { running = true; window = new JFrame("Finest Hour"); window.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE); window.setPreferredSize(n

我正在尝试在OS X中创建一个简单的JFrame窗口,并使用Graphics2d在其上呈现一个简单的黑色方块:

public Start() { 
    running = true;
    window = new JFrame("Finest Hour");
    window.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
    window.setPreferredSize(new Dimension(500,500));
    window.setSize(window.getPreferredSize());
    FullScreenUtilities.setWindowCanFullScreen(window,true);
    window.setIgnoreRepaint(true);
    window.setVisible(true);
    window.createBufferStrategy(2);
    strategy = window.getBufferStrategy();
}

public static void main(String[] args) {
    Start s = new Start();
    s.loop();
}

public void loop() {
    Random random = new Random();
    while(running) {
        Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
        g.setColor(Color.black);
        g.fillRect(20,20,40,40);
        strategy.show();
    }
}

然而,当我使用strategy.show()时,窗口似乎在底部没有圆角. :

无缓冲策略渲染它,即:Graphics2D =(Graphics2D)window.getGraphics();生产一个圆角窗口:

我知道这是一个非常小的问题,但它仍然很烦人.有任何解决这个问题的方法吗?

最佳答案
对我来说很好.

public class TestMacFrame extends JFrame {

    public TestMacFrame() throws HeadlessException {            
        super("Testing");
        setSize(200,200);
        setLocationRelativeTo(null);
        setDefaultCloSEOperation(EXIT_ON_CLOSE);            
        setLayout(new BorderLayout());
        add(new PaintPane());            
        setVisible(true);
    }

    protected class PaintPane extends JPanel {

        @Override
        protected void paintComponent(Graphics g) {    
            super.paintComponent(g);                
            int width = getWidth() - 1;
            int height = getHeight() - 1;                
            g.setColor(Color.BLACK);                
            int blockWidth = width / 2;
            int blockHeight = height / 2;                
            int x = (width - blockWidth) / 2;
            int y = (height - blockHeight) / 2;                
            g.fillRect(x,y,blockWidth,blockHeight);                
        }            
    }

    public static void main(String[] args) {    
        new TestMacFrame();            
    }
}

现在,我不知道你想要的是什么,但我可以告诉你:

public void loop() {
    Random random = new Random();
    while(running) {
        Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
        g.setColor(Color.black);
        g.fillRect(20,40);
        strategy.show();
    }
}

是个坏主意.首先,正如SoboLAN指出的那样,您正试图在Event Dispatching Thread外侧更新UI组件.Swing组件不是Thread安全的.

其次,这个循环最终会破坏你的CPU周期,使你的应用程序和整个系统都无法使用.

动画更新

在这里,试试这个.这是一个非常基本的例子;)

public class TestMacFrame extends JFrame {

    private float angel = 0;
    private Timer timer;

    public TestMacFrame() throws HeadlessException {
        super("Testing");
        setSize(200,200);
        setLocationRelativeTo(null);
        setDefaultCloSEOperation(EXIT_ON_CLOSE);
        setLayout(new BorderLayout());
        add(new PaintPane());
        setVisible(true);

        timer = new Timer(25,new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                angel += 1;
                repaint();
            }
        });

        timer.setRepeats(true);
        timer.setCoalesce(true);
        timer.setInitialDelay(0);
        timer.start();
    }

    protected class PaintPane extends JPanel {

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            Graphics2D g2d = (Graphics2D) g.create();

            int width = getWidth() - 1;
            int height = getHeight() - 1;

            g.setColor(Color.BLACK);

            int blockWidth = width / 2;
            int blockHeight = height / 2;

            int x = (width - blockWidth) / 2;
            int y = (height - blockHeight) / 2;

            g2d.setTransform(AffineTransform.getRotateInstance(Math.toRadians(angel),width / 2,height / 2));
            g2d.fillRect(x,blockHeight);
            g2d.dispose();

        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        new TestMacFrame();
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读