How can I set in the midst?

丶灬走出姿态 提交于 2019-11-25 23:32:21

问题


I try to draw a rectangular in Java. I set the frame size (800,400) and resizable(false) rectangular\'s x = 50, y = 50 width = 700 height = 300. Why isn\'t it in the middle? Thank you.


回答1:


Without any evidence otherwise, I'd guess you've overriden the paint method of something like a JFrame and are painting directly to it.

The problem is, frames have decoration (a border and title bar for example), which takes up space inside the frame...

Technically, this is correct. The rectangle is painted in the center of frame, but the because of the frame's decorations, it looks like it's slightly high...

Instead, you should be painting onto the frame's content area instead.

Here the rectangle now looks correctly centered. In my tests, I set the first frame (bad) to 800x400, I made the second frame's content pane's preferred size 800x400, which made the frame size actually 816x438, as the frame's decorations are now outside of the paint area.

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); //To change body of generated methods, choose Tools | Templates.
            paintTest(g, getWidth() - 1, getHeight() - 1);
        }
    }
}

This is, one of many reasons, why you should not override the paint method of top level containers ;)




回答2:


    Rectangle rect = new Rectangle(50,50,700,300); 

That should work fine, are you creating a new instance of Rectangle before you access member variables?

Also 800 by 400 it kind of a weird resolution, 800 by 600 is more standard.



来源:https://stackoverflow.com/questions/13734069/how-can-i-set-in-the-midst

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!