Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 带缓冲区策略的OS X上的JFrame禁用圆角_Java_Macos_Swing_Graphics_Graphics2d - Fatal编程技术网

Java 带缓冲区策略的OS X上的JFrame禁用圆角

Java 带缓冲区策略的OS X上的JFrame禁用圆角,java,macos,swing,graphics,graphics2d,Java,Macos,Swing,Graphics,Graphics2d,我试图在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));

我试图在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, 20, 40, 40);
        strategy.show();
    }
}
这是个坏主意。首先,正如SoboLAN所指出的,您正在尝试更新外部的UI组件。回转组件的螺纹不安全

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

用动画更新

来,试试这个。这是一个非常基本的例子;)


我建议在Swing的EDT中编写与GUI相关的代码:@SoboLAN这实际上是为了游戏动画,但我很好奇这是为什么。循环是因为我计划使用动画而不是GUI,因此使用缓冲区策略和setIgnoreRepain();,但是CPU周期真的会那么糟糕吗?我应该如何渲染它?尝试使用
java.swingx.Timer
来代替,或者看看其中一个动画帧作品,我想到了
Swing
@BubbaWoop的TimingFramework和Trident。我添加了一个非常基本的动画示例,所以我刚刚尝试了你原来的方法,结果是出现了方角。但是,在调整大小时(通过在角上拖动),我会得到圆角。我的java一定有问题,是时候更新编辑了:我还设法找到了为什么我的原始窗口没有圆角的问题是由于JFrame.setIgnoreRepainedit 2:我还发现,角如何变为正方形的真正原因是由于每当窗口或面板绘制时。如果我强制重画(),我又会得到正方形的角。
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, y, blockWidth, blockHeight);
            g2d.dispose();

        }
    }

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