Java如何在鼠标点击上绘制矩形 [英] Java how to paint rectangles on mouseclick

查看:188
本文介绍了Java如何在鼠标点击上绘制矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所说,我很难在JApplet中绘制一些矩形(填充)。
确切的目标是拥有一个50x50的表格,当你点击一个目标单元格时,填充它(可能通过绘制一个填充的矩形来完成)。我已经完成了关于起点坐标的数学运算,但出于某种原因,我无法在MouseClicked方法中绘制新的矩形。有什么建议吗?

As the title says, I'm having a hard time trying to draw some rectangles (filled) in JApplet. The exact goal is to have a 50x50 table and when you click on a targeted cell, to make it filled (possibly done by drawing a filled rectangle). I have done the maths about the coordinates of the starting point, but for some reason I can't draw the new rectangle in the MouseClicked method. Any suggestions?

public class Main extends JApplet {

public static final int DIMX = 800;
public static final int DIMY = 800;
public static final int ratio = 16;
Graphics g;
boolean drawing;
public int cX;
public int cY;

public Main() {
    JPanel MainFrame = new JPanel();
    MainFrame.setPreferredSize(new Dimension(400, 800));
    MainFrame.setBackground(Color.LIGHT_GRAY);
    JPanel Table = new JPanel();
    Table.setPreferredSize(new Dimension(800, 800));
    Table.setBackground(Color.LIGHT_GRAY);
    add(MainFrame, BorderLayout.EAST);
    add(Table, BorderLayout.WEST);
    addMouseListener(new clicked());
}



public void paint(Graphics g) {
    super.paintComponents(g);
    g.setColor(Color.black);
    for (int i = 0; i <= 800; i += 16) {
        g.drawLine(0, i, 800, i);
        g.drawLine(i, 0, i, 800);
//            g.fillRect(cX, cY, 16, 16);
    }
}

public static void main(String[] args) {
    JFrame win = new JFrame("Retarded Bullshit");
    win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    win.setPreferredSize(new Dimension(1216, 840));
    win.setContentPane(new Main());
    win.pack();
    win.setVisible(true);

}

public class clicked extends JApplet implements MouseListener {

public int cX;
public int cY;
Graphics g;

@Override
public void mouseClicked(MouseEvent e) {
//            Point a = e.getLocationOnScreen();
    int cellX = e.getX();
    int cellY = e.getY();
    if (cellX < 800 && cellX > 0 && cellY < 800 && cellY > 0) {
        cX = cellX / 16 + 1;
        cY = cellY / 16 + 1;

        JOptionPane.showMessageDialog(null, "" + cX + " " + cY);
    }


推荐答案

这是一个相对简单的概念(没有冒犯)。

This is a relatively simple concept (no offense).

首先,不要将你的代码与 JApplet 和<$ c $混合C>的JFrame 。如果要在这两种介质中使用应用程序,请将逻辑分成单独的组件(如 JPanel ),您可以轻松地将其添加到其中。你真的不应该将顶级容器添加到另一个顶级容器(将一个applet添加到一个框架) - 它很乱。

To start with, don't mix your code with JApplet and JFrame. If you want to use your application in these two mediums, separate the logic into a separate component (like JPanel) which you can easily add to either. You really shouldn't add a top level container to another top level container (adding an applet to a frame) - it's messy.

避免覆盖 paint 顶级容器的方法(如 JApplet ),而是使用自定义组件(如 JPanel )而是覆盖它的 paintComponent 方法。

Avoid overriding the paint methods of top level containers (like JApplet), instead, use a custom component (like JPanel) instead and override it's paintComponent method.

在你的例子中,你应该调用 super.paint 而不是 super.paintComponents paint 做重要工作,你不想跳过它 - 但你应该使用 JComponent#paintComponent

In your example, you should be calling super.paint rather then super.paintComponents. paint does important work, you don't want to skip it - but you should be using JComponent#paintComponent

MouseListener 应该添加到您有兴趣管理鼠标事件的组件中。因为点击永远不会添加到任何容器,它永远不会收到鼠标事件。

MouseListeners should added to the components that you are interested in managing mouse events. Because clicked is never added to any containers, it will never recieve mouse events.

看看

  • How to write mouse listeners
  • Performing Custom Painting
  • 2D Graphics
  • Painting in AWT and Swing (because every Swing developer should have an understanding of this)

public class SimplePaint03 {

    public static void main(String[] args) {
        new SimplePaint03();
    }

    public SimplePaint03() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception ex) {
                }

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

    public class PaintPane extends JPanel {

        private List<Shape> grid;
        private List<Shape> fill;

        public PaintPane() {
            grid = new ArrayList<>(5);
            fill = new ArrayList<>(5);
            addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    for (Shape shape : grid) {
                        if (shape.contains(e.getPoint())) {
                            if (fill.contains(shape)) {
                                fill.remove(shape);
                            } else {
                                fill.add(shape);
                            }
                        }
                    }
                    repaint();
                }
            });

            int colWidth = 200 / 50;
            int rowHeight = 200 / 50;

            for (int row = 0; row < 50; row++) {
                for (int col = 0; col < 50; col++) {
                    grid.add(new Rectangle(colWidth * col, rowHeight * row, colWidth, rowHeight));
                }
            }

        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g); 
            Graphics2D g2d = (Graphics2D) g;
            g2d.setColor(Color.RED);
            for (Shape cell : fill) {
                g2d.fill(cell);
            }
            g2d.setColor(Color.BLACK);
            for (Shape cell : grid) {
                g2d.draw(cell);
            }
        }

    }

}

附加

不保留从一个油漆周期到另一个油漆周期的信息。您需要按照希望的方式重新绘制组件。这意味着您需要维护一个可以随时重新绘制的点击列表。

Information from one paint cycle to another is not maintained. You are required to repaint the component exactly the way you want it to appear. This means you will need to maintain a list of click points that can be repainted at any time.

这篇关于Java如何在鼠标点击上绘制矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆