2

I am working on a simple object drawing program using Swing in Java. 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();
    }

}
4

1 回答 1

3

herehere所述,getGraphics()不是如何在Swing中执行自定义绘画。相反,覆盖paintComponent()以呈现所需的内容。看起来您想使用鼠标拖动形状。此处显示了移动选定对象的基本方法;用您的fillXxx()调用代替drawString()那里显示的内容。对于多个形状,使用List<Shape>; 然后清除命令变得简单List::clear这里引用了一个完整的例子;它具有由边缘连接的可移动、可选择、可调整大小、彩色节点。

图片

于 2016-07-10T15:02:32.030 回答