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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:53:30+00:00 2026-06-16T18:53:30+00:00

What is the best practice or standard for drawing graphics in java? I have

  • 0

What is the best practice or standard for drawing graphics in java?
I have my Jframe with a Jpanel and a Class(foo) which contains a Polygon(shape).
If I want to display the classes shape should I call foo.draw(pass it the graphics2d and a few other things) and have it draw itself (which I think would be encapsulation? which I believe is preferred?) or do all the drawing within my drawScreen() method?

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-16T18:53:31+00:00Added an answer on June 16, 2026 at 6:53 pm

    If the Foo should never overlap with any other graphics, e.g. it is a Label or a Button, then it should definitely draw itself. I suggest that it extend JComponent, implement paintComponent(), and be added using a LayoutManager etc. etc.

    If the Foo can overlap with other graphics (say, it’s some space game and the polygon is a quickly moving asteroid heading towards earth) then things get trickier. I still think it best if the Foo knows how to draw itself to a Graphics, but you might not want to extend JComponent.

    option 1: extend JComponent and implement paintComponent, but note in the javadocs that this is not a “real” JComponent and that it never gets added using a LayoutManager. You can even change addNotify() to throw an exception to prevent this.

    Option 2: have the Foo implement Icon – a nice little interface for lightweight drawings.

    This still begs the question, something else needs to tell it to draw itself. So something needs to keep a list of these JComponents or Icons and tell them to draw…

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

Sidebar

Related Questions

is there a standard rule or best practice which dictates when a custom class/library
What is the best practice for configuring a java class library? At my work,
is there a standard or best practice with database table names and fields in
Is there a best practice or defacto standard with how much of the GORM
Is there any guideline or standard best practice how to version a software you
Are there any standard or best practice ways of limiting feature functionality for a
Is there a best-practice or industry standard for throwing exceptions from a toolkit API?
Is there a standard or best practice for the order of grouping of styles
Is there a good advisory / best practice manual etc. out there which outlines
I've been inspired by another question: Best Practice for Using Java System Properties I'm

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.