0

我现在完全停电了。在底部有一个名为 drawlines2 的方法,我怎样才能让它与两个不同的参数并行执行?我已经尝试过实现可运行,但我不知道在 run 方法中放入什么,我没有掌握它,因为我想在同一个类中创建线程。

package javastuff;
/**
 * Copyright © 2011 Parag Patil
 * Licensed under the Apache License, Version 2.0
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * You may not use this file except in compliance with  Apache License, Version 2.0
 * You may obtain a copy of the license at
 * http://www.apache.org/licenses/LICENSE-2.0

**/

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

public class TreeAnimation {
   public static final int SIZE_X = 1366;
   public static final int SIZE_Y = 768;
   public static void main(final String[] args) {

        final PFrame pframe = new PFrame();         
        pframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);                      
        final PCanvas canvas = new PCanvas((int)pframe.getBounds().getWidth(),(int)pframe.getBounds().getHeight());
        pframe.add(canvas);
        pframe.setVisible(true);
        canvas.setIgnoreRepaint(true);
    }
}
class PFrame extends JFrame {   
    private static final long serialVersionUID = 1L;

    PFrame() {
        setUndecorated(true);
        final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setBounds(0,0,screenSize.width, screenSize.height);
        final KeyStroke escapeKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE,0, false);
        Action escapeAction = new AbstractAction() {
            private static final long serialVersionUID = 1L;

            public void actionPerformed(final ActionEvent e) {
            System.exit(0);
            }
        };
        getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(escapeKeyStroke,"ESCAPE");
        getRootPane().getActionMap().put("ESCAPE", escapeAction);
    }
}
class PCanvas extends Canvas{
    private static final long serialVersionUID = 1L;
    public static int xsize;
    public static int ysize;
    public static final double TRIM_FACTOR = 0.8;
    public static final double INITIAL_LENGTH = 180;
    public static final double EXIT_LENGTH = 2;
    public static final double BRANCH_ANGLE = Math.PI / 9.0;
    public static final int WAIT = 10;

    PCanvas(int size_x, int size_y) {
        super();
        xsize = size_x;
        ysize = size_y - size_y / 20;
        setBackground(Color.black);
        setForeground(Color.white);
    }

    @Override
    public void paint(final Graphics g) {
        drawLine2(g, xsize / 2, 0, INITIAL_LENGTH, Math.PI / 2);
    }

    public void drawLine2(final Graphics g, final double x1, final double y1, final double l, final double theta) {

        if (l < EXIT_LENGTH) {
            return;
        }
        final double x2 = x1 + l * Math.cos(theta);
        final double y2 = y1 + l * Math.sin(theta);

        g.drawLine((int)x1, (int)(ysize - y1), (int)x2, (int)(ysize - y2));

            //Here be parallelizable code
        drawLine2(g, x2, y2, l * TRIM_FACTOR, theta - BRANCH_ANGLE);
        drawLine2(g, x2, y2, l * TRIM_FACTOR, theta + BRANCH_ANGLE);
    }
}
4

1 回答 1

2

请注意,正如评论中指出的那样,在 Swing 中进行绘制并不是一个好主意。但是特别关注您的 OP 问题,要并行运行drawLine2语句,这就是(通常)您会做的事情:

// start first
new Thread(new Runnable() {
    public void run() {
        drawLine2(g, x2, y2, l * TRIM_FACTOR, theta - BRANCH_ANGLE);
    }
}).start();
// start second
new Thread(new Runnable() {
    public void run() {
        drawLine2(g, x2, y2, l * TRIM_FACTOR, theta + BRANCH_ANGLE);
    }
}).start()
于 2012-12-10T16:21:01.943 回答