I need to create a class in which with the help of Graphics, draw the necessary elements from the lines, then save them in image and pass them to the main class. Now, through the second application, I save the image and in the Main class I read it from the directory. Here is the code. I don’t understand how to do this at all. Perhaps you need to create a method that will return an image.
import javax.imageio.ImageIO; import javax.swing.; import java.awt.; import java.io.File; import java.io.IOException; import java.awt.image.*; public class Main extends JFrame{ public static void main(String[] args) throws IOException { Main main_window = new Main(); main_window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); main_window.setLocation(0,0); Dimension sSize = Toolkit.getDefaultToolkit ().getScreenSize (); int vert = sSize.height; int hor = sSize.width; main_window.setSize(hor,vert); main_window.setResizable(false); MainField game_field = new MainField(); game_field.setBackground(Color.WHITE); main_window.add(game_field); main_window.setVisible(true); game_field.setLayout(null); } private static void onRepaint (Graphics g ){ g.setColor(Color.BLACK); BufferedImage img = new BufferedImage(50,50, BufferedImage.TYPE_INT_ARGB); Graphics2D g2D = img.createGraphics(); g2D.setStroke(new BasicStroke(3.0f)); g2D.setColor(Color.BLACK); // Рисую что необходимо g2D.dispose(); try { ImageIO.write(img, "png", new File("src/lol/Питание 0.png")); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private static class MainField extends JPanel{ protected void paintComponent (Graphics g){ super.paintComponent(g); onRepaint(g); repaint(); } } }