2014-07-21 4 views
1

내 GUI의 원점 (0,0)이 약 25/26 픽셀만큼 위로 이동했습니다. 따라서 GUI의 맨 위 왼쪽 구석은 0,25 또는 0,26을 나타내는 것으로 보입니다. 사물을 그릴 때 이중 버퍼를 사용하고 있습니다. 기본적으로, 0,0에서 내 버퍼에 무언가를 그리려고 할 때마다, 화면 밖에서 보입니다.내 GUI의 원점 위치가 약 25/26 픽셀입니다.

예를 들어, 생성하려고 시도한 체커 보드 패턴의 스크린 샷은 0,0부터 시작합니다. 그것이 수평으로 잘 보이더라도, 정상에 잘게 잘린 바둑판을주의하십시오. 높이는 32로 나눌 수있는 480입니다. 바둑판 무늬가 완벽하게 채워야합니다.

public class Dekari_gameGUI extends JFrame implements KeyListener { 
// Resources 
private final String imagePath = "Resources/Images/"; 
private final String spriteSheetPath = imagePath + "Sprite Sheets/"; 
private final String soundPath = "Resources/Sounds/"; 
private final String dataPath = "Resources/Data/"; 
private BufferedImage[][] sprites; 
private Timer playerMovementTimer = new Timer(); 
//Game variables 
private int pX = 0, pY = 0; 
private short pDir = -1, pLastDir = 0, pAniStage = 0, counter = 0; 
//Graphics and paint variables 
private Graphics buffer; 
private Image offscreen; 
private Dimension dim; 
//Internal variables, used for loops and placeholders 
private int a, b, c, d; 

/* 
    ____    _     _    
/___|___ _ __ ___| |_ _ __ _ _ ___| |_ ___ _ __ 
| | /_ \| '_ \/ __| __| '__| | | |/ __| __/ _ \| '__| 
| |__| (_) | | | \__ \ |_| | | |_| | (__| || (_) | | 
\____\___/|_| |_|___/\__|_| \__,_|\___|\__\___/|_| 
*/ 
public Dekari_gameGUI() { 
    // Declaring GUI setup 
    setResizable(false); 
    setTitle("Dekari RPG Indev v1.0"); 
    setDefaultCloseOperation(EXIT_ON_CLOSE); 
    pack(); 
    setSize(640, 480); 
    setBackground(Color.GREEN); 
    setLocationRelativeTo(null); //Centers the window 

    splitSpriteSheets(); //Sets up resources 

    dim = getSize(); 
    offscreen = createImage(dim.width, dim.height); 
    buffer = offscreen.getGraphics(); 

    *snip* 

    setVisible(true); 
} 

*snip* 

/* 
____  _  _ 
| _ \ __ _(_)_ __ | |_ 
| |_)/_` | | '_ \| __| 
| __/ (_| | | | | | |_ 
|_| \__,_|_|_| |_|\__| 
*/ 
public void paint(Graphics g) { 
    //Clears the buffer 
    buffer.clearRect(0, 0, dim.width, dim.width); 
    //Drawing images to the buffer 
    buffer.setColor(Color.BLACK); 
    boolean paint = true; 
    //This is my checkerboard drawing function 
    for (a = 0; a < getWidth()/32; a++) { 
     for (b = 0; b < getHeight()/32; b++) { 
      if (paint) { 
       paint = false; 
       buffer.fillRect(a * 32, b * 32, 32, 32); 
      } else { 
       paint = true; 
      } 
     } 
    } 
    if (pDir == -1) //If the player is not moving 
     //Draws player in an idle stance facing last moved direction 
     buffer.drawImage(sprites[0][4 * pLastDir], pX - 24, pY - 32, this); 
    else //If the player is moving 
     //Draws player in a movement state facing the current direction 
     buffer.drawImage(sprites[0][pAniStage + (4 * pLastDir)], pX - 24, pY - 32, this); 

    //Drawing the buffer to the frame 
    g.drawImage(offscreen, 0, 0, this); 
} 

public void update(Graphics g) { 
    paint(g); 
} 
} 

답변

6

아니 그렇지 않은 : 여기

image

그리고

은 (관련없는 부분 냈다) 내 코드입니다. 프레임에 직접 그림을 그릴 수 있습니다. 테두리 테두리 안에 그려지는 테두리 장식이 있습니다. 왜 최상위 컨테이너에 직접 페인트하지 말아야하는지에 대한 멋진 세계에 오신 것을 환영합니다.

대신 사용자 정의 그림을 JPanel과 같은 다른 구성 요소로 이동하고이를 프레임에 직접 추가하십시오. 프레임 테두리 장식 내에 배치되도록 멀리 배치됩니다.

패널의 getPreferredSize 메서드를 재정의하고 적절한 값을 반환하면 JFrame#pack을 사용하여 기본 크기를 충족하도록 프레임을 "패킹"할 수 있습니다 콘텐츠 요구 사항 이렇게하면 내용이 원하는 크기가되고 창 장식이 내용을 둘러 쌉니다.

또한 스윙 구성 요소는 기본적으로 더블 버퍼링이다, 기억, 그래서 당신은 당신이 그렇게 할 특별한 필요가 없다면, 자세한 내용은

..., 바퀴를 재발견해야 살펴 보도록하지 않습니다 How can I set in the midst?How to get the EXACT middle of a screen, even when re-sizedWhy is overriding paint in a top-level container so bad?Why not to draw directly inside JFrame 자세한 내용은

+0

대단히 감사합니다. –

+0

@RemiliaScarlet 도움이 되었으면합니다. – MadProgrammer

+0

문제를 해결하는 데 도움이된다면 [답변] (http://meta.stackexchange.com/a/5235/155831)을 입력하십시오. –