0
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Random;

public class BubblePanel extends JPanel {

    Random rand = new Random();
    ArrayList<Bubble> bubbleList;
    int size = 25;

    public BubblePanel(){
        bubbleList = new ArrayList<>();
        setBackground(Color.BLACK);

        addMouseListener(new BubbleListener());
    }

    public void paintComponent(Graphics canvas){
        super.paintComponent(canvas);
        for (Bubble b : bubbleList) {
            b.draw(canvas);
        }
    }
    

    private class BubbleListener extends MouseAdapter{
        @Override
        public void mousePressed(MouseEvent e) {
            super.mousePressed(e);
            bubbleList.add(new Bubble(e.getX(),getY(),size));
            repaint();
        }
    }

    private class Bubble{
        private int x;
        private int y;
        private int size;
        private Color color;

        public Bubble(int newX, int newY, int newSize){
            this.x = newX;
            this.y = newY;
            this.size = newSize;
            color = new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
        }
        public void draw(Graphics canvas){
            canvas.setColor(color);
            canvas.fillOval(x - size / 2, y - size / 2, size, size);
        }
    }
}

1 ответ 1

0

Проблема в addMouseListener(add BubbleListener); Так как addMouseMotionListener(new BubbleListener()); , отрабатывается правильно.

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.