2016-05-26 5 views
3

ここに初めて投稿するこのプログラミングの割り当ては、私は困惑している、コードは以下のとおりである:それは正しくコンパイルが、プログラムを実行するときに私が得るすべては空のJFrameのある初心者スイング再帰

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

public class HTree extends JPanel { 

private Color color = new Color((int)(Math.random() * 256), (int)(Math.random() * 256), (int)(Math.random() * 256)); 

public void draw(Graphics g, int n, double sz, double x, double y) { 
    if (n == 0) return; 
    double x0 = x - sz/2, x1 = x + sz/2; 
    double y0 = y - sz/2, y1 = y + sz/2; 
    // draw the 3 line segments of the H 
    g.setColor(color); 
    g.drawLine((int)x0, (int)y, (int)x1, (int)y); 
    g.drawLine((int)x0, (int)y0, (int)x0, (int)y1); 
    g.drawLine((int)x1, (int)y0, (int)x1, (int)y1); 
    // recursively draw 4 half-size 
    // H-trees of order n-1 
    g.setColor(color); 
    draw(g, n-1, sz/2, x0, y0); 
    draw(g, n-1, sz/2, x0, y1); 
    draw(g, n-1, sz/2, x1, y0); 
    draw(g, n-1, sz/2, x1, y1); 
    repaint(); 
} 

public void paintComponent(Graphics g) { 
    super.paintComponent(g); 
    draw(g, 3, .5, .5, .5); 
} 

public static void main(String[] args) { 
    HTree h = new HTree(); 
    JFrame application = new JFrame("HTree"); 
    application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    application.add(h); 
    application.setSize(1000, 1000); 
    application.setVisible(true); 
} 
} 

。私はスイングに慣れていませんが、問題はHTreeのコンストラクタが間違っているか、メインでdraw()を呼び出す必要がありますが、Graphicsオブジェクトで呼び出す方法がわからないことがあると思います。どんな助けもありがとう、ありがとう!

答えて

5

いくつかの所見:

  • 使用整数は、パネルの現在のサイズに対する相対座標。

  • getPreferredSize()を上書きして初期ジオメトリを設定します。

  • Color.getHSBColor()を使用すると明るく飽和した色が得られます。 exampleについては、各レベルで別の色相を選択して、色相を作成することを検討してください。

  • 構築し、スイングGUIを操作するだけevent dispatch thread.

  • にはherehereとが示唆されているように、対話的に再帰の深さを制御するJSpinner又はJSliderの使用を検討オブジェクト。

  • repaint()を再帰的に呼び出さないでください。再帰の深さが入力に応じて課金される場合のように、paintComponent()の実装への呼び出しをスケジュールするために使用します。

  • とレンダリングヒントを使用することを検討してください(here)。

  • javax.swing.Timerを使用して、ActionListenerが呼び出されるたびに再帰の深さを1レベル増加させることを検討してください。

image

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.Graphics; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 

/** 
* @see https://stackoverflow.com/a/37450393/230513 
*/ 
public class HTree { 

    private void display() { 
     JFrame f = new JFrame("HTree"); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     f.add(new Tree()); 
     f.pack(); 
     f.setLocationRelativeTo(null); 
     f.setVisible(true); 
    } 

    private static class Tree extends JPanel { 

     private final Color color = Color.getHSBColor((float) Math.random(), 1, 1); 

     public void draw(Graphics g, int n, double sz, double x, double y) { 
      if (n == 0) { 
       return; 
      } 
      double x0 = x - sz/2, x1 = x + sz/2; 
      double y0 = y - sz/2, y1 = y + sz/2; 
      // draw the 3 line segments of the H 
      g.setColor(color); 
      g.drawLine((int) x0, (int) y, (int) x1, (int) y); 
      g.drawLine((int) x0, (int) y0, (int) x0, (int) y1); 
      g.drawLine((int) x1, (int) y0, (int) x1, (int) y1); 
      // recursively draw 4 half-size 
      // H-trees of order n-1 
      g.setColor(color); 
      draw(g, n - 1, sz/2, x0, y0); 
      draw(g, n - 1, sz/2, x0, y1); 
      draw(g, n - 1, sz/2, x1, y0); 
      draw(g, n - 1, sz/2, x1, y1); 
     } 

     @Override 
     public void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      draw(g, 3, getWidth()/2, getWidth()/2, getHeight()/2); 
     } 

     @Override 
     public Dimension getPreferredSize() { 
      return new Dimension(500, 500); 
     } 

     public static void main(String[] args) { 
      EventQueue.invokeLater(new HTree()::display); 
     } 
    } 
} 
+0

ありがとう!今のところ:http://i.imgur.com/BlS4Amk.pngでは、args [0]から再帰回数をどのように渡すかはまだ分かりませんが、代わりにユーザー入力のインスタンス変数が使用されています。私は各再帰を配列でランダムな色にし、g.fillRectで線を太くすることができました。 – skol

+0

優秀;私は上記のいくつかの提案を追加しました。 – trashgod