0

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();
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
FuryMaxim
  • 65
  • 1
  • 8
  • 2
    1) Don't call repaint from within a paint method. For animation, create a Swing `Timer` to invoke `repaint()` at set intervals. 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). E.G. [This answer](https://stackoverflow.com/a/10862262/418556) hot links to an image embedded in [this question](https://stackoverflow.com/q/10861852/418556). 3) The `JFrame` icon is likely irrlevant to the problem. Test that by removing it from the example code. 4) A single blank line of white space in source code is all .. – Andrew Thompson Nov 17 '18 at 12:43
  • .. that is *ever* needed. Blank lines after `{` or before `}` are also typically redundant. – Andrew Thompson Nov 17 '18 at 12:43
  • 1
    5) When custom painting, always call the `super` method first. 6) But paint to a `JPanel` using the `paintComponent(..)` method. 7) `g.setFont(new Font("Palatino Linotype", Font.BOLD,25));` Create the font once and store it as an attribute of the class. There is no benefit to creating it each time paint is called. – Andrew Thompson Nov 17 '18 at 12:53
  • Thank you, so, I put 18500 ms delay and my screen flickers after 18.5s anyway.I use graphics because I want to display real-time parabola graph for example Timer timer = new Timer(18500, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { repaint(); } }); timer.start(); – FuryMaxim Nov 17 '18 at 12:55
  • 1
    1) *"I put 18500 ms delay"* That's a ***very*** slow animation! 2) *"I use graphics because.."* Keep using `Graphics` but in a `JPanel` 3) *"for example"* So .. the code uses a `Timer` now? That code should be in the question. Please [edit] to add it. Make the other changes I suggested, while you're at it. That's points 2, 3, 5, 6 & 7. – Andrew Thompson Nov 17 '18 at 13:46
  • `I know that maybe I have to use double buffering` - Swing is double buffered by default. The problem is you are turning it off because you are overriding paint() on the frame. **Don't override paint() on the frame**. Custom painting is done by overriding the `paintComponent()` method of a JPanel and then you add the panel to the frame. Read the section from the Swing tutorial on [Custom Painting](https://docs.oracle.com/javase/tutorial/uiswing/painting/index.html) for more information and working examples to get you started. – camickr Nov 17 '18 at 15:56

0 Answers0