I would like to know how to implement drawing graphics (for example, using the g.drawPolyline method on any particular element, say on JPanel or something else, so that when using Layout s, when the window is resized, the graph moves along the program window along with the elements , in the same way as JButton and others move, but not superimposed on other elements and did not disappear outside the window (after all, I draw using g.fillRect , g.drawPolyline and others that use absolute coordinates - for example, in g.fillRect you need to specify the upper left point of the right flax, its width and height, and the coordinates of the left upper point are taken as absolute values from the upper left corner of the program window itself. And is there any other way to draw a graph as well (using arrays with coordinates of points of this graph)?
1 answer
You can create a window ( JFrame )
JFrame window = new JFrame(); Then, install, for example, BorderLayout :
window.setLayout(new BorderLayout()); Place a JPanel on which we will draw, for example, in the center:
JPanel graph = new JPanel(); window.add(graph, BorderLayout.CENTER); I recommend using the following method:
For JPanel , create your own class, override the component's drawing in it:
@Override protected void paintComponent(Graphics g2) { Graphics2D g = (Graphics2D)g2.create( x_offset_of_graphic, // координаты новой точки начала отсчёта y_offset_of_graphic, // до этого был левый верхний угол width_of_graphic, // размеры области, height_of_graphic // за которыми рисоваться ничего не будет ); You can also create the width and height for the chart as global variables in this class, and from the main class (from JFrame ) set these variables to the same value as the width / height of the JPanel itself, the instance of which you will create. But, then the graph itself, which you are building, must be related to the size of the panel, otherwise everything will move down, like this:
Window completely 
The result of the shift of elements inside the Layout
(low quality graphics shift) 
Shift quality graphics:
You draw with the help of g.drawLine , g.drawRect and other methods your schedule, it refers to only one panel and is updated with the repaint() this panel, and is also tied to its size, which means that with any change in window size and other manipulations will move, which I wanted to achieve.