如何在JComponent或Jpanel上绘制多个形状?

时间:2016-09-23 12:37:07

标签: java swing jpanel draw

我正在尝试构建 Paint 应用,并且我在DrawingArea类中做错了什么。 问题是当我尝试绘制第二个形状时,第一个形状或图形是自动删除所以我需要一些关于如何解决这个问题的想法。所有答案都可以接受。 感谢帮助。

DrawingArea.class 代码的一部分:

    @Override // GETTING FIRST (STARTING) COORDINATE WHEN THE MOUSE PRESSED
    public void mousePressed(MouseEvent e) {
        oldX = e.getX();
        oldY = e.getY();
        repaint();

    }

    @Override // GETTING RELEASED COORDINATE TO DRAW LINE.
    public void mouseReleased(MouseEvent e) {
        lastX = e.getX();
        lastY = e.getY();
        repaint();
    }

    public void mouseClicked(MouseEvent e) {
        clickedX = true;
        COUNT = e.getClickCount();
    }

    // GETTING COORDINATE TO DRAW FILLEDRECT,FILLEDOVAL,OVAL,RECT.
    public void mouseDragged(MouseEvent e) {
        draggedX = e.getX();
        draggedY = e.getY();
        repaint();
        width = Math.abs(oldX - draggedX);
        height = Math.abs(oldY - draggedY);
        x = Math.min(draggedX, oldX);
        y = Math.min(draggedY, oldY);

    }

    public void mouseMoved(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

    // CLEAR THE ALL SHAPES DRAWED ON DRAW AREA.
    public void clear() {
        g2.setColor(Color.WHITE);
        g2.fillRect(0, 0, (int) this.getWidth() + 55, (int) this.getHeight() + 55);
        super.repaint();
    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (image == null) {
            image = new BufferedImage((int) this.getWidth(), (int) this.getHeight(), BufferedImage.TYPE_INT_ARGB);
            g2 = (Graphics2D) image.getGraphics();
            g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            clear();
        }

        g2.drawImage(image, 0, 0, getWidth(), getHeight(), this);
        g2.dispose();

        g.setColor(initialColor);
        if (shape == Shapers.PENCIL) {
            g.setColor(currentColor);
            g.fillOval(draggedX, draggedY, thickness, thickness);

        } else if (shape == Shapers.OVAL) {
            g.setColor(currentColor);
            g.drawOval(oldX, oldY, draggedX, draggedX);

        } else if (shape == Shapers.FILLEDOVAL) {
            g.setColor(currentColor);
            g.fillOval(oldX, oldY, draggedX, draggedY);

        } else if (shape == Shapers.RECT) {
            g.setColor(currentColor);
            g.drawRect(x, y, width, height);

        } else if (shape == Shapers.FILLEDRECT) {
            g.setColor(currentColor);
            g.fillRect(x, y, width, height);

        } else if (shape == Shapers.LINE) {
            g.setColor(currentColor);
            g.drawLine(oldX, oldY, draggedX, draggedY);
            oldX = draggedX;
            oldY = draggedY;

        } else if (shape == Shapers.ERASER) {
            g.setColor(Color.WHITE);
            g.fillRect(draggedX, draggedY, thickness, thickness);

        } else if (shape == Shapers.TEXT) {
            if (clickedX == true || COUNT == 2) {
                String str = JOptionPane.showInputDialog("Write Your Text Here : ");
                g.setFont(myFont);
                g.setColor(currentColor);
                if (str != null) {
                    g.drawString(str, oldX, oldY);
                    COUNT = 0;

                } else {
                    return;
                }
            }
        } else {
            COUNT = 0;
            return;
        }

    }


}

1 个答案:

答案 0 :(得分:3)

您需要:

  1. 存储要在List中绘制的形状,然后在paintComponent()方法中绘制List中的所有形状,或者
  2. 将您的形状绘制为BufferedImage,然后仅绘制BufferedImage
  3. 查看Custom Painting Approaches了解这两种方法的工作示例,并使用最符合您要求的方法。