用秋千画一个箭头

时间:2015-05-02 05:32:47

标签: java swing canvas

我们如何使用swing在java(桌面应用程序)中绘制箭头?我大多看到那里的箭头画布。当我们创建画布时,现有的容器将替换为画布,然后只显示箭头。如何解决这个问题?

这是我的代码。

public class last extends JFrame 
{

    public static JFrame frame = new JFrame("NoLayout Test");
    ArrayList<Output> out;

    public last()
    {

    JFrame.setDefaultLookAndFeelDecorated(true);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(null);


    frame.setSize(600, 400);
    frame.setVisible(true);

    }



    public void Graph(ArrayList<Output> o) {

        out=o;
     int x1= 300-50, x2=300+25, y=34;

    JLabel label = new JLabel(out.get(0).value);
    label.setBounds(x1, y, x2, y);


    addComponent(3,  x1, y);

    frame.add(label);


  }

    private void addComponent(int child, int x, int y)
    {
        Insets insets = frame.getInsets();
        int x1, x2;
       if (child ==1)
       {
           JLabel label = new JLabel("leaf");
            label.setBounds(x, y*2, 100, 20);
            frame.add(label);
       }


       if (child ==2)
       {
           JLabel label1 = new JLabel("nodes1");

           x1=round(x/2-25);

           System.out.println(x1+"   "+ y);

            label1.setBounds(x1, y*2, 100, 20);
            frame.add(label1);

           x1=round(x+(x/2+25));

           System.out.println(x1+"    "+ y);

             JLabel label2 = new JLabel("nodes2");
            label2.setBounds( x1, y*2, 100, 20);
            frame.add(label2);
       }


       if (child ==3)
       {


           x1=x/2-25;

           JLabel label = new JLabel("nodes1");
           Dimension size = label.getPreferredSize();
            label.setBounds(x1 + insets.left, y*2 + insets.top, size.width, size.height);

            System.out.println(x1+"    "+ y*2);

            //label.setBounds(x1, y*2, 100, 20);
            frame.add(label);



            x1= x+(x/2+25);


             JLabel label2 = new JLabel("nodes2");
            size = label2.getPreferredSize();
            label2.setBounds(x1 + insets.left, y*2 + insets.top, size.width, size.height);
           frame.add(label2);
            System.out.println(x1+"    "+ y*2);

            JLabel label3 = new JLabel("nodes3");


            size = label3.getPreferredSize();
            label3.setBounds(x + insets.left, y*2 + insets.top, size.width, size.height);
            System.out.println(x1+"    "+ y*2);
            frame.add(label3);
       }

    }
}

我想绘制线条或箭头来创建树。这些行在两个节点之间添加,因此形成一个树。

1 个答案:

答案 0 :(得分:-1)

只需使用图片而不是标签!!