简体   繁体   English

Java-单击按钮后用鼠标拖动图形

[英]Java - Drawing shape with mouse and drag after clicking button

I would like to do this without the use of the JComponent. 我想在不使用JComponent的情况下执行此操作。 The idea is to have multiple buttons for each a shape, by clicking a button, I can then draw the shape for that button. 想法是每个形状都有多个按钮,通过单击一个按钮,然后可以为该按钮绘制形状。 Unfortunately, I can't even draw the shapes right now. 不幸的是,我什至无法绘制形状。

public static void main(String[] args) {
    JFrame frame = new JFrame();
    JButton rect = new JButton("Rectangle");
    ActionListener rListener = new RectangleNode();
    rect.addActionListener(rListener);
    MouseListener rMListener = new RectangleNode();
    rect.addMouseListener(rMListener);
    MouseMotionListener rMMListener = new RectangleNode();
    rect.addMouseMotionListener(rMMListener);
    JButton ellipse = new JButton("Ellipse");
    JPanel panel = new JPanel();
    panel.add(rect);
    panel.add(ellipse);
    frame.add(panel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(600, 600);
    frame.setTitle("Graph Draw");
    frame.setVisible(true);
}

The RectangleNode class RectangleNode类

public class RectangleNode implements ActionListener,MouseListener,MouseMotionListener {
    private Point p1;
    private Rectangle r;
    private boolean rdraw;

    @Override
    public void actionPerformed(ActionEvent e) {
        rdraw = true;   
    }

    @Override
    public void mousePressed(MouseEvent e) {
        if(rdraw = true){
            p1 = e.getPoint();
            r = new Rectangle(p1.x, p1.y, p1.x - p1.x, p1.y - p1.y);
        }
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseDragged(MouseEvent e) {
        if(rdraw = true){
            int x = Math.min(p1.x, e.getX());
            int y = Math.min(p1.y, e.getY());
            int width = Math.abs(p1.x - e.getX());
            int height = Math.abs(p1.y - e.getY());
            r.setBounds(x, y, width, height);
            repaint();
        }
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseClicked(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseEntered(MouseEvent e) {
        TODO Auto-generated method stub

    }

    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub

    }
    public void paintComponent(Graphics g) {  
        Graphics2D g2 = (Graphics2D)g;
        g2.draw(r); ;
    }
}

I'm not sure how to use repaint, and the paintComponent method in this situation. 在这种情况下,我不确定如何使用重绘和paintComponent方法。

Check out Custom Painting Approaches for an example of how to dynamically draw Rectangles. 查看“ 自定义绘画方法”以获取有关如何动态绘制矩形的示例。

In your case you want the ability to draw a Rectangle or an Ellipse so you would need to make some changes: 在您的情况下,您希望能够绘制矩形或椭圆形,因此需要进行一些更改:

  1. The Java API supports a Shape class. Java API支持Shape类。 A Shape can be a Rectangle, Ellipse, Polygon etc. So you would need to change the "ColoredRectangle" class to a "ColoredShape" class. 形状可以是矩形,椭圆形,多边形等。因此,您需要将“ ColoredRectangle”类更改为“ ColoredShape”类。 This would allow you to store a Rectangle or an Ellipse. 这将允许您存储矩形或椭圆形。

  2. Then in the paintComponent() code you would need to change the drawRect(..) method to be a draw( Shape ) so you can draw both Rectangles and Ellipses. 然后,在paintComponent()代码中,您需要将drawRect(..)方法更改为draw(Shape),以便可以绘制矩形和椭圆形。

  3. In the mouseDragged() logic you would need to modify the logic to check if you want to draw a Rectangle or an Ellispse. 在mouseDragged()逻辑中,您需要修改逻辑以检查是否要绘制Rectangle或Ellispse。 Then you would create the proper Shape and again use the draw( Shape ) method instead of the drawRect(...) method. 然后,您将创建合适的Shape并再次使用draw(Shape)方法而不是drawRect(...)方法。

  4. Then you would need to add a property to the class to control which Shape you want to paint. 然后,您需要向类添加一个属性,以控制要绘制的Shape。 Then when you click the button you set the property. 然后,当您单击按钮时,设置属性。 Or maybe instead of using a button you set up radio buttons for each Shape. 或者,也可以不使用按钮,而是为每个Shape设置单选按钮。

Anyway, play with the original code to understand how it works. 无论如何,请使用原始代码来了解其工作原理。 That is start by converting the code to use the ColoredShape class just for Rectangles. 首先,将代码转换为仅对矩形使用ColoredShape类。 Then once that works you can add the support for the Ellipse. 然后,一旦可行,您就可以添加对Ellipse的支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM