How does the drawing guide in this java paint program work?

91 Views Asked by At

I'm currently doing a paint program where we are drawing squares where they must draw as the user drags the mouse. My professor taught it to us using the graphics XOR mode.

I found this code and it looks much more efficient. I am confused as to how the guide works, the way its written to me looks like as they are dragging the mouse. It would just create a bunch of different rectangles and they would all stay on the screen.

How is it that the rectangle is continuously updating to the users mouse but not staying on the screen?

Is it because the shape is not being added to an ArrayList?

public DrawingBoard() {
    this.addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent e) {
            // When the mouse is pressed get x & y position
            drawStart = new Point(e.getX(), e.getY());
            drawEnd = drawStart;
            repaint();
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            // Create a shape using the starting x & y
            // and finishing x & y positions
            Shape aShape = drawRectangle(drawStart.x, drawStart.y, e.getX(), e.getY());

            // Add shapes, fills and colors to there ArrayLists
            shapes.add(aShape);
            shapeFill.add(fillColor);
            shapeStroke.add(strokeColor);

            drawStart = null;
            drawEnd = null;

            // repaint the drawing area
            repaint();
        }
    });

    this.addMouseMotionListener(new MouseMotionAdapter() {
        @Override
        public void mouseDragged(MouseEvent e) {
            // Get the final x & y position after the mouse is dragged
            drawEnd = new Point(e.getX(), e.getY());
            repaint();
        }
    });
}

public void paint(Graphics g) {
    // Class used to define the shapes to be drawn
    Graphics2D graphSettings = (Graphics2D) g;

    // Antialiasing cleans up the jagged lines and defines rendering rules
    graphSettings.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
            RenderingHints.VALUE_ANTIALIAS_ON);

    // Defines the line width of the stroke
    graphSettings.setStroke(new BasicStroke(2));

    // Iterators created to cycle through strokes and fills
    Iterator<Color> strokeCounter = shapeStroke.iterator();
    Iterator<Color> fillCounter = shapeFill.iterator();

    // Eliminates transparent setting below
    graphSettings.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f));

    for (Shape s : shapes) {
        // Grabs the next stroke from the color arraylist
        graphSettings.setPaint(strokeCounter.next());
        graphSettings.draw(s);

        // Grabs the next fill from the color arraylist
        graphSettings.setPaint(fillCounter.next());
        graphSettings.fill(s);
    }

    // Guide shape used for drawing
    if (drawStart != null && drawEnd != null) {
        // Makes the guide shape transparent
        graphSettings.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.40f));

        // Make guide shape gray for professional look
        graphSettings.setPaint(Color.LIGHT_GRAY);

        // Create a new rectangle using x & y coordinates
        Shape aShape = drawRectangle(drawStart.x, drawStart.y, drawEnd.x, drawEnd.y);
        graphSettings.draw(aShape);
    }

}
1

There are 1 best solutions below

0
YCXU1993 On

The code will store all the rectangles user created. If you want one rectangle in the screen, you can draw the rectangle without saving it in the arraylist. delete all codes which are related to the arraylist.