P
P
Pavel Subbotin2021-01-26 13:05:03
Java
Pavel Subbotin, 2021-01-26 13:05:03

Who can help with my paint in Java?

The guys in general wrote paint in the classroom and now I need help. When I draw a square, I want it to be displayed before I draw it, but I have a problem. Who can help?
Here is the whole problem in the video: https://youtu.be/iVjPqU1H_2A .

The code:

case 5:
                        if (endX != 0 && endY != 0){
                            g2.setColor(Color.white);
                            g2.drawRect(startX, startY, endX, endY);
                        }
                        g2.setColor(mainColor);
                        if (xN < e.getX() && yN < e.getY()) {
                            g2.drawRect(xN, yN, e.getX() - xN, e.getY() - yN);
                            endX = e.getX()-xN;
                            endY = e.getY()-yN;
                            startX = xN;
                            startY = yN;
                        }
                        else if (xN > e.getX() && yN <e.getY()) {
                            g2.drawRect(e.getX(), yN, xN-e.getX(), e.getY() - yN);
                            endX = xN - e.getX();
                            endY = e.getY()-yN;
                            startX = e.getX();
                            startY = yN;
                        }
                        else if(xN < e.getX() && yN > e.getY()) {
                            g2.drawRect(xN, e.getY(), e.getX() - xN, yN - e.getY());
                            endX = e.getX()-xN;
                            endY = yN - e.getY();
                            startX = xN;
                            startY = e.getY();
                        }
                        else {
                            g2.drawRect(e.getX(), e.getY(), xN - e.getX(), yN - e.getY());
                            endX = xN - e.getX();
                            endY = yN - e.getY();
                            startX = e.getX();
                            startY = e.getY();
                        }
                        break;

I understand that the error is in the line with changing the color to white (g2.setColor(Color.white)) and I want to know how to change this so that I can understand what I am drawing.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vasily Bannikov, 2021-01-26
@vabka

None.

D
Dmitry Alexandrov, 2021-01-26
@jamakasi666

Dumb way:

1) перед началом рисования полностью копируете весь холст без новой фигуры. Т.е. в момент тыка мышкой.
...
2) Вставляете на основной холст чистую копию без фигуры
3) Рисуете прямоугольник
g2.setColor(Color.white);
g2.drawRect(startX, startY, endX, endY);

According to the correct one, for which your hands will not be torn off.
Before drawing the figure on top of the canvas, make a transparent canvas and draw on it already. As soon as we have drawn, we reduce the result obtained.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question