hi there, i want to make an application in which that if we select a shape to be drawn and the we draged the mouse in the panel aseries of the sames shape will be drawn
my prog in the other hand draw the shape and if i drage the shape the figure moves withe the mouse and if i want to draw another shape it disapers...im having truble with this mouse although it cute and a mazing; these are my mouse liseners interfaces :

public void mouseClicked( final MouseEvent event ) {
       int x=event.getPoint().x;
       int y=event.getPoint().y;
       shapesList[selctIndex1].setFirstPoint(x, y);
       repaint();
    }

// handle event when mouse pressed
    public void mousePressed( final MouseEvent event ) {
        int x2=x1+20;
        int y2=y1+30;
        shapesList[selctIndex1].setSecondPoint(x2, y2);
        x2=x2+5;
        y2=y2+5;
        repaint();
    }

// handle event when mouse released after dragging
    public void mouseReleased( final MouseEvent event ) {
         
    }



// handle event when mouse enters area
    public void mouseEntered( final MouseEvent event ) {

    }

// handle event when mouse exits area
    public void mouseExited( final MouseEvent event ) {

    }


    // MouseMotionListener event handlers // handle event when user drags mouse with button pressed
    public void mouseDragged( final MouseEvent event ) {
         x1=getX();
         y1=getY();
         drwanShapes.add(shapesList[selctIndex1]);
         repaint();
    }

// handle event when user moves mouse
    public void mouseMoved( final MouseEvent event ) {
    //do nothing
    }

    public void paint(Graphics g)
    {
        super.paint(g);
        shapesList[selctIndex1].draw(g);
    }

i reach the point were my mind is stuck and cannot think any one that can help
me i thank you in advance^___^

oh- the shapesList encapsulates line, rectangle and oval shapes
and you can pass 2 points to draw them....in the paint i draw them polymorphically.

Can you explain your problem?
If the shapesList array has all the shapes you want to draw, could the paint method loop through the array and draw the shapes saved there?

i want the user to select from the list a shape to be drawn..not only just draw one single shape if you select a rectangle for example and drag the mouse in the drawing area while draging multiple recatngles are to be drawn until the mouse is released...
it dose not harm if i override paint() is it or it is the resone were my panel allow me to draw one shape if i select anothe shape it deletes the privous one....gust question im using borderlayout is it a resone also >.<

You only draw one shape because your paint method only draws one shape. It's nothing to do with your panel or or layout manager. So:
replace paint by paintComponent
put a loop into that method to call draw for all the shapes in shapesList.

ok i replaced it..the it dosent draw any shape i have smth in my mouse listener handler but cant figure it out...
this is the modified code:

public void mouseClicked( final MouseEvent event ) {
      
//         x1=getX();
//         y1=getY();
//         drwanShapes.add(shapesList[selctIndex1]);
//         repaint();
    }

// handle event when mouse pressed
    public void mousePressed( final MouseEvent event ) {
        int x=event.getPoint().x;
        int y=event.getPoint().y;
        shapesList[selctIndex1].setFirstPoint(x, y);
        int x2=x1+20;
        int y2=y1+30;
        shapesList[selctIndex1].setSecondPoint(x2, y2);
        x2=x2+5;
        y2=y2+5;
       repaint();
    }

// handle event when mouse released after dragging
    public void mouseReleased( final MouseEvent event ) {
         
    }



// handle event when mouse enters area
    public void mouseEntered( final MouseEvent event ) {

    }

// handle event when mouse exits area
    public void mouseExited( final MouseEvent event ) {

    }


    // MouseMotionListener event handlers // handle event when user drags mouse with button pressed
    public void mouseDragged( final MouseEvent event ) {
      
       repaint();
    }

// handle event when user moves mouse
    public void mouseMoved( final MouseEvent event ) {
    //do nothing
    }

    public void paintComponent(Graphics g)
    {
        super.paint(g);
        shapesList[selctIndex1].draw(g);
    }

oh one thing i dont want to draw the shapes by it self i want the user to shoose what
to draw .

i want the user to choose what to draw .

How will the user tell the paint method which shape(s) to draw?
Will you show a list of shapes and have the user chose those that are to be drawn?
As the user chooses the ones to be drawn, copy them to a listToDrawn list and call repaint after they have all been chosen. The paint method would then use the listToDraw list to draw what the user has chosen.

well i have A list that contain objects of shapes i save the index that is beeing selected in a variable using lisHandler and then in the paint i access the chhosen shape
from the list to be drawen....but it wont draw..

..but it wont draw..

Please explain.
Add a println in the paint method that displays the index and the contents of the array at that index.

it dose not show any thing

That means the paintComponent method was not being called.
Add this in front of the paintComponent() method to be sure it is overriding correctly:

@Override

What class is this in? Does it have a paintComponent method to override?

We're assuming that the paint method is in a Swing component like JPanel and not in JFrame.

Edited 4 Years Ago by NormR1: n/a

yes im extending JFrame..when i put the print inside the list handler listener it prints to me the index as 00 for the first index.

In my previous posts I may have misunderstood shapesList - I thought that was a list of all the shapes you have created, but now I suspect it's a list of possible shapes. In which case... as you create new instances of yuor shapes you need to add those to a list, and that's the list whose members you need to draw in paintComponent.

yes im extending JFrame

Did you add the @Override before the method you are using to draw the shapes? What happened when you compiled?

it gave me error but when i put it back to paint() it prints out the endexs as 00 11 22

What prints out for the contents of the array at those indexes? Are the indexes's values what you expect?

yes im extending JFrame..

Sorry, in that case paintComponent would not be right. I really recommend that you implement your drawing area as a JPanel so you can add buttons etc in the same window, but without interfering with your drawing/mouse handling. (And if you extend JPanel then yes, it's paintComponent!)

JamesCherril this is the my whole code to better understand and help me with this issue
im trying to figure it out with your help.thanks guys very much:

package shape;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import javax.swing.event.*;
import javax.swing.*;
import javax.swing.JPanel;



public class drawingSapes extends JFrame  implements MouseListener,MouseMotionListener {
    private JPanel pan1,pan2;
    private Container c;
    private JList list;
    private shape []shapesList= {new line(),new rectangle(),new oval()};
    private JLabel l1;
    private JComboBox bx1;
    private String[] clr={"red","yellow","green","blue","black"};
    private int selctIndex1=0;//will contain the index of the index (list)
    private Color currClr=Color.red;
    private int x1,x2,y1,y2;
    private point p;
    private int count=0;
    private final ArrayList< shape > drwanShapes = new ArrayList< shape >();//shapes the are to be drawen must be add here
    private class listHandler implements ListSelectionListener
    {
           public void valueChanged(ListSelectionEvent ev)
           {
              if(list.getSelectedValue()==shapesList[0])
              {  
                  selctIndex1=0;
                   System.out.print(selctIndex1);


              }
              if(list.getSelectedValue()==shapesList[1])
              {   
                  selctIndex1=1;
                   System.out.print(selctIndex1);
              }
              if(list.getSelectedValue()==shapesList[2])
              {   
                  selctIndex1=2;
                  System.out.print(selctIndex1);
              }
           }
    }

    private class comboBoxHandler implements ItemListener
    {
        public void itemStateChanged(ItemEvent ev)
        {
          if(bx1.getSelectedIndex()==0)
          {
              
              currClr=Color.red;
              shapesList[selctIndex1].setColor(currClr);
          }
           if(bx1.getSelectedIndex()==1)
          {
            
              currClr=Color.yellow;
             shapesList[selctIndex1].setColor(currClr);
          }
           if(bx1.getSelectedIndex()==2)
          {
           
              currClr=Color.green;
              shapesList[selctIndex1].setColor(currClr);
          }
           if(bx1.getSelectedIndex()==3)
          {
              
              currClr=Color.BLUE;
              shapesList[selctIndex1].setColor(currClr);
          }
           if(bx1.getSelectedIndex()==4)
          {
             
              currClr=Color.black;
              shapesList[selctIndex1].setColor(currClr);
          }

        }
    }

    public drawingSapes()
   {
        super("              shapes drawing application");
        c=getContentPane();
        pan1= new JPanel();//set pan1 to flow layout which will have the components list and compo box
        pan1.setLayout(new FlowLayout());
        l1=new JLabel("Select a shape and a color ");
        list= new JList(shapesList);
        bx1= new JComboBox(clr);
        pan1.add(l1);
        pan1.add(list);
        pan1.add(bx1);
        pan1.setBackground(Color.red);
        listHandler lh= new listHandler();
        comboBoxHandler cbx= new comboBoxHandler();
        list.addListSelectionListener(lh);
        bx1.addItemListener(cbx);
        //***************************************
        c.setLayout(new BorderLayout());
       

        pan2= new JPanel();
        pan2.setLayout(null);
        pan2.setBackground(Color.white);
        pan2.addMouseMotionListener(this);
        //*********************************************
        c.add(pan1,BorderLayout.NORTH);
        c.add(pan2);
        setSize(600,600);
        setVisible(true);
    }
//end of the constructor

    public void mouseClicked( final MouseEvent event ) {
      
//         x1=getX();
//         y1=getY();
//         drwanShapes.add(shapesList[selctIndex1]);
//         repaint();
    }

// handle event when mouse pressed
    public void mousePressed( final MouseEvent event ) {
        int x=event.getPoint().x;
        int y=event.getPoint().y;
        shapesList[selctIndex1].setFirstPoint(x, y);
        int x2=x1+20;
        int y2=y1+30;
        shapesList[selctIndex1].setSecondPoint(x2, y2);
        drwanShapes.add(shapesList[selctIndex1]);
        x2=x2+5;
        y2=y2+5;
        repaint();
    }

// handle event when mouse released after dragging
    public void mouseReleased( final MouseEvent event ) {
         
    }



// handle event when mouse enters area
    public void mouseEntered( final MouseEvent event ) {

    }

// handle event when mouse exits area
    public void mouseExited( final MouseEvent event ) {

    }


    // MouseMotionListener event handlers // handle event when user drags mouse with button pressed
    public void mouseDragged( final MouseEvent event ) {
      
       repaint();
    }

// handle event when user moves mouse
    public void mouseMoved( final MouseEvent event ) {
    //do nothing
    }
     //the paint method will draw the shapes that have been added to the array list
    public void paint(Graphics g)
    {
         super.paint(g);
         System.out.print(selctIndex1);
         drwanShapes.get(0).draw(g);
    }

    public static void main(String[] args) {
       drawingSapes shape= new drawingSapes();
       shape.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

}

Edited 4 Years Ago by moonL!ght: n/a

i will pm you the other class if you can help me i will be thankful

Your posted code looks like it will always drawn the same (the first) shape in the arraylist.
Why have a list if you only use one shape?
What is the purpose of the selctIndex1 variable?

The first thing I get when I execute the code is this:

Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
	at java.util.ArrayList.RangeCheck(Unknown Source)
	at java.util.ArrayList.get(Unknown Source)
	at DrawingShapes.paint(DrawingShapes.java:194)

The paint method is trying to use a non-existent element in the arraylist???

the idea is to draw shapes polymorphicaly each time the user selects a shape from the list to draw a spicified shape and that index is to know what shape to draw...the array list was a suggestion i i used the array of shapes directly it worked but allowing me to draw only one thing at a time

yah this error i didnt know what dose it mean...i think i would return to my first thought that is use shapesList directly in the paint.

Your posted code looks like it will always drawn the same (the first) shape in the arraylist.
Why have a list if you only use one shape?
What is the purpose of the selctIndex1 variable?

What about the exception when the program starts?

You need to go back and redesign your program.
Where/how are shapes created?
Where are they saved?
How are they to be drawn?

Your current code is a mess that needs to be rewritten.

oh my fault i was trying to know what is the error
beacuse the idea is that i but int count to count nuber of shapes each time the user shoose
a shape and then do this thing :

super.paint(g);
         System.out.print(selctIndex1);
         drwanShapes.get(++count).draw(g);

were it also gives me the same error endex out of bound.

Stop just changing the code. Its a mess.

You need to go back and redesign your program.
Where/how are shapes created?
Where are they saved?
How are they to be drawn?

Your current code is a mess that needs to be rewritten.

This article has been dead for over six months. Start a new discussion instead.