2013-03-08 4 views
2

정적 메서드를 호출해야하지만 정적 메서드는 인스턴스를 만들어야한다는 것을 알고 있습니다. 나는 간단한 2D 게임을 만들려고 노력하고있다. 모든 그래픽이 한 창에 나타나기를 원하는데, 각 클래스마다 몇 가지 다른 창이 나타나기를 원합니다. 그래서 graphics2D 변수 (g2d)에 이미지를 추가 할 정적 updateBackBuffer 메서드로 paintGraphics 클래스를 만들기로 결정했습니다. 이 코드를 시도했지만 내가 정적 맥락에서이를 사용할 수 없다는 오류가있어, 나는이 문제를 얻을 수있는 방법이 포함 된 개체에 액세스하기 위해'this'사용에 대한 도움이 필요합니다 - 정적 컨텍스트에서는 사용할 수 없습니다.

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);  
} 
+0

- http://www.buggybread.com/2014/06/error-cannot-use-this- in-static-context.html –

답변

3

:의 정적 메소드의 파라미터, 예로서 g2d.drawImage(image,trans,this); 상기 thisupdateBuffer를 정의하는 클래스의 인스턴스를 지칭한다. updateBufferstatic으로 선언되었으므로 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); 
    } 
} 
+1

그런데 어떻게 수정해야합니까? 그건 원래의 질문이었습니다. –

1

을? : 왜 객체의 인스턴스를 통과하지 라인에

public static void updateBuffer(Image image, int XPos , int YPos , int Height , int Width , int Rotation, AffineTransform trans, Object parent) 
관련 문제