This is quite odd, because I have a Character class that can access Frame.dimension.getWidth(); and its partner getHeight(), however when I wanted to use this in the Map class eclipse underlines it and cannot give me feedback. Running the program anyways ends up in java errors stating they can't find the X value of a Map object.
Here's the Frame class:
package Core;
import java.awt.Dimension;
import java.awt.Rectangle;
import javax.swing.JFrame;
public class Frame
{
static int width = 800, height = 600;
static Dimension dimension;
public static void main(String[]args){
JFrame frame= new JFrame("RETRO");
frame.add(new Screen());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(width,height);
frame.setVisible(true);
frame.setResizable(false);
dimension = frame.getContentPane().getSize();
}
}
And the Map class:
package Core;
import java.awt.Frame;
import java.awt.Image;
import java.awt.Rectangle;
import javax.swing.ImageIcon;
import java.awt.Dimension;
public class Map
{
double x, y;
Image map;
ImageIcon background = new ImageIcon("images/Maps/maze.jpg");
public Map(){
map = background.getImage();
}
public void move(double moveX, double moveY){
x += moveX;
y += moveY;
}
//////////Gets
public Image getMap(){
return map;
}
public double getX(){
if(x<0)
x=0;
else if(x>Frame.dimension.getWidth())
x=Frame.dimension.getWidth();
return x;
}
public double getY(){
if(y<0)
y=0;
else if(y>Frame.dimension.getHeight())
y=Frame.dimension.getHeight();
return y;
}
public int getHeight(){
return map.getHeight(null);
}
public int getWidth(){
return map.getWidth(null);
}
}
I could provide the Character class but it is very long and messy at the moment.. however the word Frame was only used in the calling of Frame.dimension.getWidth()/getHeight();
Your
Map
class is referring tojava.awt.Frame
(which you imported), notCore.Frame
.If you really need to keep the
import java.awt.Frame
, just use the fully qualified name of your frame class when referring to it (Core.Frame
) to avoid the collision, e.g. in yourgetX
method:Or, if you don't really need to use
java.awt.Frame
at all, just remove thatimport
line.You may have a name clash with your own Core.Frame and Java's own java.awt.Frame.
I would re-name your Frame class to something different from a core-Java name, and I'd avoid using static variables as well.
the Frame class you refer to in
x=Frame.dimension.getWidth();
isjava.awt.Frame
. note that you imported this class. Try mentioning explicitly:Core.Frame
instead or remove the lineimport java.awt.Frame;
if you don't use this class.Seems like there's a collision between import between java.awt.Frame and Core.Frame
You should add "import static Core.Frame.dimension" to your imports, and then just work with "dimension" instead of "Frame.dimension"