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

Categories

I'm creating a snake game in java I'm making my menu and after you press start it opens up the game. I want to add extra levels of difficulty after you press start but don't know how to clear what I've drawn from my screen and don't want to constantly open more JFrames each time(so it doesn't lag or cause bugs later on).

import java.awt.event.*;
import java.awt.image.BufferStrategy;
import java.awt.*;
import javax.swing.*;
import java.util.Random;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;

class Menu1 extends JPanel {
    private static final long serialVersionUID = 1138641784482936196L;

    protected static boolean clickStart = false;
    private final int UNITS = 25;
    private final int BUTTON_WIDTH = UNITS * 10;
    private final int BUTTON_HEIGHT = UNITS * 3;
    private Rectangle bounds;
    Handler handler = new Handler();
    private BufferedImage image;

    public Menu1() {
        this.setPreferredSize(new Dimension(Panel.SCREEN_WIDTH, Panel.SCREEN_HEIGHT));
        this.setBackground(Color.BLACK);
        // * Note to self: add settings with background color change
        this.addMouseListener(handler);
        this.addMouseMotionListener(handler);
        this.setFocusable(true);
    }

    public void paintComponent(Graphics g) {
        startImage();
        g.setColor(Color.BLACK);
        int x = 13 * (UNITS / 2);
        int y = 6 * UNITS;
        g.drawImage(image, x, y, BUTTON_WIDTH + 2, BUTTON_HEIGHT * 2, null);

        bounds = new Rectangle(x, y, BUTTON_WIDTH + 2, BUTTON_HEIGHT * 2);
        g.setFont(new Font("Gill Sans", Font.PLAIN, 35));
        g.setColor(Color.white);
    }

    public void startImage() {
        try {
            image = ImageIO.read(getClass().getResourceAsStream("StartButton.png"));
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }

    private class Handler implements MouseListener, MouseMotionListener {
        @Override
        public void mouseClicked(MouseEvent e) {
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            if (bounds.contains(e.getX(), e.getY())) {
                new PlayFrame();
                // creates a new PlayFrame
                GameFrame.gameFrame.dispose();
                // disposes the old frame
            }
        }

        @Override
        public void mousePressed(MouseEvent e) {

        }

        @Override
        public void mouseExited(MouseEvent e) {
            System.out.println("Start Area Exited");
        }

        @Override
        public void mouseEntered(MouseEvent e) {
            System.out.println("Start Area Entered");
        }

        @Override
        public void mouseDragged(MouseEvent e) {
            System.out.println("Dragged");
        }

        @Override
        public void mouseMoved(MouseEvent e) {

        }
    }
}
question from:https://stackoverflow.com/questions/65940476/java-jpanel-clearing-graphics

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

1 Answer

Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...