Layered painting by java?

I'm basically trying to do something like the classic "Paint" (Microsoft program). But I want to work with layers when drawing. I thought I could use the JPanel component as a layer.

I tested the code below. The goal is to draw a rectangle with the mouse. There is a temporary level (temp) for drawing on it when dragging the mouse, and there is an actual layer (area) for drawing when the mouse is released. But every time I start drawing a new rectangle, the old ones disappear. Also, if I run setVisible (false) again and true, everything will disappear.

MouseInputAdapter mia = new MouseInputAdapter() { private int startx = 0, starty = 0, stopx = 0, stopy = 0; public void mousePressed(MouseEvent evt) { startx = evt.getX(); starty = evt.getY(); } public void mouseDragged(MouseEvent evt) { Graphics2D tempg = (Graphics2D) temp.getGraphics(); int width = Math.abs(startx - evt.getX()); int height = Math.abs(starty - evt.getY()); int x = evt.getX(), y = evt.getY(); if(x > startx) x = startx; if(y > starty) y = starty; Rectangle r = new Rectangle(x, y, width, height); tempg.clearRect(0, 0, getWidth(), getHeight()); tempg.draw(r); } public void mouseReleased(MouseEvent evt) { Graphics2D g = (Graphics2D) area.getGraphics(); stopx = evt.getX(); stopy = evt.getY(); int width = Math.abs(startx - stopx); int height = Math.abs(starty - stopy); int x = startx, y = starty; if(x > stopx) x = stopx; if(y > stopy) y = stopy; Rectangle r = new Rectangle(x, y, width, height); g.draw(r); } }; area.addMouseListener(mia); area.addMouseMotionListener(mia); temp.addMouseListener(mia); temp.addMouseMotionListener(mia); 

What is wrong with this code?

+4
source share
4 answers

That's what I was looking for; http://www.leepoint.net/notes-java/examples/mouse/paintdemo.html

My mistake; using the getGraphics () method from paintComponent () and waiting for the changes to be saved.

Why @Keilly doesn't work for me; Because if I placed the shapes in a list or array when the shape changed (for example, deleting the 1/4 circle), I cannot update the item in the list. Because she is no longer the same. Therefore, I need to save the figures in the form of drawings, and I do not need (and do not want) to save them separately.

0
source

Every time you redraw, there is no guarantee that you will get the same graphics in the state that you left.

Instead of two two steps:

  • Create a list of rectangles in your class.
  • In your mouse listener, instead of drawing on a graph, add a rectangle to the list.
  • Override paintComponent and there draw a list of rectangles on the graphics passed to them.

Using a list is good, since the elements at the beginning of the list will be colored below at the end.

+6
source

Classic bitmap graphics software works with the target bitmap. You can display multiple Layer in paintComponent() , as @Keily offers for Rectangle s.

Alternatively, you can see the classic object-oriented drawing marked.

+2
source

Here is a general idea: (I assume you mean layers, for example in Photoshop)

Set up one JPanel for drawing.

Create a data structure containing all the available objects for drawing.

In this data structure, also create a field containing an integer expressing to which layer it is bound to a specific addressable object.

In your paintComponent () method, check which layer is currently active, and only draw data at this level or below.

+1
source

Source: https://habr.com/ru/post/1395977/


All Articles