Results 1 to 14 of 14
- 11-02-2012, 05:10 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 37
- Rep Power
- 0
Where is this method located / why can't I use it?
All I want to do is transfer this to another class
however I get "The method createImage is undefined for the type newClass" I didnt actually call it newClass but you get the pointJava 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))); }
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?
- 11-02-2012, 05:18 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
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)
- 11-02-2012, 05:30 PM #3
Member
- Join Date
- Nov 2010
- Posts
- 37
- Rep Power
- 0
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.
-
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.
- 11-02-2012, 06:00 PM #5
Member
- Join Date
- Nov 2010
- Posts
- 37
- Rep Power
- 0
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
-
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.
- 11-02-2012, 06:22 PM #7
Member
- Join Date
- Nov 2010
- Posts
- 37
- Rep Power
- 0
Re: Where is this method located / why can't I use it?
well its just
run calls repaint, which calls define which calls the codeJava Code:public class Screen extends JPanel implements Runnable { public Screen() { gameloop.start() } }
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
-
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.
- 11-03-2012, 02:35 PM #9
Member
- Join Date
- Nov 2010
- Posts
- 37
- Rep Power
- 0
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
the createImage method is unresolved for type Sprite below and I don't know whyJava 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) { } } } }
remember making it a jpane didnt workJava 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))); } } }
-
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-03-2012, 03:25 PM #11
Member
- Join Date
- Nov 2010
- Posts
- 37
- Rep Power
- 0
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
-
Re: Where is this method located / why can't I use it?
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?
static methods are not inherited.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?
- 11-03-2012, 06:31 PM #13
Member
- Join Date
- Nov 2010
- Posts
- 37
- Rep Power
- 0
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.
-
Similar Threads
-
File IO - where do the txt files need to be located?
By runnerandreader in forum New To JavaReplies: 1Last Post: 11-23-2011, 04:52 AM -
Tomcat Webapps directory located in eclipse
By ajincoep in forum New To JavaReplies: 0Last Post: 02-18-2011, 06:58 AM -
method to check which type of object with which name is located at given coordinates
By stringargs in forum Java 2DReplies: 5Last Post: 11-14-2010, 04:28 PM -
Senior Java Software Engineer- Located in NYC
By lecgerken in forum Jobs OfferedReplies: 0Last Post: 04-05-2010, 04:22 PM -
Help. Find a word and located sentences in the file
By rashad in forum New To JavaReplies: 1Last Post: 02-28-2010, 03:06 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks