Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: how to send variable to different class by using extends

  1. #1
    Member
    Join Date
    Oct 2012
    Posts
    133
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default how to send variable to different class by using extends

    problem is that picture is not being printed on screen from enemy_1 class.

    i have a class enemy where i am setting up main enemy stuff. for ex collision and move bc all enemy will have the same move and collision.
    than i want to create another class called enemy_1 where i will upload different images.
    my goal is to set variable from Enemy class to Enemy_1 class.


    first here is a Enemy class
    public class Enemy 
    {
    	private int x;
    	private int y;
    	private int width = 30;
    	private int height = 40;
     
    	public Enemy(int ix, int iy)
    	{
    		x = ix;
    		y = iy;
    	}
     
    	public int getX()
    	{ return x; }
    	public void setX(int value) 
    	{ this.x =  value; }
     
    	public int getY()
    	{ return y; }
    	public void setY(int value) 
    	{ this.y =  value; }
     
    	public int getWIDTH()
    	{ return width; }
    	public void setWIDTH(int value) 
    	{ this.width =  value; }
     
    	public int getHEIGHT()
    	{ return height; }
    	public void setHEIGHT(int value) 
    	{ this.height =  value; }
     
     
     
    	/*** update method ****/
    	/*** player move ***/
    	public void ENEMY_MOVE(Main m, Sprite_Sheet s)
    	{
    		Random r = new Random();
    		//x -= 1;
     
    		if(x+width < 0)
    		{
    			x = m.getWidth() + r.nextInt(200);
    		}
    	}
     
     
     
    	/*** player + enemy collion ***/
    	public void PLAYER_ENEMY_COLLISION(Main m, Player p, Enemy e)
    	{
    		int playerX = p.getX();
    		int playerY = p.getY();
    		int playerW = p.getWIDTH();
    		int playerH = p.getHEIGHT();
     
     
    		if(playerX + playerW >= x && playerX <= x + width)
    		{
    		    if(playerY+playerH >= y && playerY <= y+height)
    		    {
    		        if (playerX <= x)  //player on left 
    		        {
    		        	//player has to be touching enemy and player has to be on left of enemy
    		        	p.setX(x - playerW);
    		        }
    		        else if(playerX >= x)//player on right
    		        {
    		        	//player has to be touch enemy and player has to be on right of enemy
    		        	p.setX(x + width);
    		        }
    		    }
    		}
    	}/*** end of PlAYER_ENEMY_COLLISION METHOD ***/
     
     
     
     
    	/*** paint method ***/
    	public void paint(Graphics g, Enemy e) 
    	{
    	}/*** paint method ***/
     
    }











    now i am extending the main enemy class.

    public class Enemy_1 extends Enemy
    {
    	private static ImageIcon enemy_image = new ImageIcon("Image/enemy/ch2.png");
     
     
    	public  Enemy_1(int ix, int iy)
    	{
    		super(ix,iy);
    	}
     
    	/*** get/set methods ***/
    	public static Image getEIMAGE()  
    	{ return enemy_image.getImage(); }
     
     
     
    	@Override
    	public void enemy_action(Player p, Score s)
    	{
     
    	}
     
     
     
    	@Override
    	public void paint(Graphics g, Enemy e)
    	{
    		g.drawImage(this.getEIMAGE(), e.getX(), e.getY(), e.getWIDTH(), e.getHEIGHT(), null); 
    		super.paint(g, e); //call enemy paint method
    	}
    }

    when i put every thing in enemy class than it works fine but when i create two class it doesnt work.

    problem is in enemy_1 class. when i run this picture is not being printed. i am not sure but i think i am not sure paint method in enemy_1 class.


    paint from enemy
    /*** paint method ***/
    	public void paint(Graphics g, Enemy e) 
    	{
    	}/*** paint method ***/


    paint from enemy_1
    @Override
    	public void paint(Graphics g, Enemy e)
    	{
    		g.drawImage(this.getEIMAGE(), e.getX(), e.getY(), e.getWIDTH(), e.getHEIGHT(), null); 
    		super.paint(g, e); //call enemy paint method
    	}

    let me now if you need more information


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: how to send variable to different class by using extends

    how to send variable to different class by using extends
    The keyword extends does not have the ability to send anything.
    When a class extends another class, say B extends A for example, it is like doing a copy of everything inside class A and a paste into class B. All variables and methods are completely visible in B and B is everything A ever becomes, plus what is added in B. So if you are trying to pass a value from Enemy to Enemy_1, it is already there.

    when i run this picture is not being printed.
    This is a symptom, not a problem. Many problems can cause this symptom. Do some troubleshooting to see what the problem is. Use printlns to verify the values of variables as well as to verify when certian methods are being called, if they ever do get called.

    i am not sure but i think i am not sure
    ..No one else is sure either. Perhaps some proofreading and a well thought out questin the future will make a reply easier.

    let me now if you need more information
    You made some statements and assumptions about what you think is wrong, but you never really asked a question in the entire post. What reply do you expect from this? A complete working program? What if I just typed "Mickey Mouse". That is as likely to fit the unasked question as well as any other phrase I can think of. Please read "How to ask questions the smart way" and your experience on this site will be greatly improved.

  3. The Following 2 Users Say Thank You to jps For This Useful Post:

    curmudgeon (November 2nd, 2012), hwoarang69 (November 2nd, 2012)

  4. #3
    Member
    Join Date
    Oct 2012
    Posts
    133
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: how to send variable to different class by using extends

    I see so lets say if i have 2 class. A and B:


    class A
    private int ax = 2;



    class B extends A
    //here B has every thing from A also B
    //and i want to get the value of ax here. than i can just call ax? ex:
    ax = 4;
    //also i think i might have to change private to protected or get rid of private

  5. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: how to send variable to different class by using extends

    ..and what happened when you tried it?

  6. The Following User Says Thank You to jps For This Useful Post:

    hwoarang69 (November 2nd, 2012)

  7. #5
    Member
    Join Date
    Oct 2012
    Posts
    133
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: how to send variable to different class by using extends

    got it working now. i forgot to call class b in main.

    thanks

Similar Threads

  1. is it possible to extends java class with another class from different package?
    By fredsilvester93 in forum Java Theory & Questions
    Replies: 6
    Last Post: July 20th, 2012, 03:51 PM
  2. Using variable in different class
    By ICEPower in forum Object Oriented Programming
    Replies: 13
    Last Post: May 12th, 2012, 12:32 PM
  3. [jsp/jsf] Send string(file name) to other class
    By barni120 in forum JavaServer Pages: JSP & JSTL
    Replies: 13
    Last Post: February 10th, 2012, 07:48 AM
  4. Abstract class variable help
    By star12345645 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 6th, 2012, 08:40 AM
  5. Must I add a Class Variable?
    By maress in forum Java Theory & Questions
    Replies: 1
    Last Post: February 24th, 2011, 04:40 AM