P
P
Pavel Subbotin2021-01-26 00:42:43
Java
Pavel Subbotin, 2021-01-26 00:42:43

Who can help with two questions on java?

Guys in general, we made a simple paint in the classroom and we were asked dz. Basically I need help with two questions.
1) How to implement such a system that before you draw a square / circle, it is displayed and you understand what size it will be, etc.
2) How to implement the fill? I was able to paint over the entire area, but how to make it paint over, say, the inside of the square or the area around it?

In general, here is the whole base, where everything is drawn:

workSpace.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                xClick = e.getX();
                yClick = e.getY();

                xN = e.getX();
                yN = e.getY();
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                g = image.getGraphics();
                g2 = (Graphics2D) g;
                g2.setColor(mainColor);

                switch (mode) {
                    case 6: //линия
                        g2.drawLine(xN, yN, e.getX(), e.getY());
                        break;
                    case 5: //квадрат
                        if (xN < e.getX() && yN < e.getY()) g2.drawRect(xN, yN, e.getX() - xN, e.getY() - yN);
                        else if (xN > e.getX() && yN <e.getY()) g2.drawRect(e.getX(), yN, xN-e.getX(), e.getY() - yN);
                        else if(xN < e.getX() && yN > e.getY()) g2.drawRect(xN, e.getY(), e.getX() - xN, yN - e.getY());
                        else g2.drawRect(e.getX(), e.getY(), xN - e.getX(), yN - e.getY());
                        break;
                    case 7: //окружность
                        if (xN < e.getX() && yN < e.getY()) g2.drawOval(xN, yN, e.getX() - xN, e.getY() - yN);
                        else if (xN > e.getX() && yN <e.getY()) g2.drawOval(e.getX(), yN, xN-e.getX(), e.getY() - yN);
                        else if(xN < e.getX() && yN > e.getY()) g2.drawOval(xN, e.getY(), e.getX() - xN, yN - e.getY());
                        else g2.drawOval(e.getX(), e.getY(), xN - e.getX(), yN - e.getY());
                        break;
                    case 4: //текст
                        workSpace.requestFocus();
                        break;
                    case 3://заливка
                        g2.setColor(mainColor);
                        g2.fillRect(0, 0, frame.getWidth(), frame.getHeight());
                        break;
                }

                workSpace.repaint();
            }
        });
        workSpace.addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                g = image.getGraphics();
                g2 = (Graphics2D) g;

                g2.setColor(mainColor);

                switch (mode) {
                    case 0://карандаш
                        g2.drawLine(xClick, yClick, e.getX(), e.getY());
                        break;
                    case 1: //кисть
                        g2.setStroke(new BasicStroke(brushSize/2));
                        g2.drawLine(xClick, yClick, e.getX(), e.getY());
                        break;
                    case 2://ластик
                        g2.setColor(Color.WHITE);
                        g2.setStroke(new BasicStroke(brushSize));
                        g2.drawLine(xClick, yClick, e.getX(), e.getY());
                        break;
                }

                xClick = e.getX();
                yClick = e.getY();
                workSpace.repaint();
            }
        });

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question