1

私はゲームを作ることを計画していて、ウィンドウに3つの異なるモードを持たせたいと思っています。小さな800 x 500ウィンドウ、最大化されたウィンドウ、およびフルスクリーンウィンドウです。アクティブレンダリングを使用して、のjframeおよびにグラフィックを描画することを計画しています。私は2つのバッファでを使用しています。すでに表示されている画面を全画面表示に設定する問題に対処するために、ウィンドウのサイズが変更されるたびに(ウィンドウ自体ではサイズ変更できず、プログラム内のボタンでのみ)、適切なサイズが指定された新しいウィンドウが作成されます。にコンポーネントを含むを追加します。どういうわけか、私が何かをペイントするたびにpaintcomponentslayeredpanebufferstrategyjframejpanelpaintframebufferstrategy、オフセットがあります(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;  
    }
}
}
4

1 に答える 1

1

何らかの理由で、bufferstrategyのグラフィックを使用して何かをペイントするたびに、オフセットがあります(0,0で何かをペイントすると、-3、-20付近に表示されますが、理由はわかりません。

ただの大げさな推測...(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 );

    // ...
}

しかし、それでも私はそれが起こる可能性がある理由を説明することはできません。たぶん、ウィンドウモードとフルスクリーンモードを切り替えるときにゼロ座標を保存しているので、そのような問題が発生します...

于 2012-04-16T08:58:47.417 に答える