Results 1 to 11 of 11
Like Tree1Likes
  • 1 Post By jammas615

Thread: canvasObject cannot be resolved.

  1. #1
    falconfetus8 is offline Member
    Join Date
    Nov 2011
    Posts
    14
    Rep Power
    0

    Default canvasObject cannot be resolved.

    Hello. I'm trying to make a basic game engine to help me learn java. Long story short, I've run into a rather infuriating snag. On line 32 of enemyBat.java, Eclipse says the canvasObject cannot be resolved to a type. Why is this and how can I fix it?

    starterUpper.java
    Java Code:
    //imports
    import javax.swing.JFrame;
    
    
    // This class "starts up" the entire game.
    
    public class starterUpper {
    	
    	public static void main(String[] args) {
    		gameWindow windowObject=new gameWindow(); //create the game window
    		gameCanvas canvasObject=new gameCanvas(); //create the game canvas
    		
    		canvasObject.heightv=windowObject.getHeight(); //tells canvasObject what the height/width of windowObject is.  This is part of an ugly work-around.
    		canvasObject.widthv=windowObject.getWidth();
    		
    		windowObject.add(canvasObject); //add the game canvas as a component to the game window
    
    	}
    	
    }
    gameWindow.java
    Java Code:
    //imports
    import javax.swing.JFrame;
    import java.awt.FlowLayout;
    import javax.swing.JLabel;
    
    
    
    public class gameWindow extends JFrame {
    	
    	public gameWindow() {
    		super("The Game"); //sets the title of the window.
    		setLayout(new FlowLayout()); //Gives the window a default layout.
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //This makes sure the game closes when you hit the red "x" button.
    		setSize(640,480);
    		setVisible(true);
    
    	}
    
    }
    gameCanvas.java
    Java Code:
    //imports
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    //This class will be the canvas that all of the graphics are painted on.  It is going to be a component in gameWindow.
    
    public class gameCanvas extends JPanel {
    	
    	public int heightv, widthv; //These variables are used to get the height and the width of windowObject.  I need to use this for an ugly work-around.
    	
    	public void paintComponent(Graphics g){
    		super.paintComponent(g); //Necessary for Java2D to work.  Just go with it.
    		setBounds(0, 0, widthv, heightv); //Set the canvas's size the game window's size and place it at 0,0 on the window.  This is that ugly work-around I was talking about.
    		this.setBackground(Color.WHITE); //set the background color to white.
    		
    		
    	}
    }
    enemyBat.java
    Java Code:
    //imports
    import java.awt.Shape;
    import java.awt.geom.*;
    
    import javax.swing.*;
    
    //This is a simple bat enemy.
    
    public class enemyBat extends General_Enemy {
    	
    	public enemyBat(double xcreate, double ycreate){
    		//set the x and y coordinates based on the specified arguments.
    		x=xcreate;
    		y=ycreate;
    		//Set the motion variables.  They're all 0 to begin with.
    		vspeed=0;
    		hspeed=0;
    		friction=0;
    		direction=0;
    		speed=0;
    		//Set the variables that are specific to this enemy
    		health=3;
    		aggressive=Boolean.FALSE;
    	}
    	
    	public void goAround(){
    		super.goAround(); //Calls General_Enemy's goAround method.
    	}
    	
    	public void paint(){
    		Shape sprite=new Rectangle2D.Float(xround,yround,50,50); //create the shape to be drawn.  Will be changed to a png image later.
    		canvasObject.draw(sprite); //THIS IS WHERE MY PROBLEM IS.
    	}
    
    }
    Last edited by falconfetus8; 12-27-2011 at 11:19 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: canvasObject cannot be resolved.

    Your enemyBat class has no canvasObject variable that is visible within its scope, so the compiler error message is correct. If you want this class to interact with a canvasObject variable, you're going to have to give it one. You'll also need to pass in a reference to the object of interest so that this variable can refer to the correct gameCanvas object. This can be done by either giving enemyBat a setCanvasObject(gameCanvas gc) method or by passing in the reference in enemyBat's constructor.

    Oh, please refactor your code so that it follows proper Java naming convention. For instance all class names should begin with a capital letter, and all variables with a lower case letter. This way folks who are trying to help you won't have to struggle as much to understand your code.

  3. #3
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default Re: canvasObject cannot be resolved.

    canvasObject is only available in your starterUpper class (in the main method). and you never create an enemyBat obbject or? You have to pass the canvas object into the other classes if you want to use it there.

    btw: please read the java code conventions : Code Conventions for the Java(TM) Programming Language: Contents
    especially the naming conventions Naming Conventions

  4. #4
    jammas615 is offline Senior Member
    Join Date
    Jul 2011
    Location
    Melbourne, Victoria, Australia
    Posts
    155
    Rep Power
    4

    Default Re: canvasObject cannot be resolved.

    You created a gameCanvas object in your first class, "starterUpper()", and you tried to use that same instance in enemyBat that had no idea of the one you created in starterUpper therefore, you are getting your error that tells you canvasObject cannot be resolved to a type.
    Its also kind of hard to see whats going on here, is this all your code?

  5. #5
    falconfetus8 is offline Member
    Join Date
    Nov 2011
    Posts
    14
    Rep Power
    0

    Default Re: canvasObject cannot be resolved.

    Quote Originally Posted by Fubarable View Post
    Your enemyBat class has no canvasObject variable that is visible within its scope, so the compiler error message is correct. If you want this class to interact with a canvasObject variable, you're going to have to give it one. You'll also need to pass in a reference to the object of interest so that this variable can refer to the correct gameCanvas object. This can be done by either giving enemyBat a setCanvasObject(gameCanvas gc) method or by passing in the reference in enemyBat's constructor.

    Oh, please refactor your code so that it follows proper Java naming convention. For instance all class names should begin with a capital letter, and all variables with a lower case letter. This way folks who are trying to help you won't have to struggle as much to understand your code.
    Thank you. I have renamed my classes accordingly.

    How can I go about passing a reference to StarterUpper's canvasObject? Since StarterUpper is the "main class", it doesn't have an object name, so I can't refer to it. I've tried to declare windowObject and canvasObject as public variables, but when I do so Eclipse says that the only modifier I can use is "final".

  6. #6
    falconfetus8 is offline Member
    Join Date
    Nov 2011
    Posts
    14
    Rep Power
    0

    Default Re: canvasObject cannot be resolved.

    Quote Originally Posted by jammas615 View Post
    You created a gameCanvas object in your first class, "starterUpper()", and you tried to use that same instance in enemyBat that had no idea of the one you created in starterUpper therefore, you are getting your error that tells you canvasObject cannot be resolved to a type.
    Its also kind of hard to see whats going on here, is this all your code?
    No, this is not all of my code. I have two more classes, but they're only for inheritance for EnemyBat. I realize that I have not created EnemyBat yet; I need to knock out this compile error first before I can get to creating it.

  7. #7
    jammas615 is offline Senior Member
    Join Date
    Jul 2011
    Location
    Melbourne, Victoria, Australia
    Posts
    155
    Rep Power
    4

    Default Re: canvasObject cannot be resolved.

    You can pass a reference to starterUpper's canvasObject by making enemy bat take a canvasObject as a parameter and then in the constructor of enemyBat assign it to another instance of canvasObject.

    It kind of looks like this:
    Java Code:
    public class enemyBat() {
    
        CanvasObject canvasObject;
    
        public enemyBat(CanvasObject canvasobject, double xcreate, ycreate) {
            this.canvasObject = canvasObject;
        } 
    }
    And when you create your instance of enemyBat you just pass the canvasObject from starterUpper as the first parameter.
    Fubarable likes this.

  8. #8
    falconfetus8 is offline Member
    Join Date
    Nov 2011
    Posts
    14
    Rep Power
    0

    Default Re: canvasObject cannot be resolved.

    Alright, I THINK I did what you said. I also decided to make a level and put this bat enemy in said level, but I'm getting the error "testLevel cannot be resolved." This is odd, because I created said object in the same class that I am getting this error. In addition, canvasObject STILL does not seem to be working correctly, among other errors. Here is my entire source code.(I must warn you, it's pretty lengthy). I've put comments on the lines where the errors are located.


    StarterUpper.java
    Java Code:
    //This class starts up the entire game
    
    //imports
    import javax.swing.JFrame;
    
    
    public class StarterUpper {
    	
    	public static void main(String[] args) {
    		GameWindow windowObject = new GameWindow();
    		GameCanvas canvasObject = new GameCanvas();
    		
    		canvasObject.heightv = windowObject.getHeight(); //tells canvasObject what the height/width of windowObject is.  This is part of an ugly work-around.
    		canvasObject.widthv = windowObject.getWidth();
    		
    		windowObject.add(canvasObject); //add the game canvas as a component to the game window
    		
    		Test_Level testLevel = new Test_Level(canvasObject); //start the test level
    		
    		startLoop(); //initiates the main loop of the game
    		
    	}
    	
    	public static void startLoop(){ 
    		for (int i=1; i==0; i=1){
    			testLevel.goAround();  //ERROR: testLevel cannot be resolved.
    		}
    	}
    	
    }
    
    
    /* LIST OF VARIABLES AND THEIR PURPOSES
     * GameWindow windowObject: This object is the game window
     * GameCanvas canvasObject: This little pain-in-the-neck is the canvas that all the graphics will be drawn on.
     * Test_Level testLevel: This object is the "room" in which the test level takes place.  Could someone please tell me why it cannot be resolved, though?
     */
    GameWindow.java
    Java Code:
    //imports
    import javax.swing.JFrame;
    import java.awt.FlowLayout;
    import javax.swing.JLabel;
    
    
    
    public class GameWindow extends JFrame {
    	
    	public GameWindow() {
    		super("The Game");
    		setLayout(new FlowLayout());
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setSize(640,480);
    		setVisible(true);
    
    	}
    
    }
    GameCanvas.java
    Java Code:
    //imports
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    //This class will be the canvas that all of the graphics are painted on.  It is going to be a component in gameWindow.
    
    public class GameCanvas extends JPanel {
    	
    	public int heightv, widthv;
    	
    	public void paintComponent(Graphics g){
    		
    		super.paintComponent(g);
    		setBounds(0, 0, widthv, heightv); //Set the canvas's size the game window's size and place it at 0,0 on the window.  This is that ugly work-around I was talking about.
    		this.setBackground(Color.WHITE); 
    		}
    	
    	public void draw(Shape todraw){ //REMINDER: make sure to change "shape" to "image" later
    		//TODO: Figure out how the drawing works
    	}
    	
    }
    
    
    /* LIST OF VARIABLES AND THEIR PURPOSES
     * int heightv, widthv: In StarterUpper.java, these variables grab the height and width of windowObject.  This is so that I make the canvas the the same size as the window.
     */
    EnemyBat.java
    Java Code:
    //imports
    import java.awt.Shape;
    import java.awt.geom.*;
    
    import javax.swing.*;
    
    
    
    public class EnemyBat extends General_Enemy {
    	
    	public EnemyBat(GameCanvas tempCanvas, double xcreate, double ycreate){
    		
    		//Pass a reference to canvasObject.
    				GameCanvas canvasObject=tempCanvas;
    		
    		
    		//set the x and y coordinates based on the specified arguments.
    		x=xcreate;
    		y=ycreate;
    		//Set the motion variables.  They're all 0 to begin with.
    		vspeed=0;
    		hspeed=0;
    		friction=0;
    		direction=0;
    		speed=0;
    		//Set the variables that are specific to this enemy
    		health=3;
    		aggressive=Boolean.FALSE;
    	}
    	
    	public void goAround(){
    		super.goAround(); //Calls General_Enemy's goAround method.
    	}
    	
    	public void paint(){
    		Shape sprite=new Rectangle2D.Float(xround,yround,50,50); //create the shape to be drawn.  Will be changed to a png image later.
    		canvasObject.draw(sprite); //ERROR: canvasObject cannot be resolved.
    		}
    
    }
    
    
    /* LIST OF VARIABLES AND THEIR PURPOSES
     * int health: the health of the mob.  When it reaches zero, the mob is destroyed.
     * boolean aggressive: if this variable is set to true, then the enemy will attack the player.
     */
    Test_Level.java
    Java Code:
    
    public class Test_Level {
    	
    	public Test_Level(GameCanvas tempCanvas){
    		GameCanvas canvasObject=tempCanvas;
    		createMobs(); //create all of the mobs that will be in the level.	
    	}
    	
    	public void createMobs(){
    		EnemyBat bat=new EnemyBat(canvasObject, 0, 0); //ERROR: canvasObject cannot be resolved to a variable
    	}
    	
    	public void goAound(){
    		bat.goAround(); //tell the bat to do its loop //ERROR: bat cannot be resolved
    	}
    }
    General_Enemy.java
    Java Code:
    //imports
    
    //This is a general enemy.  All enemy mobs inherit this class.
    
    abstract public class General_Enemy extends General_Mob {
    	
    	//variable declarations; see list of variables and their purposes at the bottom.
    	int health;
    	boolean aggressive;
    	
    	public void General_Enemy(GameCanvas canvasObject){
    		//TODO: Not sure yet
    	}
    	
    	public void goAround(){
    		checkDead(); //kill the enemy if he's out of health.
    		super.goAround(); //Executes General_Mob's goAround() method.  Nifty, huh?
    		paint();
    	}
    	
    	private void checkDead(){ //kill the enemy when he runs out of health.
    		if (health<=0){
    			//TODO: Destroy the object
    		}
    	}
    	
    	abstract public void paint(); //paint the object on the canvas.  THIS MUST BE OVERRIDDEN!
    	}
    
    /* LIST OF VARIABLES AND THEIR PURPOSES
     * int health: the health of the mob.  When it reaches zero, the mob is destroyed.
     * boolean aggressive: if this variable is set to true, then the enemy will attack the player.
     */
    General_Mob.java
    Java Code:
    //imports
    import java.lang.Math;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    
    abstract public class General_Mob {
    	
    	//declare variables.
    	public double x, y, z, vspeed, hspeed, zspeed, friction, speed, direction, pidir;
    	public int xround, yround, zround;
    
    	public General_Mob() {
    		//currently empty.
    		}
    	
    	public void goAround(){ //This method is executed every time the loop happens
    		//Update the mob's position, speed, and friction.
    		crunchPhysics();
    		paint();
    		}
    	
    	public void crunchPhysics(){
    		crunchFriction();//Slow the mob down based on its friction
    		crunchDirection();//compute the hspeed and vspeed of the mob.
    		x += hspeed;
    		y += vspeed;
    		z += zspeed;
    		roundCoords();//Round the positions to integers so that they can be drawn on the screen.
    		}
    	
    	abstract public void paint();
    	
    	public int getX(){
    		return xround;
    	}
    	
    	public int getY(){
    		return yround;
    	}
    	
    	public int getZ(){
    		return zround;
    	}
    	
    	public double getSpeed(){
    		return speed;
    	}
    	
    	public double getHspeed(){
    		return hspeed;
    	}
    	
    	public double getVspeed(){
    		return vspeed;
    	}
    	
    	private void crunchDirection(){ //Convert "direction" and "speed" to hspeed and vspeed.  Credit for the algorithm goes to Yoyo Games.
    		pidir=Math.PI * (direction / 180);
    		hspeed=Math.cos(pidir) * speed;
    		vspeed=Math.sin(pidir) * speed * -1;
    	}
    	
    	private void crunchFriction(){ //Slow the mob down based on its friction
    		if (speed>0){
    			speed-=friction;
    		}
    		if (speed<0){
    			speed+=friction;
    		}
    		//if the speed is close enough to zero, then just set it to zero.
    		if (Math.abs(friction) >= Math.abs(speed)){
    			speed=0;
    			}
    	}
    	
    	private void roundCoords(){ //rounds the coordinates of the mob to the nearest whole number.  This is for graphical purposes only.
    		xround=(int) Math.rint(x);
    		yround=(int) Math.rint(y);
    		zround=(int) Math.rint(z);
    	}
    	
    }

  9. #9
    jammas615 is offline Senior Member
    Join Date
    Jul 2011
    Location
    Melbourne, Victoria, Australia
    Posts
    155
    Rep Power
    4

    Default Re: canvasObject cannot be resolved.

    Take a look at the example I showed you, declaring an object like I did allows for use throughout the class. Without your error. Try changing your code up to look like mine.

  10. #10
    falconfetus8 is offline Member
    Join Date
    Nov 2011
    Posts
    14
    Rep Power
    0

    Default Re: canvasObject cannot be resolved.

    Let me make sure I understood correctly: I am supposed to pass StarterUpper.canvasObject as a parameter to Test_Level, and then pass it from Test_Level to Enemy_Bat? If so, that didn't work; I did it the way you said to, but it told me that I had two identical instance variables. So, in the parameters for the constructor for Test_Level and Enemy_Bat, I changed canvasObject to tempCanvas, and then set a new canvasObject equal to tempCanvas. This ended up not working either.

    You have not explained why I am getting errors at line 18 of StarterUpper.java, line 37 of Enemy_Bat, or line 13 of Test_Level. In all three of these cases, I declared a variable inside the same class that it's being used in but am still getting an error.

    Please, if somebody could explain why this isn't working, it would be very much appreciated.

  11. #11
    jammas615 is offline Senior Member
    Join Date
    Jul 2011
    Location
    Melbourne, Victoria, Australia
    Posts
    155
    Rep Power
    4

    Default Re: canvasObject cannot be resolved.

    Here:
    You declare and assign your objects in your main method, when you try and use them in the same class but in another method, they're are not visible, hence why my example showed them delcared as fields and assigned only in the main() method.
    Java Code:
    public class StarterUpper {
         
        public static void main(String[] args) {
            GameWindow windowObject = new GameWindow(); //This one
            GameCanvas canvasObject = new GameCanvas(); //This one
             
            canvasObject.heightv = windowObject.getHeight();
            canvasObject.widthv = windowObject.getWidth();
             
            windowObject.add(canvasObject); //add the game canvas as a component to the game window
             
            Test_Level testLevel = new Test_Level(canvasObject); //start the test level // This one
             
            startLoop(); //initiates the main loop of the game
             
        }
    
    public void anotherMethod() {
        testlevel.goAround(); //Cannot be resolved because it was delcared in main()
    }
    }
     }
    
    This is better:
    
    public class StarterUpper {
        
        GameWindow windowObject;
        GameCanvas canvasObject;
        Test_Level test_level;
    
        public static void main(String[] args) {
            windowObject = new GameWindow();
            canvasObject = new GameCanvas();
    
            //
           //Other stuff
    
           test_level = new Test_Level(canvasObject);
           
        }
    }
    Now do you see what i mean?

    This is the same scenario in your other classes, and what i was trying to say in my other post. What i meant by looking at my example was to literally look at it and help you to see the error.
    Last edited by jammas615; 12-31-2011 at 12:46 AM.

Similar Threads

  1. HttpServlet Cannot be resolved to a type
    By chizbox in forum Eclipse
    Replies: 3
    Last Post: 07-01-2012, 06:09 PM
  2. Array cannot be resolved
    By aqeel2010 in forum New To Java
    Replies: 4
    Last Post: 10-07-2011, 07:38 PM
  3. HttpServletRequest cannot be resolved-- Help me
    By hphuoc in forum Java Servlet
    Replies: 1
    Last Post: 04-26-2011, 10:23 AM
  4. HibernateUtil cannot be resolved
    By fierof2 in forum Advanced Java
    Replies: 14
    Last Post: 02-01-2011, 03:24 PM
  5. Progress bar no updating resolved
    By Haarp425 in forum Threads and Synchronization
    Replies: 2
    Last Post: 06-25-2010, 03:28 AM

Posting Permissions

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