Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JFrame on OS X with buffer strategy disables rounded corners

I'm trying to create a simple JFrame window in OS X and render a simple black square on it like so using 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();
    }
}

However the window doesyn't seem to have rounded corners at the bottom when I use strategy.show(); :

With strategy.show

Rendering it without the buffer strategy, i.e: Graphics2D = (Graphics2D) window.getGraphics(); produces a window with rounded corners:

enter image description here

I know it's a really small problem but it's annoying none the less. Any way to fix this?

like image 583
Luke Avatar asked Oct 07 '22 21:10

Luke


1 Answers

Works fine for me.

enter image description here

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();            
    }
}

Now, I have no idea what it is you're trying to, but I can tell you this:

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();
    }
}

Is a bad idea. Firstly as SoboLAN pointed out, you're trying to update a UI component out side the Event Dispatching Thread. Swing components are not Thread safe.

Secondly, this loop will eventually chew up your CPU cycles, making you application unusable as well as you overall system.

UPDATED with Animation

Here, try this. This is a really BASIC example ;)

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();
    }
}
like image 107
MadProgrammer Avatar answered Oct 23 '22 11:10

MadProgrammer