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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:35:40+00:00 2026-05-30T22:35:40+00:00

In a sub-class of JComponent I am overriding paintComponent method to draw. The first

  • 0

In a sub-class of JComponent I am overriding paintComponent method to draw. The first thing the overridden method does is to call super.paintComponent since most texts tell me to do this. Some say that this is necessary so that other JComponents can paint themselves. However, calling the super method clears the image, filling it with the background color. I don’t want this happen. If I skip calling the super method, I am free to choose whether or not I want to call

g.setColor(getBackground());
g.fillRect(0, 0, getWidth() - 1, getHeight() - 1);

in order to clear the image before redrawing.

But, I’m worried that by skipping the super method, I will be skipping other important operations the super method does. How can I make sure that those other important operations are executed while allowing myself to choose if I want to clear the image or not?

EDIT:

Here is my paintComponent:

public void paintComponent(Graphics g) {
    super.paintComponent(g); // necessary so other panels can paint themselves.    
    //         g.setColor(getBackground());
    //         g.fillRect(0, 0, getWidth(), getHeight());
    //         g.setColor(Color.black);
    if (down) {
        g.drawLine(downX, downY, currentX, currentY);
    }        
}

It just draws a line from where the user presses the mouse button to where the mouse was dragged to. If the mouse drags, the line moves. If the mouse is released it won’t draw anything.

As it is, the program will erase the line the user is drawing if the mouse is released. I don’t want the program to to that so that there will be a trail of lines.

  • 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-05-30T22:35:42+00:00Added an answer on May 30, 2026 at 10:35 pm

    You can safely skip the super() call or set opaque to false with JComponent’s setOpague()

    Good luck!

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

Sidebar

Related Questions

If subclass in objective-c wants to override a super class's method, does it have
While attempting my first sub-class in Objective-C I have come across the following warning
I have a VB6 class with a method which raises an error: Public Sub
I have the following code in my class : sub new { my $class
I have the following code: Imports MySql.Data.MySqlClient Imports MySql.Data.Types Public Class FormMain Private Sub
I'm trying to create a sub-class of the JButton component that will enable or
I have a JComponent subclass that I am using to draw shapes onto my
I have class PrintUtilities , which has the following method to print JPanel and
I have classX: Sub New(ByVal item_line_no As String, ByVal item_text As String) ' check
I have a subclass with an over-ridden method that I know always returns a

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.