Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8942533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:32:19+00:00 2026-06-15T11:32:19+00:00

I try to draw a rectangular in Java. I set the frame size (800,400)

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T11:32:21+00:00Added an answer on June 15, 2026 at 11:32 am

    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…

    enter image description here

    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.

    enter image description here

    Here the rectangle now looks correctly centered. In my tests, I set the first frame (bad) to 800×400, I made the second frame’s content pane’s preferred size 800×400, which made the frame size actually 816×438, 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 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'am trying to draw a line above mapkit. If I try to set the
I try to draw a line with the width depending on the density. The
I try to draw a spline using openGL and Java. What I have now
I'm trying to to program the Mandelbrot set for Android. I try to draw
I try to draw rain and snow as particle system using Core Graphics .
I try to draw a table on a panel in C# Windows Form by
I try to draw a sequence of pattern images (different repeated patterns in one
I try to draw some lines with different colors. This code tries to draw
I am using the pictureBox_Paint event to try and draw an overlay onto the
Try this code - import java.io.StringReader; public class StringReaderTest { public static void main(String[]

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.