How to draw multiple shapes on JComponent or Jpanel?

Coder ACJHP

I'm trying to build Paint app and I doing something wrong in DrawingArea class. The problem is when I try to draw second shape , the first shape or figure is auto deleting so I need to some idea about how to solve this.All answers acceptable. THANKS FOR HELP.

There is part of DrawingArea.class codes :

    @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;
        }

    }


}
camickr

You need to either:

  1. Store shapes to be painted in a List and then in the paintComponent() method you paint all the shapes in the List, or
  2. Paint your shapes to a BufferedImage and then just paint the BufferedImage

Check out Custom Painting Approaches for working examples of both approaches and use the approach that best meets your requirement.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

TOP Ranking

HotTag

Archive