Whiteheart Awashreh
Whiteheart Awashreh

Reputation: 25

Handling an generated shapes in a JPanel

I have a program that allows the user to free draw shapes inside a JPanel, the drawn shapes are stored in an array-list of a general type (a class that is the class the shape types extend). However I need to allow the user to interact with the shapes. Read: event handlers. Any idea how to work that one?

Current code of how I draw my shapes, although it's unrelated:

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.GeneralPath;
import java.util.ArrayList;
import java.util.LinkedList;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class DrawPanel extends JPanel {
    private ArrayList <shape> shapes;
    public enum ShapeType{LINE,OVAL};
    public ShapeType shapeType;
    public shape currentShape;
    public Color currentColor=Color.BLACK;
    public DrawPanel(){
        shapes= new ArrayList<shape>();
        currentShape=null;
        setBackground(Color.WHITE);
        MouseHandler mouseHandler = new MouseHandler();
        addMouseListener(mouseHandler);
        addMouseMotionListener(mouseHandler);
    }
    @Override
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        for(int i=0;i<shapes.size();i++)
        {
            shapes.get(i).draw(g);

        }
        if(currentShape!=null)
        {
            currentShape.draw(g);
        }
    }
    public void clearDraw(){
        shapes.clear();
        repaint();
       }

    public void setToOval(){
        setShapeType(ShapeType.OVAL);
    }
    public void setToLine(){
        setShapeType(ShapeType.LINE);
    }
    public void setShapeType(ShapeType shape){
        shapeType=shape;
    }
    private class MouseHandler extends MouseAdapter implements MouseMotionListener{
        public void mousePressed(MouseEvent event){
            if(shapeType!=null){
                switch(shapeType){
                case LINE:
                    currentShape = new line(event.getX(),event.getX(),event.getY(),event.getY(),currentColor);
                    break;
                case OVAL:{
                    currentShape=new oval(event.getX(),event.getX(),event.getY(),event.getY(),currentColor);
                }
                break;
                }
            }
        }
        public void mouseReleased(MouseEvent event){
            if(currentShape!=null)
            {
        currentShape.setMyColor(currentColor);
                currentShape.setX2(event.getX());
                currentShape.setY2(event.getY());
                shapes.add(currentShape);
                currentShape=null;
                validate();
                repaint();

            }
        }
        public void mouseDragged(MouseEvent event){
            if(currentShape!=null)
            {
                currentShape.setMyColor(currentColor);

                currentShape.setX2(event.getX());
                currentShape.setY2(event.getY());
                validate();
                repaint();
            }
        }
    }
    public void setCurrentColor(Color color){

        this.currentColor=color;

    }

}

Upvotes: 0

Views: 55

Answers (1)

Christoph Bimminger
Christoph Bimminger

Reputation: 1018

Shapes are elements which you just draw on your surface. You cannot directly handle events on them. An option is to handle mouse events in your JPanel, and iterate through your list of painted shapes. You can check if a shape is located at your current mouse location, and based on that result, set your currentShape variable to this shape (or set currentShape to NULL and consider that probably NO shape is selected in your other handlers...)

Upvotes: 1

Related Questions