0

这是我的 ScreenManager 类,用于获得完美的 DisplayMode 并将屏幕设置为全屏

import java.awt.*;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.lang.reflect.InvocationTargetException;
import javax.swing.JFrame;

public class ScreenManager
{
    private GraphicsDevice vc;

//give vc access to monitor screen
public ScreenManager()
{
    GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
    vc = e.getDefaultScreenDevice();
}

//get all compatible DM
public DisplayMode[] getCompatibleDisplayModes()
{
    return vc.getDisplayModes();
}

//compares DM passed into vc DM & see if they match
public DisplayMode findFirstCompatibleMode(DisplayMode modes[])
{
    DisplayMode goodModes[] = vc.getDisplayModes();
    for ( int x = 0; x < modes.length; x++)
    {
        for( int y = 0; y < goodModes.length; y++)
        {
            if(displayModesMatch(modes[x], goodModes[y]))
            {
                return modes[x];
            }
        }
    }
    return null;
}

//get current DM
public DisplayMode getCurrentDisplayMode()
{
    return vc.getDisplayMode();
}

//checks if two modes match each other
public boolean displayModesMatch(DisplayMode m1, DisplayMode m2)
{
    if(m1.getWidth() != m2.getWidth() || m1.getHeight() != m2.getHeight())
    {
        return false;
    }
    if(m1.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI && m2.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI && m1.getBitDepth() != m2.getBitDepth())
    {
        return false;
    }
    if(m1.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN && m2.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN && m1.getRefreshRate() != m2.getRefreshRate())
    {
        return false;
    }

    return true;
}

//make frame full screen
public void setFullScreen(DisplayMode dm)
{
    JFrame f = new JFrame();
    f.setUndecorated(true);
    f.setIgnoreRepaint(true);
    f.setResizable(true);
    vc.setFullScreenWindow(f);

    if( dm != null & vc.isDisplayChangeSupported())
    {
        try
        {
            vc.setDisplayMode(dm);
        }
        catch(Exception ex) { }
    }
    f.createBufferStrategy(2);
}

//we will set graphics object to this
public Graphics2D getGraphics()
{
    Window w = vc.getFullScreenWindow();
    if(w != null)
    {
        BufferStrategy s = w.getBufferStrategy();
        return (Graphics2D)s.getDrawGraphics();
    }
    else
    {
        return null;
    }
}

//update displaye
public void update()
{
    Window w = vc.getFullScreenWindow();
    if( w != null )
    {
        BufferStrategy s = w.getBufferStrategy();
        if(!s.contentsLost())
        {
            s.show();
        }
    }
}

//return full screen window
public Window getFullScreenWindow()
{
    return vc.getFullScreenWindow();
}

//get width of window
public int getWidth()
{
    Window w = vc.getFullScreenWindow();
    if( w != null)
    {
        return w.getWidth();
    }
    else
    {
        return 0;
    }
}

//get height of window
public int getHeight()
{
    Window w = vc.getFullScreenWindow();
    if( w != null)
    {
        return w.getHeight();
    }
    else
    {
        return 0;
    }
}

//get out of full screen
public void restoreScreen()
{
    Window w =vc.getFullScreenWindow();
    if( w != null)
    {
        w.dispose();
    }
    vc.setFullScreenWindow(null);
}

//create image compatible with your moitor
public BufferedImage createCompatibleImage(int w, int h, int t)
{
    Window win = vc.getFullScreenWindow();
    if( win != null)
    {
        GraphicsConfiguration gc = win.getGraphicsConfiguration();
        return gc.createCompatibleImage(w, h, t);
    }
    else
    {
        return null;
        }
    }
}

这是我用于初始化屏幕、循环机制等的核心类。

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

public abstract class Core
{
    private static DisplayMode modes[] = {
    new DisplayMode(800, 600, 32, 0),
    new DisplayMode(800, 600, 24, 0),
    new DisplayMode(800, 600, 16, 0),
    new DisplayMode(640, 480, 32, 0),
    new DisplayMode(640, 480, 24, 0),
    new DisplayMode(640, 480, 16, 0),
    };

    private boolean running;
    protected ScreenManager s;

    //stop method
    public void stop()
    {
        running = false;
    }

    //call inint & gameloop
    public void run()
    {
        try
        {
            init();
            gameloop();
        }
        finally
        {
            s.restoreScreen();
        }
    }

    //set to full screen
    public void init()
    {
        s = new ScreenManager();
        DisplayMode dm = s.findFirstCompatibleMode(modes);
        s.setFullScreen(dm);

        Window w = s.getFullScreenWindow();
        w.setFont(new Font("Arial", Font.PLAIN, 20));
        w.setBackground(Color.GREEN);
        w.setForeground(Color.WHITE);
        running = true;
    }

    //main gameloop
    public void gameloop()
    {
        long startTime = System.currentTimeMillis();
        long cumTime = startTime;

        while(running)
        {
            long timePassed = System.currentTimeMillis() - cumTime;
            cumTime += timePassed;

            update(timePassed);

            Graphics2D g = s.getGraphics();
            draw(g);
            g.dispose();
            s.update();

            try
            {
                Thread.sleep(20);
            }
            catch(Exception ex) { }
        }
    }

    //update animation
    public void update(long timePassed) {   }

    //draws to the screen
    public abstract void draw(Graphics2D g);
}

这是我用于在窗口上设计起始屏幕的实际游戏类

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

public class Game extends Core implements KeyListener, MouseListener
{
    public static void main(String []args)
    {
        new Game().run();
    }

    private String mess = "";
    private Image VRacer;
    private String start = "Start", controls = "Controls", credits = "Credits", exit = "Exit";

    private void loadPics()
    {
        VRacer = new ImageIcon("VRacers.png").getImage();
    }

    public void init()
    {
        super.init();
        loadPics();
        Window w = s.getFullScreenWindow();
        w.setFocusTraversalKeysEnabled(false);
        w.addKeyListener(this);
        w.addMouseListener(this);
        mess = "Press Esc to Close Application";
    }

    //draw
    public synchronized void draw(Graphics2D g)
    {
        Window w = s.getFullScreenWindow();
        Graphics2D g2 = (Graphics2D)g;
        g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, s.getWidth(), s.getHeight());
        g.setColor(Color.YELLOW);
        g2.drawString(mess, 30, 30);
        g.drawImage(VRacer, 160, 50, null);
        g2.drawString(start, 355, 200);
        g2.drawString(controls, 340, 270);
        g2.drawString(credits, 345, 340);
        g2.drawString(exit, 360, 410);
    }


    //key pressed
    public void keyPressed(KeyEvent e)
    {
        int keyCode = e.getKeyCode();
        if(keyCode == KeyEvent.VK_ESCAPE)
        {
            stop();
        }
        else
        {
            mess = "Pressed : " + KeyEvent.getKeyText(keyCode);
            e.consume();
        }
    }

    //key pressed
    public void keyTyped(KeyEvent e)
    {
        e.consume();
    }

    //key released
    public void keyReleased(KeyEvent e)
    {
        int keyCode = e.getKeyCode();
        mess = "Released : " + KeyEvent.getKeyText(keyCode);
        e.consume();
    }

    //Mouse pressed
    public void mousePressed(MouseEvent e)
    {
        if( (e.getX() >= 355 && e.getX() <=  396) && (e.getY() >= 174 && e.getY() <=  200) )
        {
            mess = "Pressed : " + start + " & location : (" + e.getX() + ", " + e.getY() + ")";
        }

        if( (e.getX() >= 340 && e.getX() <=  410) && (e.getY() >= 244 && e.getY() <=  270) )
        {
            mess = "Pressed : " + controls + " & location : (" + e.getX() + ", " + e.getY() + ")";
        }

        if( (e.getX() >= 345 && e.getX() <=  407) && (e.getY() >= 314 && e.getY() <=  340) )
        {
            mess = "Pressed : " + credits + " & location : (" + e.getX() + ", " + e.getY() + ")";
        }

        if( (e.getX() >= 360 && e.getX() <=  390) && (e.getY() >= 384 && e.getY() <=  410) )
        {
            stop();
            mess = "Pressed : " + exit + " & location : (" + e.getX() + ", " + e.getY() + ")";
        }

    }

    //Mouse released
    public void mouseReleased(MouseEvent e)     {   }

    //Mouse clicked
    public void mouseClicked(MouseEvent e)      {   }

    //Mouse entered
    public void mouseEntered(MouseEvent e)  {   }

    //Mouse exited
    public void mouseExited(MouseEvent e)       {   }

    //
}

我在屏幕上添加了一些文本,当鼠标单击文本所包围的区域时,它会给出输出。但是,如果我想在单击开始、控件或 Credits 时在屏幕上绘制新内容,我该如何清除屏幕?例如删除开始、信用、控制、退出和图像等所有内容,这样我就可以在那个空白屏幕上绘制新的东西。

thnx提前寻求帮助...

4

0 回答 0