简体   繁体   中英

How to prevent Java from erasing content if screen is minimized?

This is a current problem I am facing for a game. It is a game of Tic-Tac-Toe the user and a friend can play together. I'm not finished yet, but this is what I've got. Any time the window is minimized or covered up by another window the portion of the graphics drawn (X and O) get deleted. I don't really know what to do about this, it would be nice to have a way where the drawing does not get deleted. My paintComponent() method for my main class just sets up the design of the board. Any help appreciated, thank you!

private class DrawXO implements MouseListener {

    public void mousePressed(MouseEvent evt) {

        int x = evt.getX();
        int y = evt.getY();

        Graphics gContext = getGraphics();
        Graphics2D graphics = (Graphics2D) gContext;


        graphics.setStroke(new BasicStroke(8));
        if (playerOneTurn) {

            Player1.drawCircle(gContext, x, y );
            checkForWinner();

            if(playerOneWins) {
                System.out.println("Player one wins");
            }

            playerTwoTurn = false;
        } else {
            // Still need to implement drawing for this ~
            checkForWinner();

        }

    }

    public void mouseExited(MouseEvent evt) {}
    public void mouseEntered(MouseEvent evt) {}
    public void mouseClicked(MouseEvent evt) {}
    public void mouseReleased(MouseEvent evt) {}
}
  1. Don't use getGraphics();

  2. You need to learn how custom painting is done in Swing. Run through Performing Custom Painting . You will notice that paint required the use of a paintComponent method (in your JPanel or JComponent class) which takes in a Graphics (created for you) context that you use to perform your custom painting. All painting should be done within that context.

     protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; // do painting here } 

    Note: Never call this method explicitly, it is automatically called

  3. To update the graphics, you will make some update to some paint variables, then call repaint() . Maybe something like:

     @Override public void mousePressed(MouseEvent e) { x = e.getX(); y = e.getY(); repaint(); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g2.fillRect(x, y, width, height); } 
  4. If you want to add/draw multiple object, say with the click of a mouse, then keep a List of objects and iterate through the list int the paintComponent method. When the mouse is clicked, add another object to the list and repaint. Something like

     List<Rectangle2D> rectangles; ... @Override public void mousePressed(MouseEvent e) { x = e.getX(); y = e.getY(); rectangles.add(new Rectangle2d.Double(x, y, width, height); repaint(); } ... @Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; for (Rectangle2D rect: rectangles) { g2.fill(rect); } } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM