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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:06:29+00:00 2026-06-09T00:06:29+00:00

Recently, I have been trying to do some Java work with graphics. Now the

  • 0

Recently, I have been trying to do some Java work with graphics. Now the Graphics class is abstract, meaning it itself cannot be instantiated but a subclass of it might be able to be instantiated. So what is going on in the following code. (That does successfully create a line)

import javax.swing.*;
import java.awt.*;

public class line extends JPanel {
    public void paintComponent(Graphics g) {
        g.drawLine(0, 0, 90, 90);
    }

    public static void main(String[] args) {
        JFrame jf = new JFrame();
        jf.add(new line());
        jf.setSize(500, 500);
        jf.setVisible(true);
    }
}

Okay now a few things don’t make sense to me. First, when line is called it must enact the paintComponent method in the line class. That’s a property of static methods (being called with the class name). But the paintComponent method doesn’t have a static modifier.

Since this program works though we have to assume that paintComponent does get called and expects a Graphics object as an argument. When it is called though it has no arguments. Why doesn’t this create an error? I ran some tests with passing in a String and then nothing into a method that was expecting an Integer object. I got errors on both programs.

So somehow a graphics object called g is created and the uses the drawline method. But Graphics is abstract.

Okay so here are my questions:

  1. Why is paintComponent being automatically called when it’s class line is called when paintComponent isn’t static?

  2. Why does paintComponent accept empty arguments when it expects a Graphics object?

  3. How is the Graphics object g then made as the Graphics class is abstract and cannot be instantiated?

Help me Stack Overflow, you’re my only hope.

EDIT:
Okay, I think this question is pretty well answered, thanks!

  • 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-09T00:06:30+00:00Added an answer on June 9, 2026 at 12:06 am
    1. paintComponent is called when AWT decides it is time to paint the window (in most cases because the OS sends it a message telling it to do so). When it paints the JFrame it traverses the tree of objects that are inside it. Among which is your instance of line because you added it with the jf.add(new Line()) call. On each of these the paintComponent method is called.

    2. It doesn’t accept empty arguments.

    3. When AWT calls paintComponent it passes an instance of a concrete class that inherits from Graphics.

    The signature paintComponent(Graphics g) not so much says the parameter should be exactly of type Graphics. But actually says it should be of type Graphics or any class inheriting from Graphics. Another way to look at it is to say that any class that inherits from Graphics is also of type Graphics.

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

Sidebar

Related Questions

I am new to Java and have been trying to make some simple games
Recently I have been trying to optimize my tables, mainly because I've learned alot
I have been recently trying to deploy a C# application on a computer that
I recently downloaded the Android Support Package and have been using it trying to
I've recently been trying to port a C++ application. I believe I have all
I have been practicing TDD in C# for several years now and recently have
So I recently have been trying to incorporate more sub forms to make the
I've been working on some solutions for some questions that have been recently posted
I've been trying to do more Web development work recently (I currently do other
I've been programming for many years, recently i've been trying to apply some of

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.