1

首先,看看这个用 d3.js 编写的不可思议的例子:http: //mbostock.github.io/d3/talk/20111018/partition.html

在此处输入图像描述

我正在尝试在 Java 中模拟类似的东西。我不是很成功。我已经能够创建 GUI,但动画被证明是困难的。在玩了一些非常不成功的事情之后,我想我需要一些指导。

编辑:我不确定如何描述问题,但我会尝试。单击嵌套的 JPanel 时,我想聚焦此面板并展开其中的内容以填充整个窗口。就像您在示例中看到的一样,如果您可以查看的话。例如,当您单击标有“vis”的蓝色块时,该块将聚焦于其下方的所有内容。它非常像一棵树,但有生气。

我想我只是被困在如何以一种能够如所描述的方式设计系统上。我正在寻找解决问题的策略。如果我需要更具体,请告诉我。

package animation;

import java.awt.BorderLayout;

public class TestAnimation extends JFrame {
    private boolean buttonActive = false;
    private boolean boxActive = false;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    TestAnimation frame = new TestAnimation();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public TestAnimation() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //this.setExtendedState(JFrame.MAXIMIZED_BOTH);
        setBounds(100, 100, 450, 300);
        getContentPane().setLayout(new BorderLayout());

        ParentObj one = new ParentObj("Vblock", this);
    }
}

这是实例化的父对象的类:

package animation;


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;


public class ParentObj extends JComponent{
    public boolean isActive = false;
    public final JFrame frame;
    public Obj1 compute;
    public Obj1 storage;
    public String name;
    public Obj1 test;

    public ParentObj(String n, JFrame f){
        this.frame = f;
        this.name = n;
        final JPanel panelL = new JPanel();
        panelL.setBackground(Color.blue);
        panelL.setLayout(new BorderLayout());
        panelL.addMouseListener(new MouseListener(){
            public void mouseClicked(MouseEvent e){
                System.out.println(name);
                //startAnimations(box);
            }

            @Override
            public void mousePressed(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseReleased(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseEntered(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseExited(MouseEvent e) {
                // TODO Auto-generated method stub

            }
        });


        JLabel title = new JLabel(name);
        title.setForeground(Color.white);
        title.setHorizontalAlignment(SwingConstants.CENTER);
        panelL.add(title, BorderLayout.CENTER);

        final JPanel panelR = new JPanel();
        panelR.setLayout(new GridLayout(0, 1, 0, 0));
        panelR.addMouseListener(new MouseListener(){
            public void mouseClicked(MouseEvent e){
                System.out.println(name);
                //startAnimations(box);
            }

            @Override
            public void mousePressed(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseReleased(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseEntered(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseExited(MouseEvent e) {
                // TODO Auto-generated method stub

            }
        });


        final JSplitPane panel = new JSplitPane(1, true, panelL, panelR);
        panel.setDividerSize(0);
        panel.setResizeWeight(0.3);
        panel.setBorder(null);
        panel.setBorder(new LineBorder(new Color(0, 0, 0)));
        frame.add(panel, BorderLayout.CENTER);

        compute = new Obj1("Compute", frame ,panelR);
        storage = new Obj1("Storage", frame, panelR);
        test = new Obj1("Network", frame, panelR);
    }

}

最后,个人作品的小班:

package animation;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.JSplitPane;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;

public class Obj1 extends JComponent{
    public String name;
    public JComponent parent;
    public JFrame frame;
    final JSplitPane panel;

    public Obj1(String n, JFrame f, JComponent p) {
        this.name = n;
        this.parent = p;
        this.frame = f;


        final JPanel panelL = new JPanel();
        panelL.setBackground(Color.blue);
        panelL.setLayout(new BorderLayout());

        panelL.addMouseListener(new MouseListener(){
            public void mouseClicked(MouseEvent e){
                System.out.println(name);
                startAnimations();
            }

            @Override
            public void mousePressed(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseReleased(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseEntered(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseExited(MouseEvent e) {
                // TODO Auto-generated method stub

            }
        });


        JLabel title = new JLabel(name);
        title.setForeground(Color.white);
        title.setHorizontalAlignment(SwingConstants.CENTER);
        panelL.add(title, BorderLayout.CENTER);

        final JPanel panelR = new JPanel();
        panelR.setLayout(new GridLayout(0, 1, 0, 0));

        panelR.addMouseListener(new MouseListener(){
            public void mouseClicked(MouseEvent e){
                System.out.println(name);
                //startAnimations(box);
            }

            @Override
            public void mousePressed(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseReleased(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseEntered(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseExited(MouseEvent e) {
                // TODO Auto-generated method stub

            }
        });


        panel = new JSplitPane(1, true, panelL, panelR);
        panel.setDividerSize(0);
        panel.setResizeWeight(0.3);
        panel.setBorder(new LineBorder(new Color(0, 0, 0)));

        p.add(panel, BorderLayout.CENTER);
    }
    public void startAnimations(){
        //panel.setSize(frame.getWidth(), frame.getHeight());
        ///JRootPane test = panel.getRootPane();

    }
}

如果您对如何产生正确的结果有任何建议,或者是否有解决此问题的另一种方法,请告诉我。非常感谢!

4

0 回答 0