Results 1 to 4 of 4
  1. #1
    Davededo is offline Member
    Join Date
    Oct 2010
    Posts
    2
    Rep Power
    0

    Default Using GUI to select a shape object from and array

    Well I'm having a problem trying to access shapes from my arrayList (sample) i have to many different classes to post here but my code is like this for what I'm trying to work out.

    sample = arraylist of drawingShape
    DrawingShape = superclass of shapes i have (circle, rectangle and triangle)
    .getDrawable() = the method to draw the shape.

    Java Code:
    package ShapeModel;
    
    import java.awt.Color;
    import java.awt.Shape;
    import java.awt.geom.Ellipse2D;
    
      public class Circle extends DrawingShape
     {
        private int diameter;
    
    //***************************************************************
    //                        Constructor
    //***************************************************************
        public Circle(int xPosition, int yPosition, int diameter, Color colour)
        {
            super(xPosition, yPosition, diameter, colour);
        }
    
        /**
         * Return a 2D shape that represents the circle
         * and can be drawn
         * @return drawable shape to return
         */
        public Shape getDrawable()
        {
            Shape drawableCircle = null;
    
            drawableCircle = new Ellipse2D.Double(getXPosition(), getYPosition(),
                    getWidth, getWidth);
            return drawableCircle;
        }
    }
    
    ---------------------------------------------------------------------------------------------------------------
    
    package ShapeModel;
    import java.awt.Color;
    import java.awt.Shape;
    import java.awt.geom.Rectangle2D;
    
    public class Rectangle extends DrawingShape
     {
    
    //***************************************************************
    //                        Constructor
    //***************************************************************
        public Rectangle(int xPosition, int yPosition, int width, Color colour)
        {
            super(xPosition, yPosition, width, colour);
        }
    
        /**
         * Return a 2D shape that represents the Rectangle
         * and can be drawn
         * @return drawable shape to return
         */
        public Shape getDrawable()
        {
            Shape drawableRect = null;
    
            drawableRect = new Rectangle2D.Double(getXPosition(), getYPosition(), getWidth(), getHeight());
            return drawableRect;
        }
    }
    
    ----------------------------------------------------------------------------------------------------------------------------------------------------
    
    package ShapeModel;
    
    import java.awt.Color;
    import java.awt.Polygon;
    import java.awt.Shape;
    
    public class Triangle extends DrawingShape
     {
    
        //class constants
        private final static int NUM_POINTS = 3;
        // Instance variables
    
    
    //***************************************************************
    //                        Constructor
    //***************************************************************
        public Triangle(int xPosition, int yPosition, int width, Color colour)
        {
            super(xPosition, yPosition, width, colour);
        }
    
        /**
         * Return a 2D shape that represents the triangle
         * and can be drawn
         * @return drawable shape to return
         */
        public Shape getDrawable()
        {
            final int HALVE = 2;
            Shape drawableTriangle = null;
            int[] xpoints = { getXPosition(), getXPosition() + (getWidth()/HALVE), getXPosition() - (getWidth()/HALVE) };
            int[] ypoints = { getYPosition(), getYPosition() + getHeight(), getYPosition() + getHeight() };
            drawableTriangle = new Polygon(xpoints, ypoints, NUM_POINTS);
            return drawableTriangle;
        }
    }
    
    -------------------------------super class--------------------------------------------------
    
    package ShapeModel;
    
    import java.awt.Color;
    
    public class DrawingShape
    {
        private int xPosition;
        private int yPosition;
        private Color colour;
        private boolean selected;
        private int width;
        private int height;
    
        /**
         * Constructor for objects of class DrawingShape
         */
        public DrawingShape(int xPosition, int yPosition, int width, Color colour)
        {
            this.xPosition = xPosition;
            this.yPosition = yPosition;
            this.width = width;
            this.colour = colour;
            this.selected = false;
            this.height = width;
        }
    
        public void setColour(Color colour)
        {
            this.colour = colour;
        }
    
        public Color getColour()
        {
            return colour;
        }
    
        public boolean isSelected()
        {
            return selected;
        }
    
        public void setSelect(boolean value)
        {
            selected = value;
        }
    
        public int getWidth()
        {
            return width;
        }
    
        public void setWidth(int width)
        {
            this.width = width;
        }
    
        public int getHeight()
        {
            return width;
        }
    
        public void setHeight(int height)
        {
            this.height = height;
        }
    
        public int getXPosition()
        {
            return xPosition;
        }
    
        public int getYPosition()
        {
            return yPosition;
        }
    
        public void moveBy(int dx, int dy)
        {
            // Move the shape by dx pixels horizontally and dy pixels vertically
            // (by changing the position of the top-left corner of the shape).
            xPosition += dx;
            yPosition += dy;
        }
    
    }
    
    ------------------------------------------------- collection of shapes ----------------------------------------
    package ShapeModel;
    
    import java.util.ArrayList;
    
    public class ShapeCollection
    {
       private ArrayList<DrawingShape> shapes;
    
       /**
        * Constructer for class ShapeCollection
        */
       public ShapeCollection()
       {
          shapes = new ArrayList<DrawingShape>(200);
       }
    
       public int getSize()
       {
            return shapes.size();
       }
       
       /**
        * adds a shape to ShapeCollection
        * @param shape
        */
       public void addDrawingShape(DrawingShape shape)
       {
          shapes.add(shape);
       }
    
       /**
        * removes a shape from ShapeCollection
        * @param shape
        */
       public void removeDrawingShape(DrawingShape shape)
       {
          shapes.remove(shape);
       }
    
    }
    
    -------------------------------------------- shape viewer ------------------------------------------
    package ShapeViewer;
    
    
    import ShapeModel.ShapeCollection;
    import ShapeModel.DrawingShape;
    import ShapeModel.Circle;
    import ShapeModel.Triangle;
    import ShapeModel.Rectangle;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Shape;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    import javax.swing.JPanel;
    
    public class ShapeView extends JPanel
            implements MouseListener, MouseMotionListener {
    
        //Constants
        public int SHAPE_WIDTH = 50;
        //fields
        private ShapeCollection sample;
        private DrawingShape selectedShape = null;
        private DrawingShape draggedShape = null;
        private int previousX;
        private int previousY;
        private boolean drawingMode;
        private int shapeToCreate;
    
    //***************************************************************
    //                        Constructor
    //***************************************************************
        public ShapeView() {
            super();
            // add a single circle for demonstration purposes
            //sample = new ShapeCollection();
            this.setBackground(Color.white);
            // Program starts in drawing mode
            drawingMode = true;
            // This class listens for mouse events so that it can react to user input
            addMouseListener(this);
            addMouseMotionListener(this);
            shapeToCreate = 1;
        }
    //***************************************************************
    //                        Mutators
    //***************************************************************
    
        /**
         * Updates value of drawingMode.
         * @param drawingMode the new value
         */
        public void setDrawingMode(boolean drawingMode) {
            this.drawingMode = drawingMode;
        }
    
        public void changeShape(int sShape)
        {
            shapeToCreate = sShape;
        }
    
        public void removeAllShapes()
        {
            sample.removeAllShapes();
        }
    
        public void deleteSelectedShape()
        {
            sample.removeDrawingShape(selectedShape);
        }
    
        /**
         * A simple method that adds a single circle for demo purposes.
         * @param x x coordinate for circle position
         * @param y x coordinate for circle position
         * @param width width of circle
         * @param colour colour for new circle
         */
        public void addNewCircle(int x, int y, int width, Color colour)
        {
            DrawingShape draw = new Circle(x, y, width, colour);
            sample.addDrawingShape(draw);
            //It is always necessary to repaint if anything has changed.
            repaint();
        }
    
        public void addNewRectangle(int x, int y, int width, Color colour)
        {
            DrawingShape draw = new Rectangle(x, y, width, colour);
            sample.addDrawingShape(draw);
            //It is always necessary to repaint if anything has changed.
            repaint();
        }
    
        public void addNewTriangle(int x, int y, int width, Color colour)
        {
            DrawingShape draw = new Triangle(x, y, width, colour);
            sample.addDrawingShape(draw);
            //It is always necessary to repaint if anything has changed.
            repaint();
        }
    
        /**
         * Override superclass paint method so that shapes can be drawn
         * @param g the graphics contextin which to paint.
         */
        @Override
        public void paint(Graphics g) {
    
            super.paint(g);
            Graphics2D g2 = (Graphics2D) g;
            //if (sample != null)
            if(sample != null)
            {
                for(int i = 0; i < sample.getSize(); i++)
                {
                    DrawingShape cont = sample.getShapeAtIndex(i);
                    Color draw = cont.getColour();
                    int xPos = cont.getXPosition();
                    int yPos = cont.getYPosition();
                    int width = cont.getWidth();
                    boolean selected = cont.isSelected();
                    g2.setColor(draw);
                    g2.draw(cont.getDrawable());
                    //If the shape is selected we need to draw selection rectangle.
                    if (selected)
                    {
                        g2.setColor(Color.black);
                        g2.drawRect(xPos, yPos,width, width);
                    }
                }
            }
    
        }
    //***************************************************************
    //                        Mouse event handlers
    //***************************************************************
        /**
         * Handle mouse pressed events
         * @param e event to be handled
         */
        public void mousePressed(MouseEvent e) {
            if (drawingMode && shapeToCreate == 1) {
                addNewCircle(e.getX(), e.getY(), SHAPE_WIDTH, Color.red);
            } 
            else if (drawingMode && shapeToCreate == 2){
                addNewRectangle(e.getX(), e.getY(), SHAPE_WIDTH, Color.red);
            }
            else if(drawingMode && shapeToCreate == 3){
                addNewTriangle(e.getX(), e.getY(), SHAPE_WIDTH, Color.red);
            }
            else if (sample != null)//not already dragging
            {
                for(int i = 0; i <= sample.getSize(); i++)
                {
    
                        int x = e.getX();
                        int y = e.getY();
    
                        selectedShape = null;  //Unselect any existing selected shape
    
                         DrawingShape drawing = sample.getShapeAtIndex(i);
                            //Currently there is only one shape
                         Shape shape = drawing.getDrawable();
                            if (shape.contains(e.getPoint()))
                            {
                                drawing.setColour(Color.GREEN);
                                drawing.setSelect(true);
                                selectedShape = drawing;
                                repaint();
                            }
                            if (selectedShape == null) {
                                // The user did not click on a shape.
                                selectedShape.setSelect(false);
                                repaint();
                            } else {
                                // Start dragging the shape.
                                draggedShape = selectedShape;
                                previousX = x;
                                previousY = y;
                            }
                     }
              
    
            }
            repaint();
        }
    
        /**
         * Handle mouse released events
         * @param e event to be handled
         */
        public void mouseReleased(MouseEvent e) {
            // If shape has been dragged completely out of the drawing space then
            // treat this as a deleted shape.
            if (draggedShape != null) {
                int x = e.getX();
                int y = e.getY();
    
                draggedShape.moveBy(x - previousX, y - previousY);
    
                boolean offScreen = ((draggedShape.getXPosition() >= getSize().width) || (draggedShape.getYPosition() >= getSize().height) || (draggedShape.getXPosition() + draggedShape.getWidth() < 0)|| (draggedShape.getYPosition() + draggedShape.getWidth() < 0));
                if (offScreen) {
                    sample.removeDrawingShape(selectedShape);
                }
                repaint();
                draggedShape = null;  // Finished dragging.
            }
        }
    
        /**
         * Handle mouse dragged events
         * @param e event to be handled
         */
        public void mouseDragged(MouseEvent e) {
           if (draggedShape != null)
           {
              int x = e.getX();
              int y = e.getY();
              draggedShape.moveBy(x - previousX, y - previousY);
              previousX = x;
              previousY = y;
              repaint();
           }
        }
    
        //These mouse events are not used but must be provided
        //to implement MouseListener & MouseMotionListener
        public void mouseClicked(MouseEvent e) {
            for(int i = 0; i <= sample.getSize(); i++)
                {
    
                        int x = e.getX();
                        int y = e.getY();
    
                        selectedShape = null;  //Unselect any existing selected shape
    
                         DrawingShape drawing = sample.getShapeAtIndex(i);
                            //Currently there is only one shape
                         Shape shape = drawing.getDrawable();
                            if (shape.contains(e.getPoint()))
                            {
                                drawing.setColour(Color.GREEN);
                                drawing.setSelect(true);
                                selectedShape = drawing;
                                repaint();
                            }
                            if (selectedShape == null) {
                                // The user did not click on a shape.
                                selectedShape.setSelect(false);
                                repaint();
                            }
            }
        }
    
        public void mouseEntered(MouseEvent e) {
        }
    
        public void mouseExited(MouseEvent e) {
        }
    
        public void mouseMoved(MouseEvent e) {
        }
    }


    it only selects the shape in the green i am unsure of how to make it select any of the shapes anywhere else on the screen it only selects the first one. I do know why but not sure how to fix it.

    Any help would be apperciated.
    Last edited by Davededo; 10-17-2010 at 06:54 AM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default

    To get better help sooner, post a SSCCE that clearly demonstrates your problem.

    db

  3. #3
    Davededo is offline Member
    Join Date
    Oct 2010
    Posts
    2
    Rep Power
    0

    Default

    Ok so i have provided all the code, the code

    Java Code:
        public void mousePressed(MouseEvent e) {
            if (drawingMode && shapeToCreate == 1) {
                addNewCircle(e.getX(), e.getY(), SHAPE_WIDTH, Color.red);
            } 
            else if (drawingMode && shapeToCreate == 2){
                addNewRectangle(e.getX(), e.getY(), SHAPE_WIDTH, Color.red);
            }
            else if(drawingMode && shapeToCreate == 3){
                addNewTriangle(e.getX(), e.getY(), SHAPE_WIDTH, Color.red);
            }
            else if (sample != null)//not already dragging
            {
                for(int i = 0; i <= sample.getSize(); i++)
                {
    
                        int x = e.getX();
                        int y = e.getY();
    
                        selectedShape = null;  //Unselect any existing selected shape
    
                         DrawingShape drawing = sample.getShapeAtIndex(i);
                            //Currently there is only one shape
                         Shape shape = drawing.getDrawable();
                            if (shape.contains(e.getPoint()))
                            {
                                drawing.setColour(Color.GREEN);
                                drawing.setSelect(true);
                                selectedShape = drawing;
                                repaint();
                            }
                            if (selectedShape == null) {
                                // The user did not click on a shape.
                                selectedShape.setSelect(false);
                                repaint();
                            } else {
                                // Start dragging the shape.
                                draggedShape = selectedShape;
                                previousX = x;
                                previousY = y;
                            }
                     }
              
    
            }
            repaint();
        }
    is what im having a problem with.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

Similar Threads

  1. Replies: 23
    Last Post: 09-07-2010, 09:12 PM
  2. Replies: 1
    Last Post: 04-02-2010, 01:48 PM
  3. Replies: 7
    Last Post: 10-06-2009, 06:58 AM
  4. how to select Shape object after rotation.
    By dorairaj in forum AWT / Swing
    Replies: 5
    Last Post: 08-14-2009, 04:28 PM
  5. How to retain value in struts 2 using <s:select></s:select> tag
    By SaiPrasad@Sella in forum Web Frameworks
    Replies: 0
    Last Post: 02-09-2009, 08:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •