有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

为什么我的java awt代码运行得很慢?

我是一名Android开发者,但现在我需要使用awt开发一个桌面socket服务器。我写了一个非常小的演示,用Shape来展示一些线条,但速度非常慢

public class Main {

    public static void drawLine(double x1,double y1,double x2,double y2,GeneralPath generalPath,int size){
        Line2D line2D = new Line2D.Double(x1, y1, x2, y2);
        Line2D line2D2 = new Line2D.Double(x2, y2 + size, x1, y1 + size);
        generalPath.append(line2D, false);
        generalPath.append(line2D2, true);
    }

    public static void main(String[] args) {
        SocketServer socketServer = new SocketServer(8890);
        socketServer.startListen();

        Window w = new Window(null) {
            GeneralPath generalPath = new GeneralPath();
            int i = 0;
            @Override
            public void paint(Graphics g) {
                Graphics2D g2d = ((Graphics2D) g);

                Vector<Action> v = socketServer.v;
                for (; i < v.size() - 1; i++) {
                    Action action = v.get(i);
                    if(action.type != action.ACTION_DOWN){
                        Action preAction = v.get(i-1);
                        drawLine(preAction.x,preAction.y,action.x,action.y,generalPath,1);
                    }
                }

                setShape(generalPath);
            }

            @Override
            public void update(Graphics g) {
                paint(g);
            }
        };
        w.setAlwaysOnTop(true);
        w.setBounds(w.getGraphicsConfiguration().getBounds());
        w.setVisible(true);
        while (true) {
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            w.repaint();
        }
    }
}

我想从插座上画实时轨迹,所以我需要一直重新绘制。我还需要提供点击行为,所以我必须使用形状

我对awt知之甚少有人能告诉我为什么我的演示在画了一些线之后运行得这么慢吗? 非常感谢


共 (0) 个答案