有没有办法让第二次点击改变结果?

Is there a way to make a 2nd click change the result?

当您单击 cameraButton 时,我正在尝试创建它,图形显示,但再次单击时,它会关闭。

@Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        int camButtonWidth = 500;
        int camButtonHeight = 33;
        int camButtonX = (width - camButtonWidth) / 2;
        int camButtonY = (height - camButtonHeight) - 5;

        int camWidth = width - 50;
        int camHeight = (height - (camButtonHeight * 2)) - 10;
        int camX = (width - camWidth) / 2;
        int camY = ((height - camHeight) - camButtonHeight) / 2;


        Graphics2D g1 = (Graphics2D) g;
        Graphics2D g2 = (Graphics2D) g;
        Graphics2D g3 = (Graphics2D) g;


        RoundRectangle2D camButton = new RoundRectangle2D.Double(camButtonX, camButtonY, camButtonWidth, camButtonHeight, 25, 25);
        RoundRectangle2D cameras = new RoundRectangle2D.Double(camX, camY, camWidth, camHeight, 25, 25);

        // Background
        g1.setColor(Color.BLACK);
        g1.fillRect(0, 0, width, height);

        addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (camButton.contains(e.getPoint())) {
                    camUp = !camUp;
                    repaint();
                }
            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });

        // Camera Button
        g2.setColor(camColor);
        g2.fill(camButton);


        paintCameras = camUp;


        // Cameras
        g3.setColor(camColor);
        if (paintCameras) {
            g3.fill(cameras);
        }


        repaint();
    }

尝试改成当你点击相机按钮时,一个图形对象显示,但是当再次点击时,它关闭。

要使此类程序运行,您应该:

  1. 在只调用一次的代码中创建您的 MouseListener,例如在构造函数中
  2. 在 class 中创建一个实例字段来表示相机按钮,例如 Rectangle 或 RoundRectangle2D 并为其提供一个可行的对象引用
  3. 在鼠标侦听器中,如果在表示相机按钮的形状内发生点击,则切换布尔变量的状态,例如,camUp = !camUp; 正如您所做的那样
  4. 然后调用repaint().
  5. 在 paintComponent 方法中,使用 if 语句检查布尔变量的状态,如果为真,则在 if 语句中绘制图像。
  6. 将鼠标侦听器和绘画代码分开并放在不同的方法(或构造函数)中。
  7. 切勿在绘画方法中调用 repaint(),因为这会导致不受控制的动画。如果你需要一个 Swing 动画,那么使用一个 Swing Timer 这样你就可以完全控制它。我在这里看不到它的必要性。

例如:

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.BufferedImage;
import javax.swing.*;

@SuppressWarnings("serial")
public class GraphicsExample extends JPanel {
    private static final int IMG_WIDTH = 400;
    private static final int PREF_W = (3 * IMG_WIDTH) / 2;
    private static final int PREF_H = PREF_W;
    private static final Color BTN_COLOR = Color.RED;
    private static final Color HOVER_COLOR = new Color(255, 100, 100);
    private static final Color BTN_CLK_COLOR = new Color(180, 0, 0);
    private static final int IMG_X = IMG_WIDTH / 2;
    private static final int IMG_Y = IMG_X;
    private double camX = 10;
    private double camY = camX;
    private double camWidth = 200;
    private double camHeight = 80;
    private Color buttonColor = Color.RED;
    private RoundRectangle2D cameraButton = new RoundRectangle2D.Double(camX, camY, camWidth, camHeight, 25, 25);
    private Image img;
    private boolean showImage = false;
    private JCheckBox toggleModeChkBox = new JCheckBox("Toggle Mode");
    // private boolean toggleMode = true;

    public GraphicsExample() {
        add(toggleModeChkBox);
        setPreferredSize(new Dimension(PREF_W, PREF_H));
        img = createMyImage();
        MouseAdapt mouseAdapt = new MouseAdapt();
        addMouseListener(mouseAdapt);
        addMouseMotionListener(mouseAdapt);
    }

    private Image createMyImage() {
        BufferedImage img = new BufferedImage(IMG_WIDTH, IMG_WIDTH, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2 = img.createGraphics();
        g2.setPaint(new GradientPaint(0, 0, Color.RED, 100, 100, Color.BLUE, true));
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        int gap = 10;
        g2.fillOval(gap, gap, IMG_WIDTH - 2 * gap, IMG_WIDTH - 2 * gap);
        g2.dispose();
        return img;
    }

    private class MouseAdapt extends MouseAdapter {
        @Override
        public void mousePressed(MouseEvent e) {
            if (cameraButton.contains(e.getPoint())) {
                buttonColor = BTN_CLK_COLOR;
                if (toggleModeChkBox.isSelected()) {
                    showImage = !showImage;
                } else {
                    showImage = true;
                }
            } else {
                buttonColor = BTN_COLOR;
            }
            repaint();
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            if (cameraButton.contains(e.getPoint())) {
                buttonColor = HOVER_COLOR;
            } else {
                buttonColor = Color.RED;
            }
            
            if (!toggleModeChkBox.isSelected()) {
                showImage = false;
            }
            repaint();
        }

        @Override
        public void mouseMoved(MouseEvent e) {
            if (cameraButton.contains(e.getPoint())) {
                buttonColor = HOVER_COLOR;
            } else {
                buttonColor = Color.RED;
            }
            if (!toggleModeChkBox.isSelected()) {
                showImage = false;
            }
            repaint();
        }
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setColor(buttonColor);
        g2.fill(cameraButton);
        
        if (showImage) {
            int x = (getWidth() - IMG_WIDTH) / 2;
            int y = (getHeight() - IMG_WIDTH) / 2;
            g2.drawImage(img, x, y, this);
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            GraphicsExample mainPanel = new GraphicsExample();

            JFrame frame = new JFrame("GUI");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(mainPanel);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        });
    }

}