Home > database >  Creating diagram for an extremely immense tree using Java
Creating diagram for an extremely immense tree using Java

Time:12-03

The problem is simple. I want to create a vertical tree of values, where as you descend down the levels, the amount of values gets exponentially larger. Let's say the 1st level has 1 numerical value, the next has 10, then the next has 100, then the next has 1000, and so on. The first level is connected to the 2nd level with the use of lines, and the 2nd to the 3rd, and so on, much like a game tree. These values are also evenly spaced, so let's say you have a JPanel which is 500x500. At a y of 100, you have 4 values, and so to evenly space them out, you would have a value at 100, one at 200, and so on.

I've tried incorporating drawString, and connecting them with the drawLine method, and placing this so called diagram on a JPanel. That is actually quite simple, and it works if you only have as many as about 50~ values in a singular level. However, when you only have a 1600x900 screen, you can't fit 100 values (on the x axis, which is 1600) without having a big jumbled up mess.

I was thinking there could be 2 possible solutions for this.

One is that the JPanel is not limited to a set resolution (a.k.a the size of your screen) and is scrollable. If it was 10000 x 900 then making this gigantic tree diagram would actually be quite simple, and I could easily fit the 100 values with enough space between them for the values to actually be discernable. However, as far as I know, it's not possible.

The second solution is that I write these values into a file, but I'm not sure how to go about this.

Does anyone know, theoretically speaking, what could be the simplest solution for properly displaying a large tree diagram filled with hundreds of values in a single level?

CodePudding user response:

Oracle has a helpful tutorial, Example

Here's the complete runnable code.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

public class LargeDrawingJPanel implements Runnable {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new LargeDrawingJPanel());
    }

    @Override
    public void run() {
        JFrame frame = new JFrame("Large Drawing JPanel");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel drawingPanel = new DrawingPanel();
        JScrollPane scrollPane = new JScrollPane(drawingPanel);
        scrollPane.setPreferredSize(new Dimension(1400, 950));
        frame.add(scrollPane, BorderLayout.CENTER);

        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public class DrawingPanel extends JPanel {

        private static final long serialVersionUID = 1L;

        public DrawingPanel() {
            this.setPreferredSize(new Dimension(10000, 900));
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            Graphics2D g2d = (Graphics2D) g;
            Color[] colors = { Color.RED, Color.BLACK };
            int colorIndex = 0;
            int x = 0;
            int y = 0;
            for (int i = 0; i < 100; i  ) {
                for (int j = 0; j < 9; j  ) {
                    g2d.setColor(colors[colorIndex]);
                    colorIndex = (colorIndex == 0) ? 1 : 0;
                    g2d.fillRect(x, y, 100, 100);
                    y  = 100;
                }
                x  = 100;
                y = 0;
            }
        }

    }

}
  • Related