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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:44:33+00:00 2026-06-13T06:44:33+00:00

I found similar problems on the internet but the solutions provided didn’t work. I

  • 0

I found similar problems on the internet but the solutions provided didn’t work.

I want to clear the JPanel. To do that I call repaint()(from the clear() method) with a flag set to false to avoid calling my drawing method (drawLines()). Drawn lines are still on the Panel.

I tried to repaint same lines again with the background color. This also didn’t work.

  public class WektPanel extends JPanel{    

    boolean check = false;
    Color c = Color.BLUE;
    boolean oval = false;

    public WektPanel() {
        setBackground(c);
        setVisible(true);

    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        if(check)
            drawLines(3, g);

    }

    void clear(){
        check=false;
        repaint();
    }
    void draw(){
        check=true;
        repaint();
    }

    void drawLines(int stroke, Graphics g){

        g.drawLine(0,0,getWidth(),getHeight());
        g.drawLine(0,getHeight(),getWidth(),0);
        for(int i=1; i<stroke;i++){
                g.drawLine(0+i,0,getWidth(),getHeight()-i);
                g.drawLine(0,0+i,getWidth()-i,getHeight());
                g.drawLine(0,getHeight()-i,getWidth()-i,0);
                g.drawLine(0+i,getHeight(),getWidth(),0+i);
        }
    }

}
  • 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-13T06:44:34+00:00Added an answer on June 13, 2026 at 6:44 am

    You’re overriding paint(), but calling super.paintComponent(g). You should override paintComponent(g).

    Edit: I couldn’t follow your check logic. This worked for me.

    public class Test {
    
        private class WektPanel extends JPanel {
    
            boolean clear;
    
            public WektPanel() {
                setBackground(Color.BLUE);
            }
    
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(300, 200);
            }
    
            @Override
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                drawLines(3, g);
            }
    
            public void clear() {
                clear = true;
                repaint();
            }
    
            public void draw() {
                clear = false;
                repaint();
            }
    
            private void drawLines(int stroke, Graphics g) {
                if (!clear) {
                    g.drawLine(0, 0, getWidth(), getHeight());
                    g.drawLine(0, getHeight(), getWidth(), 0);
                    for (int i = 1; i < stroke; i++) {
                        g.drawLine(0 + i, 0, getWidth(), getHeight() - i);
                        g.drawLine(0, 0 + i, getWidth() - i, getHeight());
                        g.drawLine(0, getHeight() - i, getWidth() - i, 0);
                        g.drawLine(0 + i, getHeight(), getWidth(), 0 + i);
                    }
                }
            }
        }
    
        private void showGUI() {
            JFrame f = new JFrame("Test");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            final WektPanel wektPanel = new WektPanel();
            f.add(wektPanel, BorderLayout.CENTER);
            JPanel buttonPanel = new JPanel();
            buttonPanel.add(new JButton(new AbstractAction("Clear") {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    wektPanel.clear();
                }
            }));
            buttonPanel.add(new JButton(new AbstractAction("Draw") {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    wektPanel.draw();
                }
            }));
            f.add(buttonPanel, BorderLayout.SOUTH);
            f.pack();
            f.setLocationRelativeTo(null);
            f.setVisible(true);
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    new Test().showGUI();
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have found similar problems on the web, but none of the posted solutions
I found some similar problems here on SO, but none seem to match my
I just search through the forum, but haven't found a similar problem. My problem
So, I've been searching around and I've found things similar to my problem, but
I found similar topics but they all used absolute positioning which placed the canvases
I found similar questions but no answer to what I am looking for. So
I ran a quick google and SO search and found similar questions but none
Sorry if this is a duplicate question...I've searched around and found similar advice but
I've been researching this one and found references to similar problems here and there,
I've tried searching for people with similar questions, but haven't found anything. I have

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.