2012-12-10 4 views
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

, 실제로. [Swing Event Dispatching] (http://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html)에서 읽으십시오. 모든 그리기 작업에 SwingUtilities.invokeLater를 사용해야합니다. – Perception

+0

thx, 체크 아웃하겠습니다. –

+1

@Perception 그는 이미 이벤트 발송 스레드입니다. 그래서 하위 작업을 그리기 위해'invokeLater'를 사용하는 것은 의미가 없다고 생각합니다. 나는 자바 스윙에서 드로잉을 병렬화하는 것은 불가능하다고 말하고 싶다. –

답변

2

주석에서 지적한대로 Swing에서 그리는 것은 좋지 않습니다. 그러나 이것은 당신이 할 것 (일반적으로) 무엇을 병렬로 drawLine2 문을 실행하기 위해 특별히 OP 질문에 국한 : 당신은 그렇게하고 싶지 않아

// 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() 
+2

어 ... 아니, 좋은 생각이 아니야. – assylias

+0

나는 알고있다. 그러나 그는 평행을 달리는 방법을 물었다. :) –

관련 문제