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 8880217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:07:29+00:00 2026-06-14T20:07:29+00:00

I have extended JPanel and overriden the paint method in order to draw some

  • 0

I have extended JPanel and overriden the paint method in order to draw some extra lines between the locations of certain JButtons within the panel. However, the lines are only drawn correctly when the gui is maximized, otherwise they are drawn at totally the wrong offset.

To paint the line I am using (‘rootNode’ and ‘child’ are both JButton, g is the Graphics parameter of the overriden paint method):

Point sourcePoint = new Point(rootNode.getLocation());
Point destPoint = new Point(child.getLocation());
SwingUtilities.convertPointToScreen(sourcePoint, rootNode.getParent());
SwingUtilities.convertPointToScreen(destPoint, child.getParent());
g.drawLine(sourcePoint.x, sourcePoint.y, destPoint.x, destPoint.y);

Picture of incorrect lines when not maximised: http://postimage.org/image/ws0yo9chf/
Picture of correct when maximised: http://postimage.org/image/fq84m5xmb/

  • 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-14T20:07:30+00:00Added an answer on June 14, 2026 at 8:07 pm

    Just to cover off on the comments. I don’t think you want to convert to screen coordinates in this case.

    The Graphics context for the paintComponent(...) method will most likely be set up for the component coordinate system.

    JavaDoc for Graphics.drawLine(...) states:

    Draws a line, using the current color, between the points (x1, y1) and
    (x2, y2) in this graphics context’s coordinate system.

    Unless you’ve done something to change it via Graphics.translate(...) or Graphics2D.setTransform(...), the coordinate system will be set up for the component.

    Apart from being in the wrong place, converting to screen coordinates will have the effect of changing the location of the lines depending on where the window is on the screen 🙂

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

Sidebar

Related Questions

I have extended the devise controller for sessions. To add some extra functionality when
I am trying to draw an animation. To do so I have extended View
I have extended a MaskedTextBox component to add some functionality. The text property of
I have extended Ext.data.Operation to implement a custom commitRecords method. The Ext.data.Operation class is
I have a method in a JPanel private void paintScore(Graphics2D g) { Font scoreFont
In my program I have a class GraphDisplay extended from JPanel that is used
I have extended objects of type IDataReader with some extension methods that I needed.
I have 'extended' the System.DateTime struct by adding some essential fields to it. Ideally
I have extended SimpleCursorAdapter and am running into a weird issue with bindView, which
I have extended my entities to implement specific interfaces for its type. I am

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.