如何使用 mouseListener 显示数组中的内容?

How to display the contents in an array by using mouseListener?

我正在做一个程序,其中有8个形状包含在一个数组中,当您单击鼠标时将显示这些形状。我是 Java 的新手,对事件处理程序和侦听器不是很熟悉。

我试图让形状出现在框架内鼠标点击的位置,但我遇到了麻烦,因为每个形状的构造函数使用 2 个点作为其参数。

下面是代码示例:

import MyLibs.Circle;
import MyLibs.Rectangle;
import MyLibs.Shape;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;

public class DrawInScreenOnClickTest extends javax.swing.JFrame {

    private Image dbImage;

    Shape[] sh = new Shape[] {
        new Circle(new Point(50, 60), new Point(130, 180)),
            new Rectangle(new Point(50, 200), new Point(150, 350)),
            new Circle(new Point(200, 300), new Point(350, 350)),
            new Rectangle(new Point(100, 100), new Point(250, 250)),
            new Circle(new Point(150, 150), new Point(300, 300)),
            new Circle(new Point(200, 200), new Point(450, 500)),
            new Rectangle(new Point(300, 300), new Point(550, 550)),
            new Circle(new Point(150, 150), new Point(320, 320))
    };

    Color[] colors = new Color[] {
        Color.cyan, Color.green, Color.red, Color.blue, Color.magenta, Color.orange
    };

    Random random = new Random();
    int i = 0;
    Shape shape;
    boolean first = true;


    public DrawInScreenOnClickTest() {
        initComponents();
    }


    private void formMouseClicked(java.awt.event.MouseEvent evt) {
        repaint();
    }


    public class Mouse extends MouseAdapter {
        @Override
        public void mousePressed(MouseEvent e) {
            int x = e.getX();
            int y = e.getY();
        }
    }

    public void paint(Graphics g) {

        if (first == true) {
            first = false;
        } else {
            int shapeColor = random.nextInt(6);

            Shape shape = sh[i]; //calls shapes array
            shape.setColor(colors[shapeColor]); //call colors array random
            shape.drawShape(g); //display shape
            if (i == 4) {
                i = 0;
            } else
                i++;
        }
    }


    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new DrawInScreenOnClickTest().setVisible(true);
            }
        });
    }
}

这是一个交替绘制具有随机长度和宽度的形状(矩形和圆形)的示例。

此示例使用 paintComponent(...) 方法在屏幕中绘制形状。

你不应该扩展 JFrame 除非你改变它的行为,而是构建你的 UI 以使用 JPanels,它更灵活并且允许你使用 paintComponent.

我使用了 Shape API 而不是自定义的 Shape class(您正在使用但没有 post它的代码)。

我没有包含随机颜色,因为这取决于 OP 来编写,这只是一个关于如何在鼠标单击时绘制组件的示例。

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class DrawInScreenOnClickTest {
    private JFrame frame;
    private DrawingPane pane;
    private List<Shape> shapes;
    
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new DrawInScreenOnClickTest()::createAndShowGUI);
    }
    
    private void createAndShowGUI() {
        frame = new JFrame(getClass().getSimpleName());
        
        shapes = new ArrayList<Shape>();
        shapes.add(new Ellipse2D.Double(50, 50, 50, 50));
        shapes.add(new Rectangle.Double(50, 200, 50, 100));
        shapes.add(new Ellipse2D.Double(150, 200, 150, 150));
        
        pane = new DrawingPane();
        
        pane.addMouseListener(new MouseListen());
        
        frame.add(pane);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
    
    @SuppressWarnings("serial")
    class DrawingPane extends JPanel {
        public void addNewShape(Shape s) {
            
        }
        
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
            for (Shape s : shapes) {
                g2d.draw(s);
            }
        }
        
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(500, 500);
        }
    }

    class MouseListen extends MouseAdapter {
        int counter = 0;
        
        @Override
        public void mouseClicked(MouseEvent e) {
            super.mouseClicked(e);
            counter++;
            
            if (counter % 2 == 0) {
                shapes.add(new Rectangle.Double(e.getX(), e.getY(), Math.random() * 50, Math.random() * 50));
            } else {
                shapes.add(new Ellipse2D.Double(e.getX(), e.getY(), Math.random() * 50, Math.random() * 50));
            }
            pane.repaint();
        }
    }
}

附加(推荐)讲座:

  • paint() vs paintComponent()