Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
622 views
in Technique[技术] by (71.8m points)

java - How to stop repaint() flickering

I am trying to make a program to java and i have the common problem with flickering. I have try many things to eliminate but all the same. while the oval that i paint is moving the japplet is flickering. i need your help to solve this problem. here is my code:

import java.awt.Color;

public class all extends JApplet implements Runnable {

    double x=0;
    double y=0;
    int m=0;
    int n=0;
    int f=30;
    int μ=0;
    Thread kinisi;
    JPanel panel;
    JFrame frame;
    private boolean running = false;
    private JTextField textField1;
    private JTextField textField2;
    Image backGround;
    JPanel panel_3;
    Image bf = createImage(m, n);
    private Graphics doubleg;
    private Image i;

    public void init() {
        this.setSize(800, 700);
    }

    public all() {
        getContentPane().setLayout(null);

        JButton btnNewButton = new JButton("Start");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) { 
                String b=textField2.getText();
                String z =textField1.getText();
                if (textField1.getText().equals("") || 
                    textField2.getText().equals("")){
                    JOptionPane.showMessageDialog(
                        new JFrame(),
                        "Δωστε τιμ? για το φ και το μ!",
                        "ERROR",JOptionPane.ERROR_MESSAGE);
        } else{
                f = Integer.parseInt(b);
                μ = Integer.parseInt(z); 
                Start();    }
            }
        });
        btnNewButton.setBounds(469, 168, 89, 23);
        getContentPane().add(btnNewButton);

        JButton btnStop = new JButton("Pause");
        btnStop.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                pause();
            }
        });
        btnStop.setBounds(588, 168, 89, 23);
        getContentPane().add(btnStop);

        JButton btnReset = new JButton("Reset");
        btnReset.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Reset();
            }
        });
        btnReset.setBounds(701, 168, 89, 23);
        getContentPane().add(btnReset);

        JLabel label = new JLabel("u03BC");
        label.setHorizontalAlignment(SwingConstants.CENTER);
        label.setBounds(549, 63, 46, 14);
        getContentPane().add(label);

        textField1 = new JTextField();
        textField1.setBounds(529, 101, 86, 20);
        getContentPane().add(textField1);
        textField1.setColumns(10);

        JLabel label_1 = new JLabel("u03C6");
        label_1.setHorizontalAlignment(SwingConstants.CENTER);
        label_1.setBounds(681, 63, 46, 14);
        getContentPane().add(label_1);

        textField2 = new JTextField();
        textField2.setBounds(667, 101, 86, 20);
        getContentPane().add(textField2);
        textField2.setColumns(10);

        JButton btnNewButton_1 = new JButton("");
        btnNewButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                JOptionPane.showMessageDialog(
                    new JFrame(),
                    "Οδηγ?ε? προγρ?μματο?","Οδηγ?ε?",
                    JOptionPane.INFORMATION_MESSAGE);
                }
        });
        btnNewButton_1.setIcon(
            new ImageIcon(all.class.getResource("/Images/info.png")));
        btnNewButton_1.setBounds(732, 5, 39, 35);
        getContentPane().add(btnNewButton_1);

        JLabel label_2 = new JLabel("");
        label_2.setIcon(
            new ImageIcon(all.class.getResource("/Images/earth.jpg")));
        label_2.setBounds(-20, 0, 820, 361);
        getContentPane().add(label_2);

        JPanel panel_1 = new JPanel();
        panel_1.setBorder(
            new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
        panel_1.setBounds(10, 372, 369, 290);
        getContentPane().add(panel_1);

        JPanel panel_2 = new JPanel();
        panel_2.setBorder(
            new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
        panel_2.setBounds(408, 372, 369, 290);
        getContentPane().add(panel_2);
    }

    public void paint(Graphics g){
        super.paint(g);
        g.drawLine(0,f,350,200);
        g.drawLine(0,200,350,200);  
        g.drawOval(m,n,40,40);
        Color brown=new Color(137,66,0);
        g.setColor(brown);
        g.fillOval(m, n, 40, 40);
        //Graphics2D g2d = (Graphics2D) g;
        g.drawLine(460,400,460,650);
        g.drawLine(20, 620, 350, 620);
        g.drawLine(50,400,50,650);
        g.drawLine(430, 620, 760, 620); 
    }

    public void Start() {
        kinisi = new Thread(this);
        kinisi.start();
        running = true; 
    }

    public void run() {
        while (running) {
            if (x < 340){
                double l = 200-f;
                double k = l/350;
                double y=k*x+f-30;
                x= x+1;
                m = (int) x;
                n = (int) y;
                repaint();
                try {
                    Thread.sleep(μ);  
                } catch (InterruptedException ie) {}
            }
        }
    }

    public void update(Graphics g) {
        if(i==null){
            i=createImage(800,700);
            doubleg = i.getGraphics();
        }
        doubleg.setColor(getBackground());
        doubleg.fillRect(0,0,800,700);
        doubleg.setColor(getForeground());
        paint(doubleg);
        g.drawImage(i,0,0,this);
    }

    public void paint1(Graphics g){
        g.drawLine(0, f ,350, 200);
        g.drawOval(m, n, 40, 40);
        Color brown=new Color(137,66,0);
        g.setColor(brown);
        g.fillOval(m, n, 40, 40);
    }

    public void pause() {
        if (kinisi != null) {
            running = false;
        }
    }

    public boolean Reset() {
        if (kinisi != null) {
            running = false;
            kinisi.interrupt();
            kinisi = null;
            x=0;
            y=0;
            f=30;
            m=0;
            n=0;
            repaint();
            textField1.setText("");
            textField2.setText("");
        }
        Graphics g = getGraphics();
        g.drawOval(m,n,40,40);
        Color brown=new Color(137,66,0);
        g.setColor(brown);
        g.fillOval(m, n, 40, 40);
        return false;
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("FISIKI");
        frame.getContentPane().add(new all());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800, 700);
        frame.setVisible(true); 
        frame.setResizable(false);
    }
}

Thank you very much and sorry for my english are not very good!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There are a number of things that jump out at me.

  • You're extending from JApplet, but are adding it to a JFrame
  • You're mixing components with you custom painting
  • Not using layout managers.
  • Using getGraphics.

Firstly...

You should never override paint of a top level container (like JApplet). There are many reasons and you've found one. Top level containers are not double buffered. Instead, you should be creating a custom component (by extending something like JPanel) and overriding it's paintComponent method...

Secondly

Decided how you application is going to be. Is it an applet or application? If you follow the first point, then it really doesn't matter, as you only simply need to add the panel to the top level container.

Thirdly

I would create a panel that did the custom painting. Then I would create separate containers for all the fields and other parts of the application. This will allow you to separate the areas of responsibility. It would also allow you to use layout managers ;)

Fourthly

Use layout managers. The layout manager API has being designed to solve one of this most annoying aspects of GUI design, you're asking for a lot of trouble and work you decided to discard it - IMHO.

Fifthly

getGraphics should never be used. Apart from the fact that it can return null, it is only a snap shot of what is currently on the screen. As soon as the RepaintManager decides to perform a repaint, anything rendered to it will be lost. You should use paintComponent to update the state of your custom pane.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...