1

I have created 2 objects of the class GConveyor. I want to get some data about the objects when I select them, but both return the same information. Thanks for the help!

/* 
* File: DragObjects.java  
*/ 
import acm.graphics.*; 
import acm.program.*; 
import java.awt.event.*;

public class DragObjects extends GraphicsProgram { 
private static final long serialVersionUID = 1L;
// Initializes the program 
public void init() { 
    //adding a few test objects
    GConveyor conv = new GConveyor(50, 44, 120); 
    add(conv); 
    GConveyor conv2 = new GConveyor(30, 24, 60); 
    add(conv2); 

    addMouseListeners(); 
    addKeyListeners(); 
    } 

// Called on mouse press to record the coordinates of the click */ 
public void mousePressed(MouseEvent e) { 
    // GPoint has X and Y coordinate 
    last = new GPoint(e.getPoint()); 
    gobj = getElementAt(last);  
    //looking at stuff so to understand
    getObjectData ();
}


public void getObjectData (){
    //looking at stuff so to understand
    System.out.println(gobj.toString());    

    GContainer obLength = gobj.getParent();
    System.out.println(obLength.toString());    

    int d = ((GCompound) gobj).getElementCount();
    System.out.println( "Element per GConveyor:"+ d  );

    System.out.println( "hashcode:" +gobj.hashCode());

    int y = this.getElementCount();
    System.out.println( "this Element Count:"+ y  );

    int a = GConveyor.getBF();      
    System.out.println( "bf:" + a  );
    int b = GConveyor.getEF();      
    System.out.println( "ef:" + b  );
    int h = GConveyor.getLength();      
    System.out.println( "length:" + h  );
    int c = ((GConveyor) gobj).getLength();     
    System.out.println( "length:" + c  );       
}



// Called on mouse drag to reposition the object 
public void mouseDragged(MouseEvent e) { 
    if (gobj != null) { 
        gobj.move(e.getX() - last.getX(), e.getY() - last.getY()); 
        last = new GPoint(e.getPoint());
        } } 

/* Private instance variables */
private GObject gobj; 
/* The object being dragged */
private GPoint last; 

}

the following is my GConveyor class

 /* 
 * 
 * File: GConveyor.java * This class implements a conveyor as a GCompound.
 */ 

import java.awt.Color;
import acm.graphics.*; 

public class GConveyor extends GCompound {
/* Constants specifying frame size*/
private static final int FRAME_WIDTH = 2;
private static int myBf;
private static int myEf;
private static int myLength;

private static final double scale = 10;
/* Private instance variables */
private GRect outer;
private GRect chain_box;
private GRect effWidth;
private GPolygon pti;
private GPolygon pto;

public GConveyor(int bf, int ef, int length) {
outer = new GRect(length, ((FRAME_WIDTH *2) + bf));
chain_box = new GRect(length , bf - ef);
effWidth = new GRect(length, bf);
pti = createAnchor(scale);
pto = createAnchor(scale);
add(outer, 0 , 0);
add(chain_box, 0 , FRAME_WIDTH);
add(effWidth, 0 , FRAME_WIDTH);
add(pti, 0 , FRAME_WIDTH + (bf-ef) + (ef/2));
add(pto, length , FRAME_WIDTH + (bf-ef) + (ef/2));

myBf = bf;
myEf = ef;
myLength = length;

}

public static int getBF(){
return myBf;
}
public static int getEF(){
return myEf; 
}
public static int getLength(){
return myLength;
}


/* Creates a hex for the Anchor */
private GPolygon createAnchor(double scale) {
GPolygon poly = new GPolygon();
poly.addVertex(-0.25*scale, 0.433*scale);
poly.addVertex(-0.5*scale, 0.0*scale);
poly.addVertex(-0.25*scale, -0.433*scale);
poly.addVertex(0.25*scale, -0.433*scale);
poly.addVertex(0.5*scale, 0.0*scale);
poly.addVertex(0.25*scale, 0.433*scale);
poly.setFilled(true);
poly.setFillColor(Color.BLUE);
return poly;
}

public String toString(){
return("" +myBf+ +myEf+ +myLength+ "");

}

}
2
  • GConveyor wouldn't be a static class would it? Commented Mar 6, 2014 at 2:08
  • I have some static methods Commented Mar 6, 2014 at 2:14

3 Answers 3

2

These variables are static, so these single variables (and their singular values) will be used in all instances of your GConveyor class. That is why you are getting the same information.

private static final int FRAME_WIDTH = 2;
private static int myBf;
private static int myEf;
private static int myLength;

Eliminate the static keyword where you don't need them (where unique information is required in each instance), and you should be good.

Sign up to request clarification or add additional context in comments.

2 Comments

static is probably OK for FRAME_WIDTH. But not the others.
Yes. true. Leave FRAME_WIDTH as static.
2

myBF, myEF, and myLength are declared static in GConveyor. That means there is only one of each of those integers, and each one is shared by all objects of the class. When you call the GConveyor constructor to create a second instance, the constructor sets those static fields, which then affect the values returned for the first instance.

Comments

1

Your toString() spits out myBf myEf and myLength, all of which are static. That means they are properties of the class itself (of GConveyer), and not of instances of the class (conv and conv2), so when you call an instance's setter method for any of those, you are modifying the same variable.

You have 2 instances, but only 1 class, so you only have 1 set of those variables, not 2.

Do not make them static if you wish them to be attributed to each instance, and not to the class as a whole.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.