简体   繁体   English

Java Swing用鼠标移动形状

[英]Java Swing moving shapes with mouse

I am working on a simple object drawing program using Swing in Java. 我正在使用Java中的Swing开发一个简单的对象绘图程序。 My program simply should draw shapes according to buttons when clicked, and move any shapes with the mouse. 我的程序只需单击鼠标即可根据按钮绘制形状,然后用鼠标移动任何形状。 I have four buttons which draw rectangle, circle and square on screen. 我有四个按钮,可在屏幕上绘制矩形,圆形和正方形。 So far I did managed to draw to shapes when you click on buttons. 到目前为止,当您单击按钮时,我确实设法绘制了形状。 but i want to move the shapes on screen which it did not work out. 但我想在屏幕上移动形状,但效果不佳。

The problem is this: When I click on circle shape to drag it around with mouse, it clears all the screen and noting is on the screen. 问题是这样的:当我单击圆形并用鼠标将其拖动时,它会清除所有屏幕,并且屏幕上会出现注释。

And, is there a way to clean all the screen when I click on clear button? 而且,当我单击清除按钮时,是否可以清洁所有屏幕?

Thank you? 谢谢?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class PaintProject extends JComponent implements ActionListener, 
    MouseMotionListener {

    private  int CircleX=0;
    private  int CircleY=0;
    private  int RectX=100;
    private  int RectY=100;
    private  int SquareX=300;
    private  int SquareY=200;


    public static void main(String[] args) {
        JFrame frame = new JFrame("NEW PAINT PROGRAME!");

        JButton CircleButton = new JButton("Circle");
        CircleButton.setActionCommand("Circle");

        JButton RectangleButton = new JButton("Rectangle");
        RectangleButton.setActionCommand("Rectangle");

        JButton SquareButton = new JButton("Square");
        SquareButton.setActionCommand("Square");

        PaintProject paint = new PaintProject();

        CircleButton.addActionListener(paint);
        RectangleButton.addActionListener(paint);
        SquareButton.addActionListener(paint);


        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout());
        frame.add(paint);
        frame.add(CircleButton);
        frame.add(RectangleButton);
        frame.add(SquareButton);


        frame.addMouseMotionListener(paint);

        frame.pack();
        frame.setVisible(true);
    }

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

    }

    private void drawCircle() {
        Graphics g = this.getGraphics();
        g.setColor(Color.red);
        g.fillOval(CircleX, CircleY, 100, 100);
    }

    private void drawRectangle() {
        Graphics g = this.getGraphics();
        g.setColor(Color.green);
        g.fillRect(RectX, RectY, 100, 300);
    }
    private void drawSquare() {
        Graphics g = this.getGraphics();
        g.setColor(Color.blue);
        g.fillRect(SquareX, SquareY, 100, 100);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String command = e.getActionCommand();
        if (command.equals("Circle")) {
        drawCircle();
    }
    else if (command.equals("Rectangle")) {
        drawRectangle();
    }
    else if (command.equals("Square")) {
        drawSquare();
    }

    }

    @Override
    public void mouseDragged(MouseEvent e) {
        CircleX=e.getX();
        CircleY=e.getY();
        repaint();
    }

}

As noted here and here , getGraphics() is not how to perform custom painting in Swing. 如此此处所述getGraphics()不是如何在Swing中执行自定义绘制 Instead, override paintComponent() to render the desired content. 而是重写paintComponent()来呈现所需的内容。 It looks like you want to drag shapes using the mouse. 您似乎想使用鼠标拖动形状。 A basic approach to moving a selected object is shown here ; 这里显示移动所选对象的基本方法; substitute your fillXxx() invocation for the drawString() shown there. 将您的fillXxx()调用替换为fillXxx()显示的drawString() For multiple shapes, use a List<Shape> ; 对于多个形状,请使用List<Shape> ; the clear command then becomes simply List::clear . clear命令将变成简单的List::clear A complete example is cited here ; 这里举一个完整的例子; it features moveable, selectable, resizable, colored nodes connected by edges. 它具有通过边缘连接的可移动,可选,可调整大小的彩色节点。

图片

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

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