拖动形状

时间:2017-11-18 09:37:36

标签: java swing user-interface

嗨,我是初学者,我正在做一个带有swing的java程序,选择一个形状,然后拖动它来制作并在屏幕上绘制一个形状(见下图)。

enter image description here

但我有一些问题:

  1. 当我画一个新形状时,第一个消失。
  2. 形状可以画出边框(见下图)
  3. 我只能从左向右拖动,我无法从右向左拖动。
  4. 2)的屏幕截图:

    enter image description here

    这是我的DrawPanel类:

    class DrawingPanel extends JPanel {
    
        public DrawingPanel() {
    
            setBackground(Color.WHITE);
        }
    
        public void paint(Graphics g) {
    
            super.paint(g);
    
            if (x == 1) {
    
                drawCircle(g);
            }
    
            else if (x == 2) {
    
                drawOval(g);
            }
    
            else if (x == 3) {
    
                drawRectangle(g);
            }
    
            else if (x == 4) {
    
                drawSquare(g);
            }
    
            else if (x == 5) {
    
                drawLine(g);
            }
    
    
    
        }
    
        public void drawCircle(Graphics g) {
            super.paint(g);
            g.drawOval(xStrart, yStrart, width, width);
    
        }
    
        public void drawOval(Graphics g) {
            super.paint(g);
            g.drawOval(xStrart, yStrart, width, height);
    
        }
    
        public void drawRectangle(Graphics g) {
            super.paint(g);
            g.drawRect(xStrart, yStrart, width, height);
    
        }
    
        public void drawSquare(Graphics g) {
            super.paint(g);
            g.drawRect(xStrart, yStrart, width, width);
    
        }
    
        public void drawLine(Graphics g) {
            super.paint(g);
            g.drawLine(xStrart, yStrart, xEnd, yEnd);
    
        }
    
    }
    

0 个答案:

没有答案