0
package spaceinvaders;
//@author AlexB
import java.awt.Color;
import java.awt.*;
import java.awt.Graphics2D;
import java.awt.event.KeyAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class SpaceInvaders extends KeyAdapter implements MouseListener {
private JFrame frame;
private JPanel panel;
public static int screenSizeX;
public static int screenSizeY;
private Dimension screenSize;

public SpaceInvaders() throws IOException
{
    //screenSize = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
    screenSize = Toolkit.getDefaultToolkit().getScreenSize(); //Finds the screen size
    screenSizeX = (int)screenSize.getWidth(); //The length of the screen
    screenSizeY = (int)screenSize.getHeight(); //The height of the screen
    frame = new JFrame("Space Invaders");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    frame.setResizable(false);
    frame.setSize(screenSizeX, screenSizeY);
    panel = new MyPanel();
    frame.getContentPane().add(panel);
    frame.addMouseListener(this);
    frame.addKeyListener(this);
    spriteStore.getImage();
}

@Override
public void mouseClicked(MouseEvent me) 
{
}

@Override
public void mousePressed(MouseEvent me) 
{
}

@Override
public void mouseReleased(MouseEvent me) 
{
}

@Override
public void mouseEntered(MouseEvent me) 
{
}

@Override
public void mouseExited(MouseEvent me) 
{
}

class MyPanel extends JPanel
    {
        @Override
        public void paint(Graphics g)
        {
            super.paint(g);
            Graphics2D g2D = (Graphics2D)g;
            g2D.setColor(Color.BLACK);
            g2D.fillRect(0,0, screenSizeX, screenSizeY);
            g2D.drawImage(spriteStore.player, playerEntity.xCoord, screenSizeY-100, null);
        }
    }


public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable()
    {
        @Override
        public void run()
        {
            SpaceInvaders spaceInvaders;
            try 
            {
                spaceInvaders = new SpaceInvaders();
            } 
            catch (IOException ex) {}
        }
    }
            );
    System.out.println("" + screenSizeX + "" + screenSizeY);
}
}

问题是为什么我的尺寸不能测量?当它打印出 screenSizeX 和 screenSizeY 时,它为两者提供值 0。我使用的是mac和netbeans,这段代码通常在windows电脑上运行。

4

0 回答 0