Results 1 to 2 of 2
  1. #1
    lucashan is offline Member
    Join Date
    Feb 2012
    Posts
    1
    Rep Power
    0

    Default how to get canvas width of another object

    hello,

    I would like to know how an object1 can know the width of the canvas of object2 that object1 is going to be put on.

    for example, I have a 'Ball' class which extends GOval and then I have another 'Pool' class which 'ball' will be added in it.

    Java Code:
    public class Pool {
    	
    	public void run(){
    		Ball ball = new Ball(30,30);
    		ball.setFilled(true);
    		
    		add(ball,0,0);
    		ball.go(5);
    		
    	
    
    }
    }

    Java Code:
    public class Ball extends GOval {
    	
    	private int ball_speed; //speed of ball rolling
    	
    	public GOval(double width, double height) {
    		super(width, height);
    		// TODO Auto-generated constructor stub
    	}
    	
    	public void go(int speed){
    		while(this.getX()<(pool.getWidth()-this.getWidth)){ //ball rolls to the end
    		ball_speed = speed;
    		this.move(ball_speed, 0);
    		pause(500);
    	
    		}
    	}
    	
    }
    the 'pool.getWidth()' in line 11 is what i mean the width of the canvas(or whatever it is), is there any way i can get it without passing parameter from pool's class.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: how to get canvas width of another object

    any way i can get it without passing parameter from pool's class.
    To get at the contents of an object, you need a reference to that object.
    Somehow you need to get the reference.

    Or you could pass the needed value as a parameter to the go() method.

Similar Threads

  1. How do I change canvas of a BufferedImage object?
    By Pojahn_M in forum New To Java
    Replies: 2
    Last Post: 10-26-2011, 05:01 PM
  2. Replies: 3
    Last Post: 05-02-2011, 04:37 AM
  3. Object Draw canvas Text
    By kyle.g in forum New To Java
    Replies: 0
    Last Post: 07-27-2010, 09:54 PM
  4. How to set line width in Graphics object?
    By inc_123 in forum New To Java
    Replies: 8
    Last Post: 08-15-2009, 10:34 PM
  5. Replies: 0
    Last Post: 07-05-2009, 12:31 PM

Posting Permissions

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