array of objects

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Norgy
    New Member
    • May 2013
    • 56

    array of objects

    i am trying to add a new object to a normal member inner class named MyLine
    here is the inner class:
    Code:
    class MyLine{
    
    		public void add(int xx1, int yy1, int xx2, int yy2){
    			x1=xx1;
    			x2=xx2;
    			y1=yy1;
    			y2=yy2;
    		}
    		public int getX1(){return x1;}
    		public int getX2(){return x2;}
    		public int getY1(){return y1;}
    		public int getY2(){return y2;}
    		
    	}
    when i try to add a new object from a method in the outer class like this:
    Code:
    public void mouseReleased(MouseEvent e){
    			obj[i] = new MyLine();
    			obj[i].add(x1,y1,x2,y2);
    			i+=1;
    		       System.out.println(i+""+obj[0].getX1());
    			
    			repaint();
    		}
    and then when i print the value of obj[0].getX1(), it seems like it is changed every time the mouse is released

    here is the method of paint() that is called when i call the repaint() method
    Code:
    public void paint(Graphics g){
    		
    			for(int j=0 ; j<i ; j++){
    				g.setColor(Color.red);
    				g.drawLine(obj[j].getX1(),obj[j].getY1(),obj[j].getX2(),obj[j].getY2());
    			}
    		
    	}
    it draws obj[0] each time with a different value and i don't know why
Working...