The screen renders everything in the right place but the entire screen flickers while it's open. If I don't add "g.drawImage(img,0,0,null);" then it works fine. I know that maybe I have to use double buffering,but I work with Swing first time and don't have any ideas how to use double buffering correctly. "setDoubleBuffered(true)" doesn't work. Thank you in advance.
import java.awt.*;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.*;
public class GraphApp extends JFrame {
int x,y;
int ax,by;
Image img = Toolkit.getDefaultToolkit().getImage("C:\\Users\\User\\IdeaProjects\\vychmat\\images\\Background.png");
public GraphApp(){
setTitle("Лабораторная работа №2");
setSize(900,700);
try{
setIconImage(ImageIO.read(new File("C:\\Users\\User\\IdeaProjects\\vychmat\\images\\icon.png")));
}
catch(Exception e){
e.getMessage();
}
setVisible(true);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void paint(Graphics g){
g.drawImage(img,0,0,null);
g.setColor(Color.BLACK);
g.drawString("Y", 210, 250);
g.drawString("X", 390, 440);
if(x==205&&y==425){
g.drawString("Origin(0,0)", 205, 425);
}
//OY
g.drawLine(200, 250, 200, 600);
//OX
g.drawLine(30,425,380,425);
g.setFont(new Font("Palatino Linotype", Font.BOLD,25));
g.drawString("Решение уравнений с заданной точностью",150,75);
repaint();
}
public static void main(String[] args){
new GraphApp();
}
}