Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
111 views
in Technique[技术] by (71.8m points)

java - How to fix the white screen drawing error?

I'm new to Stackoverflow and tried to follow the rules as much as possible. I haven't been able to solve this problem for hours. When I start the program it seldom starts strangely:
properly started: correct image:

https://i.stack.imgur.com/pc0Hk.png

not properly started: white screen:

https://i.stack.imgur.com/gesKX.png

If you see an error in the code and help me fix it I would appreciate it. any correction


public class MainClass extends JPanel implements Runnable, MouseListener {
    enum MenuState {
        xMenu1, xMenu2;
    }

    private MenuState menuState = MenuState.xMenu1;
    JFrame frame;
    private Image bgImage, playButtonImage, backButtonImage;
    private Rectangle playButton, backButton;

    
    public MainClass() {
        frame = new JFrame("TEST");
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        frame.setUndecorated(true);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBackground(Color.BLACK);
        frame.setFocusable(true);
        frame.addMouseListener(this);
        frame.add(this);
        try {
            bgImage = ImageIO.read(getClass().getResourceAsStream("/Data/background.png"));
            playButtonImage = ImageIO.read(getClass().getResource("/Data/playButton2.png"));
            backButtonImage = ImageIO.read(getClass().getResource("/Data/backButton.png"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        playButton = new Rectangle(900, 400, 214, 78);
        backButton = new Rectangle(5, 5, 136, 92);

        Thread thread = new Thread(this);
        thread.start();
        System.out.println("thread started");
        
    }

    public static void main(String[] args) {
        new MainClass();

    }

    @Override
    public void run() {
        
        while (true) {
            System.out.println("run method started");
            if (menuState == menuState.xMenu1) {

            } else if (menuState == menuState.xMenu2) {

            }
            repaint();
            try {
                Thread.sleep(17);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

    @Override
    public void paint(Graphics g) {
        if (menuState == menuState.xMenu1) {
            g.drawImage(playButtonImage, playButton.x, playButton.y, this); // THIS IS THE 77th LINE
        } else if (menuState == menuState.xMenu2) {
            g.drawImage(bgImage, 0, 0, this);
            g.drawImage(backButtonImage, backButton.x, backButton.y, this);
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {
        if (menuState == menuState.xMenu1) {
            if (playButton.intersects(e.getX(), e.getY(), 1, 1)) {
                menuState = menuState.xMenu2;
            }
        } else if (menuState == menuState.xMenu2) {
            if (backButton.intersects(e.getX(), e.getY(), 1, 1)) {
                menuState = menuState.xMenu1;
            }
        }
    }
}
question from:https://stackoverflow.com/questions/65882380/how-to-fix-the-white-screen-drawing-error

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...