如何停止重绘()闪烁 [英] How to stop repaint() flickering

查看:171
本文介绍了如何停止重绘()闪烁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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!

推荐答案

我有很多事情要发生。


  • 您从 JApplet 延伸,但是将其添加到 JFrame

  • 您正在使用自定义绘画混合组件

  • 不使用布局管理器。

  • 使用 getGraphics

  • 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.

首先......

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

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...

其次

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

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.

第三

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

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

第四

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

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.

第五

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

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.

这篇关于如何停止重绘()闪烁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆