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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:59:27+00:00 2026-06-06T19:59:27+00:00

This is the java code about JPanel : class Battle_field extends JPanel{ public List<Image_Obj>

  • 0

This is the java code about JPanel:

class Battle_field extends JPanel{
    public List<Image_Obj> pics_to_be_drawn;    

    @Override   
    public void paintComponent(Graphics g){     
            super.paintComponent(g);                 
            g.drawImage(pics_to_be_drawn.get(0).Get_the_buf_img() , 41, 41, 59, 59, Color.black, null);
    }       
}

As I set up the GUI:

added_panel= new Battle_field();
added_panel.setBorder(new LineBorder(SystemColor.activeCaption, 3));
added_panel.setBounds(27, 10, 397, 630);
added_panel.setBackground(Color.white);
this.getContentPane().add(added_panel);

I found that the creation of Battle_field() object above will call the paintComponent automatically.

But here, I didn’t initialize the variable “pics_to_be_drawn” yet,so if it is called, it would cause compiler error. Is this design unavoidable?

As you do so, it’s necessary to happen?
I wanna know this very much and of course the solution.

p.s:
According to the official document,only if I call repaint(), it will call paintComponent(). So I can write my customized code within paintComponent.

  • 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-06T19:59:29+00:00Added an answer on June 6, 2026 at 7:59 pm

    Swing calls the paintComponent() method when the panel must be painted. I don’t think it will call it until the panel is made visible.

    That said, your panel should be in a paintable state as soon as it’s added to the GUI. So the paintComponent() method should handle the case where the pictures have not been added yet, by simply checking that the list if not null (and not empty):

    @Override   
    public void paintComponent(Graphics g){     
        super.paintComponent(g);                 
        if (pics_to_be_drawn != null && !pics_to_be_drawn.isEmpty()) {
            g.drawImage(pics_to_be_drawn.get(0).Get_the_buf_img() , 41, 41, 59, 59, Color.black, null);
        }
    }
    

    Side note: I would initialize the list to Collections.emptyList() or to new ArrayList<>(), which would avoid the check for null. I would also respect the Java naming conventions: no underscore in class and variable names, camelCasing.

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

Sidebar

Related Questions

I have this Java code which is used for JSF pagination: public List<ActiveSessionObj> list(int
Is it possible to convert this java code snippet to php? public void testBalanceReturnsToZeroOnVending()
Context I have this piece of Java Code btn.setOnAction(new EventHandler<ActionEvent>() { public void handle(ActionEvent
When indenting java code with annotations, vim insists on indenting like this: @Test public
This Java code compiles fine, but when I try to run it I get:
For this Java code: stmt.addBatch( INSERT INTO Bills (BillDateTime, Table, Item, NoAttended, Service, Payment,
i have this java code: ArrayList<MessageMap> ids = getNewMail(UserID, type, maxIdMessage); Message[] message =
I have this Java code, and I want to do the same thing in
Can you explain to me the output of this Java code? int a=5,i; i=++a
I have a jsp page which needs a java code. This java code inside

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.