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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:21:25+00:00 2026-06-12T09:21:25+00:00

I am trying to draw graphics based on a button click using boolean flags.

  • 0

I am trying to draw graphics based on a button click using boolean flags. Before, I set the boolean value as a public static boolean type in the ShapeGUI class, and called it in ShapeListener using Shape.draw and that worked, although it took around 10 seconds to conjure a drawing.

Now I set the boolean flag inside the ShapeListener class itself. Now its either not repainting or is very slow. How can I make a drawing appear instantly when I click a button?

public class ShapeGUI extends JFrame
{
    public static final int WIDTH = 500;
    public static final int HEIGHT = 500;

    public ShapeGUI()
    {
        setSize(WIDTH, HEIGHT);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //setLayout(new FlowLayout());
        setLocationRelativeTo(null);
        JMenuBar menu = new JMenuBar();
        JMenu file = new JMenu("File");
        menu.add(file);
        JMenuItem save = new JMenuItem("Save Information");
        JMenuItem exit = new JMenuItem("Exit");
        file.add(save);
        file.add(exit);               
        setJMenuBar(menu);

        ShapeListener test = new ShapeListener();        
        JButton button = new JButton("Hello"); 
        test.setBackground(Color.CYAN);
        button.addActionListener(new ShapeListener());
        test.add(button); 
        add(test);

//followed this person's advice        
//First off, your drawing should be done in a paintComponent method override that is held in a class that extends JPanel. Next, this class could have a boolean variable that the paintComponent uses to decide if it will draw a rectangle or not. The button press simply sets the boolean variable and calls repaint on the drawing JPanel.
    }
}

    public class ShapeListener extends JPanel implements ActionListener

    {

    boolean draw = false;
    Shape s = new Circle();
    Shape a = new Rectangle();

    @Override
    public void actionPerformed(ActionEvent e) 
    {
        draw = true;
        repaint();
    }
    @Override
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);

        if(draw)
        {
        s.draw(g);
        }
        else
        {
            a.draw(g);
        }

    }

}
  • 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-12T09:21:25+00:00Added an answer on June 12, 2026 at 9:21 am

    Your logic is a little off…

    ShapeListener test = new ShapeListener(); // First instance
    JButton button = new JButton("Hello");
    test.setBackground(Color.CYAN);
    button.addActionListener(new ShapeListener()); // Second instance...
    test.add(button);
    add(test);
    

    You create two instance of the ShapeListener class, add one to the container and use the other as the action listener. Neither will know about each other, meaning that you can click to the cows come home, the first instance of ShapeListener will never change.

    Also, unless you’re planning to override the preferredSize method of the ShapeListener pane, I’d use something like a BorderLayout on the frame, to ensure that the panel is properly laid out, otherwise it will look like the panel isn’t being painted…

    Try something like…

    public class ShapeGUI extends JFrame {
    
        public static final int WIDTH = 500;
        public static final int HEIGHT = 500;
    
        public ShapeGUI() {
            setSize(WIDTH, HEIGHT);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLocationRelativeTo(null);
            JMenuBar menu = new JMenuBar();
            JMenu file = new JMenu("File");
            menu.add(file);
            JMenuItem save = new JMenuItem("Save Information");
            JMenuItem exit = new JMenuItem("Exit");
            file.add(save);
            file.add(exit);
            setJMenuBar(menu);
    
            setLayout(new BorderLayout());
    
            ShapeListener test = new ShapeListener();
            JButton button = new JButton("Hello");
            test.setBackground(Color.CYAN);
            button.addActionListener(test);
            test.add(button);
            add(test);
    
            setVisible(true);
    
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException ex) {
                    } catch (InstantiationException ex) {
                    } catch (IllegalAccessException ex) {
                    } catch (UnsupportedLookAndFeelException ex) {
                    }
    
                    new ShapeGUI();
    
                }
            });
        }
    
        public class ShapeListener extends JPanel implements ActionListener {
    
            Shape s = new Ellipse2D.Float(0, 0, 20, 20);
            Shape a = new Rectangle2D.Float(0, 0, 20, 20);
    
            private Shape paintMe = a;
    
            @Override
            public void actionPerformed(ActionEvent e) {
                paintMe = s;
                repaint();
            }
    
            @Override
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
    
                int x = (getWidth() - paintMe.getBounds().width) / 2;
                int y = (getHeight() - paintMe.getBounds().height) / 2;
    
                Graphics2D g2d = (Graphics2D) g.create();
                g2d.translate(x, y);
                g2d.draw(paintMe);
                g2d.dispose();
    
            }
        }
    }
    

    instead…

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

Sidebar

Related Questions

I am trying to draw some basic graphics primitives(line, rectangle etc.) using GDI+ apis
Using core graphics , I am trying to draw a line, in that when
I'm trying to draw Thai/Chinese strings on a J2ME Canvas using graphics.drawString(string, i, i,
I am trying to draw in a web worker using html5 canvas. The worker
I'm trying to draw an open rectangle using a Polygon: int[] xPoints = {1,1,3,3};
I'm trying to figure out how to draw graphics in XNA, and someone else
I am trying to draw custom shapes in iPad application. I am using UIBezierPath
I'm trying to write a simple PDF viewer using CGPDFDocument , based on QuartzDemo.
I am trying to understand the relationship between android.graphics package and SurfaceFlinger . Based
I'm trying to draw shapes that will move around the screen using the LunarLander

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.