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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:29:10+00:00 2026-05-27T05:29:10+00:00

When i am trying to access repaint() method for JPanel form main static method

  • 0

When i am trying to access repaint() method for JPanel form main static method I am getting error: "Cannot make a static reference to the non-static method repaint() from the type Component". When I tried to change main method as "public void main", or remove static – it can not compile. What is the proper approach to solve this problem?

public class mull extends JPanel  { ...
....
public static void main(String[] args) {

        JFrame f = new JFrame("Swing Paint Demo");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        p = new JPanel();

        f.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {

                if ((e.getX() >= ringX && e.getX() <= (ringSize + ringX))
                        && (e.getY() >= ringY && e.getY() <= (ringSize + ringY))) {
                    ringPoints += 100;
                    allowedToDrawRing = false;
                }
                if ((e.getX() >= squareX && e.getX() <= (squareSize + squareX))
                        && (e.getY() >= squareY && e.getY() <= (squareSize + squareY))) {
                    squarePoints += 100;
                    allowedToDrawSquare = false;
                }
                if(!allowedToDrawSquare && !allowedToDrawRing){
//                  stop timer
                    // display points
                    ringTimer.stop();
                    squareTimer.stop();
                    showScores = true;
                    repaint(); // Cannot make a static reference to the non-static method repaint() from the type Component

                }
            }
        });

        f.add(p);
        f.add(new mull());
        f.setSize(frameWidth, frameHeight);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    } 

I replaced all my code form main and there is one detail I can not see how to deal with. In my main method I created JFrame and added to it times (Swing timers). When I palced all code into constructor it does not work and makes no sense to call constructor itself while inside in (in order to add to JFrame Timers). How to solve it?
Constructor:

public Mull() {

        JFrame f = new JFrame("Swing Paint Demo");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        p = new JPanel();

        f.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {

                if ((e.getX() >= ringX && e.getX() <= (ringSize + ringX))
                        && (e.getY() >= ringY && e.getY() <= (ringSize + ringY))) {
                    ringPoints += 100;
                    allowedToDrawRing = false;
                }
                if ((e.getX() >= squareX && e.getX() <= (squareSize + squareX))
                        && (e.getY() >= squareY && e.getY() <= (squareSize + squareY))) {
                    squarePoints += 100;
                    allowedToDrawSquare = false;
                }
                if(!allowedToDrawSquare && !allowedToDrawRing){
//                  stop timer
                    // display points
                    ringTimer.stop();
                    squareTimer.stop();
                    showScores = true;
                    repaint();

                }
            }
        });

        f.add(p);
        f.add(new Mull());
        f.setSize(frameWidth, frameHeight);
        f.setLocationRelativeTo(null);
        f.setVisible(true);


        ActionListener actionListenerRing = new ActionListener() {
              public void actionPerformed(ActionEvent actionEvent) {
                  if(ringCounter < 3){
                    xRing = (int) ((getWidth() - ringSize) * (Math.random()));
                    yRing = (int) ((getHeight() - ringSize) * (Math.random()));
                    while( !(
                            (xSquare + squareSize) < (xRing)
                            || (xSquare) > (xRing + ringSize )
                            || (ySquare + squareSize) < (yRing)
                            || (ySquare) > (yRing + ringSize)
                            )
                            ){
                        xRing = (int) ((getWidth() - ringSize) * (Math.random()));
                        yRing = (int) ((getHeight() - ringSize) * (Math.random()));


                    }
                    setParamsRing(xRing, yRing, ringSize);
                    ringCounter++;
                repaint();


                  } 
              }
            };
        ActionListener actionListenerSquare = new ActionListener() {
                public void actionPerformed(ActionEvent actionEvent) {
                    if(squareCounter < 3){
                    xSquare = (int) ((getWidth() - squareSize) * (Math.random()));
                    ySquare = (int) ((getHeight() - squareSize) * (Math.random()));
                    while( !(
                            (xSquare + squareSize) < (xRing)
                            || (xSquare) > (xRing + ringSize )
                            || (ySquare + squareSize) < (yRing)
                            || (ySquare) > (yRing + ringSize)
                            )
                            ){
                        xSquare = (int) ((getWidth() - squareSize) * (Math.random()));
                        ySquare = (int) ((getHeight() - squareSize) * (Math.random()));
                    }
                    setParamsSquare(xSquare, ySquare, squareSize);
                    squareCounter++;
                repaint();


                    }   
                }
              };


        ringTimer = new Timer(700, actionListenerRing);
        ringTimer.setInitialDelay(1000);
        ringTimer.start();

        squareTimer = new Timer(500, actionListenerSquare);
        squareTimer.setInitialDelay(1000);
        squareTimer.start();
    }

Probably, I should move Timers methods to another class and then call something like f.add(new setTimers()); ….

  • 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-27T05:29:11+00:00Added an answer on May 27, 2026 at 5:29 am

    Placing all your logic into the main method is a bad practice. It should reside in the object itself. That would also solve your static access problems. Here is how to do that:

    Move all that code to another non-static method, for example public void init() {}. Then, in your public static void main method, call that init method like this:

    mull m = new mull();
    m.init();
    

    Also, by Java naming conventions Java classes use CamelCase naming. So your class should be renamed to “Mull”.

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

Sidebar

Related Questions

Im trying to access a web service from a remote computer. I managed to
I am trying to access Outlook 2007 from C#. I have installed the PIA
I'm trying to access the Facebook API Admin.getMetrics method via jQuery. I'm correctly composing
I am trying to access the Cache in the Application_Start method of Global.asax. I
I am trying to access a view inside a splitter from my mainframe. At
I am trying to access Facebook from Python :D I want to fetch some
I'm trying to access 1.20163 from the following: 1 British Pound Sterling = 1.20163
I am trying to access the outer class variable cell[i][j] from the inner class
Im trying to access some variables from the parent in a child mc. Parent
I am trying to access static member of a class. my class is: class

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.