如何停止重绘()闪烁

时间:2013-06-15 01:27:13

标签: java swing paint japplet event-dispatch-thread

我正在尝试为java创建一个程序,我有闪烁的常见问题。我尝试过很多东西要消除,但都是一样的。而我画的椭圆正在移动japplet是闪烁的。我需要你的帮助来解决这个问题。这是我的代码:

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);
    }
}

非常感谢,抱歉我的英语不是很好!

1 个答案:

答案 0 :(得分:11)

有很多事情在我身上跳出来。

  • 您从JApplet延伸,但正在将其添加到JFrame
  • 您正在使用自定义绘画混合组件
  • 不使用布局管理器。
  • 使用getGraphics

<强>首先...

您永远不应该覆盖顶级容器的paint(例如JApplet)。有很多原因,你找到了一个。顶级容器不是双缓冲的。相反,您应该创建一个自定义组件(通过扩展JPanel之类的内容)并覆盖它的paintComponent方法......

<强>其次

决定你的申请方式。它是applet还是应用程序?如果你遵循第一点,那么它真的没关系,因为你只需要将面板添加到顶层容器。

<强>第三

我会创建一个执行自定义绘画的面板。然后我会为所有字段和应用程序的其他部分创建单独的容器。这将允许您分离责任区域。它还允许您使用布局管理器;)

<强>第四

使用布局管理器。布局管理器API旨在解决GUI设计中最令人讨厌的问题之一,你要求很多麻烦并且你决定丢弃它 - 恕我直言。

<强>第五

永远不要使用

getGraphics。除了它可以返回null之外,它只是屏幕上当前显示的内容的快照。一旦RepaintManager决定执行重绘,任何呈现给它的内容都将丢失。您应该使用paintComponent更新自定义窗格的状态。