Java 需要使用';这';-无法在静态上下文中使用
我知道静态方法必须被调用,但静态方法必须有一个实例。我想做一个简单的2D游戏。我希望我所有的图形都出现在一个窗口中,而不是出现在每个类的几个不同窗口中。因此,我决定使用静态updateBackBuffer方法创建一个paintGraphics类,该方法将向graphics2D变量(名为g2d)添加一个图像。我尝试了这段代码,但出现了一个错误,我无法在静态上下文中使用它,我如何才能绕过它Java 需要使用';这';-无法在静态上下文中使用,java,this,affinetransform,Java,This,Affinetransform,我知道静态方法必须被调用,但静态方法必须有一个实例。我想做一个简单的2D游戏。我希望我所有的图形都出现在一个窗口中,而不是出现在每个类的几个不同窗口中。因此,我决定使用静态updateBackBuffer方法创建一个paintGraphics类,该方法将向graphics2D变量(名为g2d)添加一个图像。我尝试了这段代码,但出现了一个错误,我无法在静态上下文中使用它,我如何才能绕过它 public static void updateBuffer(Image image, int XPos ,
public static void updateBuffer(Image image, int XPos , int YPos , int Height , int Width , int Rotation, AffineTransform trans) {
trans.translate(XPos,YPos);
trans.rotate(Rotation); //More lines will probably be more lines totransform the shape more as the game gets more advanced
g2d.drawImage(image,trans,this);
}
为了访问包含对象,为什么不将对象的实例作为参数传递给静态方法,即:
public static void updateBuffer(Image image, int XPos , int YPos , int Height , int Width , int Rotation, AffineTransform trans, Object parent)
为了访问包含对象,为什么不将对象的实例作为参数传递给静态方法,即:
public static void updateBuffer(Image image, int XPos , int YPos , int Height , int Width , int Rotation, AffineTransform trans, Object parent)
行中:
g2d.drawImage(image,trans,this)
,this
引用定义updateBuffer
的类的实例。由于updateBuffer
被声明为静态
,因此它不能使用引用此
,因为此
不保证初始化
更新
public class Foo {
public Foo() {
...
}
public static void updateBuffer(Image image, int XPos , int YPos , int Height , int Width , int Rotation, AffineTransform trans, Foo foo) {
trans.translate(XPos,YPos);
trans.rotate(Rotation); //More lines will probably be more lines totransform the shape more as the game gets more advanced
g2d.drawImage(image,trans,foo); // <-- 'foo' stands in for 'this'
}
public static void main(String[] args) {
Image i = new Image();
int x,y,h,w,r;
AffineTransform t = new AffineTransform();
Foo f = new Foo();
Foo.updateBuffer(i,x,y,h,w,r,t,f);
}
}
公共类Foo{
公共食物({
...
}
公共静态void updateBuffer(图像图像、int-XPos、int-YPos、int-Height、int-Width、int-Rotation、仿射变换、Foo-Foo){
翻译(XPos,YPO);
trans.rotate(Rotation);//随着游戏的发展,更多的线条可能会更多地改变形状
g2d.drawImage(image,trans,foo);//行:g2d.drawImage(image,trans,this)
,this
引用定义updateBuffer
的类的实例。由于updateBuffer
声明为static
,因此它不能使用引用this
,因为this
不能保证初始化
更新
public class Foo {
public Foo() {
...
}
public static void updateBuffer(Image image, int XPos , int YPos , int Height , int Width , int Rotation, AffineTransform trans, Foo foo) {
trans.translate(XPos,YPos);
trans.rotate(Rotation); //More lines will probably be more lines totransform the shape more as the game gets more advanced
g2d.drawImage(image,trans,foo); // <-- 'foo' stands in for 'this'
}
public static void main(String[] args) {
Image i = new Image();
int x,y,h,w,r;
AffineTransform t = new AffineTransform();
Foo f = new Foo();
Foo.updateBuffer(i,x,y,h,w,r,t,f);
}
}
公共类Foo{
公共食物({
...
}
公共静态void updateBuffer(图像图像、int-XPos、int-YPos、int-Height、int-Width、int-Rotation、仿射变换、Foo-Foo){
翻译(XPos,YPO);
trans.rotate(Rotation);//随着游戏的发展,更多的线条可能会更多地改变形状
g2d.drawImage(image,trans,foo);//那我该怎么解决呢?那是我的原始问题那我该怎么解决呢?那是我的原始问题