Results 1 to 14 of 14
  1. #1
    Codeless is offline Member
    Join Date
    Nov 2010
    Posts
    37
    Rep Power
    0

    Default Where is this method located / why can't I use it?

    All I want to do is transfer this to another class
    Java Code:
    for( int i = 0 ; i < tileset_ground.length ; i++ )
            {
                tileset_ground[i] = new ImageIcon("Resources/tileset_ground.png").getImage();
                tileset_ground[i] = createImage(new FilteredImageSource(tileset_ground[i].getSource(), new CropImageFilter(0, 26 * i, 26, 26))); // crop image
            }
             
            for( int i = 0 ; i < tileset_air.length ; i++ )
            {
                tileset_air[i] = new ImageIcon("Resources/tileset_air.png").getImage();
                tileset_air[i] = createImage(new FilteredImageSource(tileset_air[i].getSource(), new CropImageFilter(0, 26 * i, 26, 26)));
            }
    however I get "The method createImage is undefined for the type newClass" I didnt actually call it newClass but you get the point

    right well, I copy all the imports from my previous class and still it does not recognise it... I look it up and it should be in java.awt.*; but it's not recognised... and no its not defined in the old class file itself. what gives, why is createImage undefined here but works fine in the old class with the same imports? I havent typed it wrong or anything so what could the problem be?

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

    Default Re: Where is this method located / why can't I use it?

    Maybe because your old class is a subclass of java.awt.Component ? Or a subclass of a subclass ...? :)
    (e.g. its a subclass of JPanel/Panel)

  3. #3
    Codeless is offline Member
    Join Date
    Nov 2010
    Posts
    37
    Rep Power
    0

    Default Re: Where is this method located / why can't I use it?

    the old class extends JPanel and therefore is a JPanel but I tried that with the new class and the compilation error insists. This doesnt make sense! I don't see how anyone could be a programmer.

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

    Default Re: Where is this method located / why can't I use it?

    Why are you assigning a value to the tileset_ground[i] twice anyway (and same for tileset_air[i])? By doing this, even if things worked, you'd just be tossing out whatever was assigned initially.

  5. #5
    Codeless is offline Member
    Join Date
    Nov 2010
    Posts
    37
    Rep Power
    0

    Default Re: Where is this method located / why can't I use it?

    what do you mean? the first assignment is just a temporary value which becomes an input value of the second assignment?

    do you mean replace tileset_air[i].getSource() of the second assignment with new ImageIcon("Resources/tileset_air.png").getImage(); of the first? that would remove the number of operations but wouldnt it be less readable?

    still cant move this code FOR SOME REASON. I notice I say FOR SOME REASON a lot when it comes to java

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

    Default Re: Where is this method located / why can't I use it?

    OK, I see what you mean. If this were my code, I'd create a local variable to hold the temporary object to avoid confusion. As for your problem you'll need to give us more context on the problem, perhaps by showing us more of the class that holds this method, in particular the class's signature line.

  7. #7
    Codeless is offline Member
    Join Date
    Nov 2010
    Posts
    37
    Rep Power
    0

    Default Re: Where is this method located / why can't I use it?

    well its just
    Java Code:
    public class Screen extends JPanel implements Runnable
    {
       public Screen()
       {
          gameloop.start()
       }
    }
    run calls repaint, which calls define which calls the code

    I want to move it to a sprite class called with a method instead

    like I said I tried the same imports, extended JPanel but the error remains unresolved type

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

    Default Re: Where is this method located / why can't I use it?

    Again, please show us the class and method that your for loop above is located in.

  9. #9
    Codeless is offline Member
    Join Date
    Nov 2010
    Posts
    37
    Rep Power
    0

    Default Re: Where is this method located / why can't I use it?

    Sorry I thought class signature line was the constructor

    anyway, here's the original class and you can see I want to move the code to the Sprite class

    Java Code:
    package pkg;
    
    import java.awt.*;
    import java.awt.image.*;
    import javax.swing.*;
    import java.io.*;
    
    public class Screen extends JPanel implements Runnable
    {
    	//the screen object fits into the window and contains the game loop
    	public Thread gameLoop = new Thread(this);
    	public Room room;
    	public Save save;
    	
    	public int width, height;	
    	public boolean isFirst = true;
    	
    	public Screen()
    	{
    		gameLoop.start();
    	}
    	
    	
    	public void define()
    	{
    		//load images
    		room = new Room(this);
    		save = new Save();
    		//Sprite.loadTextiles();
    		
    		for( int i = 0 ; i < Sprite.tileset_ground.length ; i++ )
    		{
    			//fill the image array with 100 images
    			//imageIcon(file)
    			//image(image producer(imageIcon, filter)
    			Sprite.tileset_ground[i] = new ImageIcon("Resources/tileset_ground.png").getImage();
    			Sprite.tileset_ground[i] = createImage(new FilteredImageSource(Sprite.tileset_ground[i].getSource(), new CropImageFilter(0, 26 * i, 26, 26))); // crop image
    		}
    
    		for( int i = 0 ; i < Sprite.tileset_air.length ; i++ )
    		{
    			Sprite.tileset_air[i] = new ImageIcon("Resources/tileset_air.png").getImage();
    			Sprite.tileset_air[i] = createImage(new FilteredImageSource(Sprite.tileset_air[i].getSource(), new CropImageFilter(0, 26 * i, 26, 26)));
    		}
    		
    		save.loadSave(new File("save/mission1.td"), this);
    	}
    	
    	public void paintComponent(Graphics g)
    	{
    		//this method is called by default
    		if(isFirst)
    		{
    			width = getWidth();
    			height = getHeight();
    			define();
    			isFirst = false;
    		}
    		
    		// draws out a blank space on the screen which clears it
    		g.clearRect(0, 0, getWidth(), getHeight());
    		
    		room.draw(g);
    	}
    	
    	public void run()
    	{
    		while(true)
    		{		
    			if(!isFirst)
    			{
    				room.physic();
    			}
    				
    			repaint();
    			
    			try
    			{
    				// set frame rate 1 milisecond
    				Thread.sleep(1);
    			}
    			catch(Exception e)
    			{
    				
    			}
    			
    		}
    	}
    }
    the createImage method is unresolved for type Sprite below and I don't know why
    Java Code:
    package pkg;
    
    import java.awt.*;
    import java.awt.Image;
    import java.awt.image.CropImageFilter;
    import java.awt.image.FilteredImageSource;
    import javax.swing.ImageIcon;
    
    import java.awt.*;
    import java.awt.image.*;
    import javax.swing.*;
    import java.io.*;
    import java.awt.Component.*;
    
    public final class Sprite
    {
    	//create image arrays
    	public static Image[] tileset_ground = new Image[100];
    	public static Image[] tileset_air = new Image[100];
    	
    	public static int groundGrass = 0;
    	public static int groundRoad = 1;		
    	public static int airAir = -1;
    	
    	public static void loadTextiles()
    	{
    		for( int i = 0 ; i < Sprite.tileset_ground.length ; i++ )
    		{
    			//fill the image array with 100 images
    			//imageIcon(file)
    			//image(image producer(imageIcon, filter)
    			Sprite.tileset_ground[i] = new ImageIcon("Resources/tileset_ground.png").getImage();
    			Sprite.tileset_ground[i] = createImage(new FilteredImageSource(Sprite.tileset_ground[i].getSource(), new CropImageFilter(0, 26 * i, 26, 26))); // crop image
    		}
    
    		for( int i = 0 ; i < Sprite.tileset_air.length ; i++ )
    		{
    			Sprite.tileset_air[i] = new ImageIcon("Resources/tileset_air.png").getImage();
    			Sprite.tileset_air[i] = createImage(new FilteredImageSource(Sprite.tileset_air[i].getSource(), new CropImageFilter(0, 26 * i, 26, 26)));
    		}
    	}
    }
    remember making it a jpane didnt work

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

    Default Re: Where is this method located / why can't I use it?

    Well of course it doesn't recognize the method. Sprite extends nothing, and you're calling it from a static method. For an inherited method to work you must use inheritance, meaning the method must be called in a class which extends a class that owns the method, and the method must be called within a non-static method of the class, as in the JPanel example above, and which is missing on both points from your Sprite example. Perhaps you don't want to use this method at all. Perhaps you want to create your own method to extract the image.
    Last edited by Fubarable; 11-03-2012 at 02:49 PM.

  11. #11
    Codeless is offline Member
    Join Date
    Nov 2010
    Posts
    37
    Rep Power
    0

    Default Re: Where is this method located / why can't I use it?

    Sorry I am trying to understand here, bare with me please I have questions

    1) So createImage is an inherited method of JPanel (or superclass of)?
    eclipse tells me java.awt.Component.createImage is it's fully qualified name. why is it importing java.awt.* does not make this available by composition?

    2) why must it be called within a non-static method?
    if class screen extends Jpanel, why can't it have its own static method containing methods inherited from Jpanel(or superclass of)
    -> is it because a static method belongs to the class and only instances inherit?

    and thanks for your help

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

    Default Re: Where is this method located / why can't I use it?

    Quote Originally Posted by Codeless View Post
    Sorry I am trying to understand here, bare with me please I have questions

    1) So createImage is an inherited method of JPanel (or superclass of)?
    eclipse tells me java.awt.Component.createImage is it's fully qualified name. why is it importing java.awt.* does not make this available by composition?
    That's not how composition works as composition doesn't work for methods but rather for objects. Big difference. If all you want to do is crop the Image, why not just create a BufferedImage and use one of its methods to get a sub-image?

    2) why must it be called within a non-static method?
    if class screen extends Jpanel, why can't it have its own static method containing methods inherited from Jpanel(or superclass of)
    -> is it because a static method belongs to the class and only instances inherit?
    static methods are not inherited.

  13. #13
    Codeless is offline Member
    Join Date
    Nov 2010
    Posts
    37
    Rep Power
    0

    Default Re: Where is this method located / why can't I use it?

    ok I understand now thanks for explaining.

    on a side note I got the map of my tower defence game working, I made a path and everything. this code was a part of that.

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

    Default Re: Where is this method located / why can't I use it?

    Glad you've got it working.

Similar Threads

  1. File IO - where do the txt files need to be located?
    By runnerandreader in forum New To Java
    Replies: 1
    Last Post: 11-23-2011, 04:52 AM
  2. Tomcat Webapps directory located in eclipse
    By ajincoep in forum New To Java
    Replies: 0
    Last Post: 02-18-2011, 06:58 AM
  3. Replies: 5
    Last Post: 11-14-2010, 04:28 PM
  4. Senior Java Software Engineer- Located in NYC
    By lecgerken in forum Jobs Offered
    Replies: 0
    Last Post: 04-05-2010, 04:22 PM
  5. Replies: 1
    Last Post: 02-28-2010, 03:06 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
  •