6

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?

6
  • OK, are you planning something like Paint shapes component?
    – Takarakaka
    Oct 17, 2012 at 11:50
  • What kind of shapes? You have to implement the contains method. It's not gonna generate itself from their 'paint' method. Oct 17, 2012 at 11:52
  • Could you kindly elaborate please @JanDvorak? So far the program works, whereby a number of shapes are drawn on the screen. My problem is that I cannot seem to click on any of the shapes.
    – Dot NET
    Oct 17, 2012 at 11:53
  • have look at MouseListener or SwingUtilities
    – mKorbel
    Oct 17, 2012 at 11:53
  • in your swift range is SSCCE more than requirement, or there I uselessly kiddin about respect to the forums habits (sure wrong too), nobody can to see rest of your code, and potentional answerers simple ignored this question (there are a few good answeres and be able to answering this question without thinking about),
    – mKorbel
    Oct 17, 2012 at 12:31

2 Answers 2

13

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;
    }
}
1
  • 2
    It didn't solve the problem however I greatly appreciate the effort. Thanks :)
    – Dot NET
    Oct 17, 2012 at 13:31
3

If you are implementing Shape you have to implement the contains method yourself. The default implementation for Shape always returns false.

If your Shape is bounded by curves that you know how to intersect (or determine if a point is on one or the other side), you can use the even-odd rule. Cast a ray from the point tested in any direction not parallel to a straight line. If the number of intersections is odd, the point is inside. If the number of intersections is even, the point is outside.

The built-in classes implement this method, so you can use/extend the Polygon, Ellipse2D.Double or RoundRectangle2D.Double class and have a filled polygon / ellipse / round rectangle that knows its inside.

4
  • Thanks for the reply. Since my shape classes call the draw method and draw a shape like that, they are not really bound with the shape class are they?
    – Dot NET
    Oct 17, 2012 at 12:20
  • @DotNET you said you had a shapeArrayList? So, the method you have tried will work if you implement the contains method. Oct 17, 2012 at 12:25
  • I've understood that much thanks. I'm not so sure about how to implement the contains method though. This is supposed to be a very simple assignment covering the basics of java, however what you mentioned seems complex beyond the scope of this task.
    – Dot NET
    Oct 17, 2012 at 12:34
  • @JanDvorak Shape is an interface and is implemented - not extended. Could be confusing for future readers. +1 once fixed.
    – Nick Rippe
    Oct 17, 2012 at 13:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.