Home >Java >javaTutorial >How to Correctly Center a Rectangle within a Java Frame?

How to Correctly Center a Rectangle within a Java Frame?

Barbara Streisand
Barbara StreisandOriginal
2024-12-07 12:01:13365browse

How to Correctly Center a Rectangle within a Java Frame?

Centering a Rectangular in Java

Your attempt to draw a rectangular in Java and center it with specific dimensions may face a misalignment issue. This occurs due to the frame's decorations (border and title bar) encroaching upon the paint area.

The fix involves painting onto the frame's content area instead. This excludes the frame's decorations from the paint area, resulting in a correctly centered rectangle.

Here's a code snippet illustrating the corrected method:

public class CenterOfFrame {
    
    public static void main(String[] args) {
        new CenterOfFrame();
    }
    
    public CenterOfFrame() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                new BadFrame().setVisible(true);

                JFrame goodFrame = new JFrame();
                goodFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                goodFrame.setContentPane(new PaintablePane());
                goodFrame.pack();
                goodFrame.setLocationRelativeTo(null);
                goodFrame.setVisible(true);
            }
        });
    }
    
    public class BadFrame extends JFrame {
        public BadFrame() {
            setSize(800, 400);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
        }
        @Override
        public void paint(Graphics g) {
            super.paint(g);
            paintTest(g, getWidth() - 1, getHeight() - 1);
        }
    }
    
    public void paintTest(Graphics g, int width, int height) {
        g.setColor(Color.RED);
        g.drawLine(0, 0, width, height);
        g.drawLine(width, 0, 0, height);
        g.drawRect(50, 50, width - 100, height - 100);
    }
    
    public class PaintablePane extends JPanel {
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(800, 400);
        }
        
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            paintTest(g, getWidth() - 1, getHeight() - 1);
        }
    }
}

This code provides an example of how to draw a rectangular centered within a frame, taking into account the frame's decorations.

The above is the detailed content of How to Correctly Center a Rectangle within a Java Frame?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn