2012-04-16 3 views
1

저는 게임을 만들고 작은 800x500 창, 최대화 된 창 및 전체 화면의 3 가지 모드를 갖기를 원합니다. 활성 렌더링을 사용하여 그래픽을 jframepaint으로 componentslayeredpane에 그릴 계획입니다. 나는 2 개의 버퍼와 함께 bufferstrategy을 사용하고 있습니다. 이미 보이는 화면을 전체 화면으로 설정하는 문제를 해결하기 위해 창의 크기가 변경 될 때마다 (프로그램 내 버튼으로 창 자체로 크기 조정할 수 없음) 적절한 크기가 주어지면 새 jframe이 생성됩니다 성분을 paint으로 함유하는 jpanelframe에 첨가한다. 웬일인지, bufferstrategy의 그래픽으로 칠할 때마다 오프셋이 있습니다 (0,0에 그림이 그려지면 -3, -20 주위에 나타납니다. 왜 그런지 모르겠습니다.) 중 당신이 줄 수있는 모든 도움을 주시면 감사하겠습니다. 그래픽으로 그리거나 (현재 JFrame의의 레이어 패널에) 그래픽 페인트 구성 요소를 호출합니다. enter code here활성 렌더링을 사용하는 스크린 관리자 클래스 만들기

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class ScreenManager { 

private GraphicsDevice device; 
private JFrame window; 
private int sizeState; 
private JPanel contentPane; 
private String title; 
private Rectangle maxBounds;  

public static final int SMALL_WINDOW = 1; 
public static final int MAXIMIZED_WINDOW = 2; 
public static final int FULLSCREEN_WINDOW = 3; 

private static final int SMALL_WIDTH = 800; 
private static final int SMALL_HEIGHT = 500; 

public ScreenManager(String s){  
    NullRepaintManager.install(); 
    title = s;  
    GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment();   
    maxBounds = environment.getMaximumWindowBounds(); 
    device = environment.getDefaultScreenDevice(); 
    contentPane = new JPanel();    
    resetJFrame();  
    setSmallWindow();  
} 

public JPanel getPanel(){ 
    return contentPane; 
} 

public Graphics2D getGraphics(){ 
    while(true){ 
     if(window != null){  
    try{ 
    return (Graphics2D) window.getBufferStrategy().getDrawGraphics(); 
    }catch(Exception e){} 
    }  
} 
} 
public void update(){ 
    if(window != null && !window.getBufferStrategy().contentsLost()){ 
     window.getBufferStrategy().show(); 
    } 

    Toolkit.getDefaultToolkit().sync(); 
} 

public int getWidth(){ 
    if(window != null){ 
     return contentPane.getWidth(); 
    } 
    return 0; 
} 

public int getHeight(){ 
    if(window != null){ 
     return contentPane.getHeight(); 
    } 
    return 0; 
} 

public void paintComponents(Graphics2D g){  
     if(window != null){ 
      contentPane.paintComponents(g); 
       } 
     } 

public void closeWindow(){   
    System.exit(0); 
} 

public void setSmallWindow(){ 
    if(window != null && sizeState != ScreenManager.SMALL_WINDOW){ 
    resetJFrame();  
    window.setSize(ScreenManager.SMALL_WIDTH, ScreenManager.SMALL_HEIGHT);  
    contentPane.setSize(ScreenManager.SMALL_WIDTH, ScreenManager.SMALL_HEIGHT); 
    window.setLocationRelativeTo(null);   
    window.setVisible(true);   
    window.createBufferStrategy(2);   
    sizeState = ScreenManager.SMALL_WINDOW;  
} 
} 

public void setMaximizedWindow(){ 
    if(window != null && sizeState != ScreenManager.MAXIMIZED_WINDOW){ 
    resetJFrame(); 
    window.setSize((int) maxBounds.getWidth(), (int) maxBounds.getHeight());  
    contentPane.setSize(window.getWidth(), window.getHeight()); 
    window.setLocation(0,0); 
    window.setVisible(true);   
    window.createBufferStrategy(2);  
    sizeState = ScreenManager.MAXIMIZED_WINDOW;  
} 
} 

public void setFullScreenWindow(){ 
    if(window != null && sizeState != ScreenManager.FULLSCREEN_WINDOW){ 
     resetJFrame();  
     window.setUndecorated(true); 
     device.setFullScreenWindow(window); 
     contentPane.setSize(window.getWidth(), window.getHeight()); 
     window.createBufferStrategy(2); 
     sizeState = ScreenManager.FULLSCREEN_WINDOW;    
    } 
} 

private void resetJFrame(){ 
    if(sizeState == ScreenManager.FULLSCREEN_WINDOW){ 
     device.setFullScreenWindow(null); 
    } 

    if(window != null){ 
     window.dispose(); 
     window = null; 
    } 

    window = new JFrame(title);   
    window.setResizable(false);   
    window.setIgnoreRepaint(true); 
    window.addWindowListener(new WindowExitAdapter()); 
    window.add(contentPane);    
    contentPane.setOpaque(false);  
} 

private class WindowExitAdapter extends WindowAdapter{ 

    public void windowClosing(WindowEvent 
e){      
     closeWindow(); 
    }    
} 
} 

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 

public class ScreenManagerTest implements ActionListener{ 

private ScreenManager sm; 
private JButton fullscreenButton; 
private JButton smallScreenButton; 
private JButton maxScreenButton; 
private JButton quitButton; 
private boolean isRunning = true; 

private int sizeState = 1; 


public static void main(String[] args) { 
    new ScreenManagerTest().go();  
} 

public void go(){ 
    sm = new ScreenManager("Nertz! Solitaire"); 
    sm.getPanel().setLayout(new BorderLayout()); 
    fullscreenButton = new JButton("Set Fullscreen");  
    sm.getPanel().add(fullscreenButton, BorderLayout.CENTER); 
    smallScreenButton = new JButton("Set Small Screen");   
    sm.getPanel().add(smallScreenButton, BorderLayout.WEST); 
    maxScreenButton = new JButton("Set Max Screen");   
    sm.getPanel().add(maxScreenButton, BorderLayout.EAST); 
    quitButton = new JButton("Exit Program");  
    sm.getPanel().add(quitButton, BorderLayout.SOUTH); 
    fullscreenButton.addActionListener(this); 
    smallScreenButton.addActionListener(this); 
    maxScreenButton.addActionListener(this); 
    quitButton.addActionListener(this); 

    while(isRunning == true){   
     switch(sizeState){ 

     case ScreenManager.FULLSCREEN_WINDOW: 
      sm.setFullScreenWindow(); 
      break; 
     case ScreenManager.MAXIMIZED_WINDOW: 
      sm.setMaximizedWindow(); 
      break; 
     case ScreenManager.SMALL_WINDOW: 
      sm.setSmallWindow(); 
      break; 
     } 
     draw(sm.getGraphics());   
     try{ 
      Thread.sleep(20); 
     }catch(Exception e){} 
    } 
    sm.closeWindow(); 
} 

public void draw(Graphics2D g){ 
    sm.paintComponents(g); 
    sm.update(); 

    g.dispose(); 
} 

public void actionPerformed(ActionEvent event){ 
    if(event.getSource() == fullscreenButton){ 
     sm.setFullScreenWindow(); 
     sizeState = ScreenManager.FULLSCREEN_WINDOW; 
    } 
    if(event.getSource() == smallScreenButton){ 
     sm.setSmallWindow(); 
     sizeState = ScreenManager.SMALL_WINDOW; 
    } 
    if(event.getSource() == maxScreenButton){ 
     sm.setMaximizedWindow();  
     sizeState = ScreenManager.MAXIMIZED_WINDOW; 
    } 
    if(event.getSource() == quitButton){ 
     isRunning = false; 
    } 
} 
} 
+1

'NullRepaintManager'? – trashgod

답변

1

을 어떤 이유로, 내가 뭔가를 칠 때마다 그래픽이있는 에서 버퍼 스트 레이지가 있습니다. 오프셋이 있습니다 (0,0에 그림이 그려져 있고, 은 -3, -20 주위에 나타납니다. 이유는 확실하지 않습니다.)

야생의 추측 ... 어쩌면 (0,0)이 아니라 프레임 경계 0으로 그림을 그리는 것입니다. (-3, -20) 점은 창 제로 좌표 (작은 경계 @ 왼쪽 및 ~ 20px 창 헤더)?

실제로 (0,0)에서 그리기는하지만 좌표가 (-3, -20)으로 이동하는 경우 실제로는 창 테두리입니다. 페인트 방법의 시작 부분에 작은 패치 @

을 추가 할 수 있습니다
protected void paintComponent (Graphics g) 
{ 
    Point wl = SwingUtilities.getWindowAncestor (this).getLocationOnScreen(); 
    Point los = this.getLocationOnScreen(); 
    Point zero = new Point (los.x-wl.x, los.y-wl.y); 
    g.translate (zero.x, zero.y); 

    // ... 
} 

하지만 여전히 그 이유는 설명 할 수 없습니다. 어쩌면 당신은 창과 전체 화면 모드 사이를 전환 할 때 제로 좌표를 저장하고 있습니다. 그러면 그 문제가 발생합니다 ...

+0

아니요 좌표를 저장하지 않고 동의합니다. 왜 이런 일이 발생했는지 아직 경험하지 못했지만 왜 그런 일이 일어나는지 확신 할 수 없지만 그 점과 함께 아이디어를 사용하여 해결할 수있었습니다. 넌 나 한테 고마워! – abalis3