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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:30:10+00:00 2026-06-12T23:30:10+00:00

I’ve got a class called Shape which inherits from JPanel. A number of sub-classes

  • 0

I’ve got a class called Shape which inherits from JPanel.

A number of sub-classes in turn extend the Shape classes, one for each type of shape.

Each shape has its own overriden paint() method, which draws the respective shape.

I would like to be able to click on any shape, and am trying to implement this logic for now. Please note that each shape has been added to an arrayList.

However, the contains statement always returns false, even when I have clearly clicked inside the shape.

Any ideas?

  • 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-12T23:30:11+00:00Added an answer on June 12, 2026 at 11:30 pm

    Never override paint() in JPanel rather paintComponent(..)

    Im not quite sure I understand however I made a short example which I hope will help.
    Basically it is a simple JFrame with a DrawingPanel (my own class which extends JPanel and the shapes are drawn on). This panel will create shapes (only 2 for testing) add them to an ArrayList and draw them to the JPanel via paintComponent(..) and a for loop, it also has a MouseAdapter to check for user mouseClicked(..) evnets on the JPanel. When a click is made we iterate through each Shape in the ArrayList and check whether the Shape contains the point or not, and if so prints its class name and uses instance of to check what type of Shape is clicked and prints appropriate message:

    enter image description here

    Output (after clicking both shapes):

    Clicked a java.awt.geom.Rectangle2D$Double

    Clicked a rectangle

    Clicked a java.awt.geom.Ellipse2D$Double

    Clicked a circle

    ShapeClicker.java:

    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Shape;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.geom.Ellipse2D;
    import java.awt.geom.Rectangle2D;
    import java.util.ArrayList;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class ShapeClicker {
    
        public ShapeClicker() {
            JFrame frame = new JFrame();
            frame.setTitle("Shape Clicker");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setResizable(false);
    
            initComponents(frame);
    
            frame.pack();
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
    
            //create frame and components on EDT
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new ShapeClicker();
                }
            });
        }
    
        private void initComponents(JFrame frame) {
            frame.add(new ShapePanel());
        }
    }
    
    //custom panel
    class ShapePanel extends JPanel {
    
        private Shape rect = new Rectangle2D.Double(50, 100, 200, 100);
        private Shape cirlce = new Ellipse2D.Double(260, 100, 100, 100);
        private Dimension dim = new Dimension(450, 300);
        private final ArrayList<Shape> shapes;
    
        public ShapePanel() {
            shapes = new ArrayList<>();
            shapes.add(rect);
            shapes.add(cirlce);
            addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent me) {
                    super.mouseClicked(me);
                    for (Shape s : shapes) {
                        
                        if (s.contains(me.getPoint())) {//check if mouse is clicked within shape
                            
                            //we can either just print out the object class name
                            System.out.println("Clicked a "+s.getClass().getName());
                            
                            //or check the shape class we are dealing with using instance of with nested if
                            if (s instanceof Rectangle2D) {
                                System.out.println("Clicked a rectangle");
                            } else if (s instanceof Ellipse2D) {
                                System.out.println("Clicked a circle");
                            }
                            
                        }
                    }
                }
            });
        }
    
        @Override
        protected void paintComponent(Graphics grphcs) {
            super.paintComponent(grphcs);
            Graphics2D g2d = (Graphics2D) grphcs;
            for (Shape s : shapes) {
                g2d.draw(s);
            }
        }
    
        @Override
        public Dimension getPreferredSize() {
            return dim;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text area in my form which accepts all possible characters from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am doing a simple coin flipping experiment for class that involves flipping a
I would like to run a str_replace or preg_replace which looks for certain words

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.