如何绘制2d树

时间:2015-10-27 02:13:17

标签: java tree

任何人都可以帮我弄清楚如何制作2D图形树吗?这就是我到目前为止所做的一切。我不知道我在做什么:/有人可以教我怎么做...请? &安培;谢谢!

enter image description here

import java.awt.Color;
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.Polygon;

public class Tree extends Canvas {

    public Tree() {

        350 200 100 350    
        249 0 249           
    }
}

1 个答案:

答案 0 :(得分:1)

首先看一下2D Graphics。您可能还想查看Painting in AWT and SwingPerforming Custom Painting,了解有关在Swing和AWT中绘画的更多详细信息

简单的树......

Tree

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

public class Forest {

    public static void main(String[] args) {
        new Forest();
    }

    public Forest() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
        }

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

        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            int width = getWidth();
            int height = getHeight();

            g2d.setColor(new Color(139, 69, 19));
            g2d.fillRect((width / 2) - 20, height / 2, 40, height / 2);

            g2d.setColor(Color.GREEN);
            int radius = 60;
            g2d.fillOval((width / 2) - radius, (height / 2) - (radius), radius * 2, radius * 2);
            g2d.dispose();
        }

    }


}

复杂树......

Tree

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

public class Forest {

    public static void main(String[] args) {
        new Forest();
    }

    public Forest() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
        }

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

        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            int width = getWidth();
            int height = getHeight();

            g2d.setColor(new Color(139, 69, 19));
            g2d.fillRect((width / 2) - 20, height / 2, 40, height / 2);

            g2d.setColor(Color.GREEN);
            int radius = 60;
            g2d.fillOval((width / 2) - radius, (height / 2) - (radius * 2), radius * 2, radius * 2);
            g2d.fillOval((width / 2) - radius, (height / 2) - radius, radius * 2, radius * 2);
            g2d.fillOval((width / 2) - (radius * 2), (height / 2) - radius, radius * 2, radius * 2);
            g2d.fillOval((width / 2), (height / 2) - radius, radius * 2, radius * 2);
            g2d.dispose();
        }

    }


}