我使用双向链表来创建此移动的正弦曲线(代码可能非常原始且杂乱无章,但这只是初稿,我几乎不知道如何使用Swing。):

import java.awt.*;
import javax.swing.JFrame;

public class DisplayGraphics extends Canvas{

    public void paint(Graphics g) {

        setForeground(Color.RED);
        this.setSize(720,440);

        class list {
            int pos;
            list next;
            list prev;

            list(){
                int pos;
                list next ;
                list prev;
            }

            list(int pos){
                this.pos = pos;
            }

            list(int pos, list next){
                this.pos = pos;
                this.next = next;
            }


            public void setpos(int pos){
                this.pos= pos;
            }

            public void setnext(list next){
                this.next= next;
                next.prev=this;
            }

            public void display(list head){
                list tracker = head;
                int y;
                //displays the sincurve momentarily
                for (int i = 1;i<721; i++){
                    y = (int)(Math.sin(Math.toRadians(tracker.pos))*200)+200;
                    g.fillOval(i,y,3,3);
                    tracker = tracker.next;
                }
            }
        }

        list temp = new list();
        temp.setpos(1);
        list head = temp;

        for (int i =2; i<720; i++){
            list thing = new list();
            thing.setpos(i);
            temp.setnext(thing);
            temp = thing;

        }
        list tail = new list(720);
        temp.setnext(tail);
        tail.setnext(head);

        //creates the moving display
        boolean run = true;
        while(run==true){
            head.display(head);

            //try {
                //Thread.sleep(10);

            //} catch(InterruptedException ex) {
               // Thread.currentThread().interrupt();
            //}
            g.clearRect(0, 0, getWidth(), getHeight());
            head = head.next ;
       }
    }

    public static void main(String[] args) {
        DisplayGraphics m=new DisplayGraphics();

        JFrame f=new JFrame();
        f.add(m);
        f.setSize(720,400);
        //f.setLayout(null);
        f.setVisible(true);
    }
}


但是,该程序运行得不太顺利。有什么建议可以使其运行更快,更流畅吗?

最佳答案

好的,有一些缺陷必须纠正^^

1)通过线程触发绘画

//set isRunning=false to stop repaint
private boolean isRunning = true;
private void startUpdateThread(){
    Runnable r = new Runnable() {
        public void run() {

            while(isRunning){
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //repaint calls the paint(Graphics g)-method
            repaint();
            }
        }
    };

    Thread t = new Thread(r);
    t.setDaemon(true);
    t.start();
}


2)不要让事情变得复杂:在绘制^^期间只需计算f(x)= y
3)只做必须要做的事情:不要在每次绘画时都设置大小

private int xCount = 0;
public void paint(Graphics g) {

    //setSize(...)

    xCount = xCount + 1;
    for (int dx = 1; dx < 721; dx++) {

        int x = (xCount%721)+dx;
        int y = (int) (Math.sin(Math.toRadians(x)) * 200) + 200;
        g.fillOval(dx, y, 3, 3);
    }
}


还剩啥?您的设置^^

public static void main(String[] args) {
    DisplayGraphic m = new DisplayGraphic();
    m.startUpdateThread();
    m.setSize(720, 440);

    JFrame f = new JFrame();
    f.add(m);
    f.setSize(720, 400);
    f.setVisible(true);
}


您可能想看看缓冲...如果您不喜欢这样做,只需使用JPanel而不是Canvas(您必须覆盖paintComponent(Graphics g)而不是paint(Graphics g)

public class DisplayGraphic extends JPanel {

    private int xCount = 0;
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        //...
        //same as paint in above
        }
    }
}

09-25 21:55