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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T06:38:34+00:00 2026-06-16T06:38:34+00:00

I have this code and want to repaint my graphic when the Button gets

  • 0

I have this code and want to repaint my graphic when the Button gets pressed:

public class JDraw extends JFrame {

/**
 * Draws a figur in a JFrame.
 * The color of it is random and can get changed by a button.
 */

public static JButton okButton = new JButton("change color");

public JDraw(String newTitel) {
    super.setTitle(newTitel);
}

//Main method
public static void main(String str[]) {
    JDraw window = new JDraw("Graphic");
    window.setSize(300, 450);
    window.setVisible(true);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.add(okButton, BorderLayout.SOUTH);

    okButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            //JDraw.repaint(); <-- problem
        }
    });
}



@Override
public void paint(final Graphics g) {

    super.paint(g);

    Random rand = new Random();
    int r1 = rand.nextInt(255);
    int b1 = rand.nextInt(255);
    int g1 = rand.nextInt(255);
    g.setColor(new Color(r1, g1, b1));

    //head
    g.drawOval(100, 50, 100, 100);
    g.fillOval(100, 50, 100, 100); // filled
    //more drawing stuff.....

}
}

However I have no idea how to do it, because I cant do the repaint in my ActionPerfomed.
Error: non-static method repaint() cannot be referenced from a static context

I hope somebody can help. 🙂

  • 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-16T06:38:35+00:00Added an answer on June 16, 2026 at 6:38 am

    You need to make the following call in your actionPerformed:

    window.repaint();
    

    To be able to reference window from within you actionPerformed, you need to make your window variable final:

    final JDraw window = ...;
    

    However, if I can suggest a few improvements:

    1. Don’t extend JFrame
    2. Don’t override paint(Graphics) of JFrame
    3. Instead create a class that extends JComponent or JPanel and set it as the content pane of the JFrame
    4. Instead of overrding paint(Graphics), rather override paintComponent(Graphics)
    5. Your okButton should not be static. Instead move all your code in a non-static method like initUI() and have a code like new JDraw().initUI();
    6. Wrap the code starting your UI in a SwingUtilities.invokeLater(Runnable) so that your UI is properly initiated from the Event Dispatching Thread.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code and I want to have my button as a square,
I have a class Maze (extends JPanel). I want to make a new button,
I have class main extends jframe , it has a button that calls /shows
I have this code that I want to make point-free; (\k t -> chr
Hi can you help me with this?? I have this code and i want
i have this code and its working but i want it to return dailyTotals
I have this html code that i want to edit with jQuery. Here is
I have this code in my controller and want to test this code line
I have this code , and I want to run Log.d every 1000 milis
I have this code down here. When format.js fires I want to serve to

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.