user3129998
user3129998

Reputation: 1

Combining an absolute layout into a standard layout

I'm trying to create a simple Java GUI application where I can place ports, switches and antenna's. Then I want to map where the output of a port is conducting to through multiple switches that'll be change in the GUI.

See the picture below is custom created objects (port, cables, switches, and antenna's) overriding JPanel containers drawn on a JFrame using absolute coordinates.

In Netbeans I created a form and I want to combine the absolute frame and the form together to make one complete form.

I've done some searching and it all starts with don't use absolute layouts. My question is what layout should I use? I expect to be using 20+ switches, 10+ ports, and even more cables connecting everything which are going to crossover all the other panels (non-Planer).

Can I create a panel using absolute coordinates and then embed it into the form with a different layout manager?

package SystemDiagram;

import java.awt.Point;
import java.util.ArrayList;
import java.util.HashMap;
import javax.swing.JFrame;
import javax.swing.JPanel;



public class SystemDiagram {

Callbox callBox;    //Callbox
Switch swi;      //Switches
HashMap<String,Cable> cables = new HashMap<>();         //Cables
Antenna downlinkAntenna;
Form control;
JFrame f;
JPanel diagram;

public static void main(String args[]) {

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new SystemDiagram();

        }
    });
}

public SystemDiagram() {
    intializeContainers();
    createElements();
}

private void intializeContainers(){
    control = new Form();   //unused until I can combine it with f JFrame as a JPanel
    diagram = control.getDiagramPanel();//unused was trying to draw callbox, switches, and antenna's on this JPanel of the Netbeans form
    f = new JFrame();   //needs to turn into a JPanel and be added to control Form object
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    f.setSize(1000, 800);
}


private void createElements()
{     
    //output
    callBox = new Callbox("OUT", new Point (0,100));

    cables.put("c1", new Cable("c1"));
    cables.put("c2", new Cable("c2"));
    cables.put("c3", new Cable("c3"));

    callBox.setOutputCable(cables.get("c1"));

    //CA Combiner
    swi = new Switch("1 to 3 Switch", 3, new Point(345, 140),true);// Name, # ouf output ports, top left point of Jpanel, reversed 
    //Output side   
    swi.setOutputCable(1, cables.get("c2"));
    swi.setOutputCable(2, cables.get("c3"));
    //Input side
    swi.setInputCable(cables.get("c1"));
    //Activate Output
    swi.setActiveOutput(2);

    //DL Antenna
    downlinkAntenna = new Antenna("link", new Point(500,320), "spiral", true);
    downlinkAntenna.setOutputCable(cables.get("c3"));



}


private void packComponents()
{
    //Callboxes/Ports
    f.add(callBox.getGraphic());// add callbox graphic (JPanel) to Frame
    f.pack();
    f.add( swi.getGraphic());// add switch graphic (JPanel) to Frame
    f.pack();
    //Cables
    ArrayList<String> cableKeys = new ArrayList<>(cables.keySet());
    for(String key: cableKeys){
        if(cables.get(key).getConnections() == 2)// if a cable is connected on both ends draw its' graphic
        {
            Cable temp = cables.get(key);
            temp.getGraphic().setPoint(0,temp.getFirstConnection().getPortCoordinate((Component)temp));// Sets first point coordinate of cable
            temp.getGraphic().setPoint(1,temp.getSecondConnection().getPortCoordinate((Component)temp));// Sets 2nd point of cable
            f.add(cables.get(key).getGraphic());
            f.pack();
        }
    }
    //Antenna pack
    f.add(downlinkAntenna.getGraphic());// add antenna JPanel to Frame
    f.pack();
    f.setVisible(true);
}
}

Upvotes: 0

Views: 1018

Answers (1)

AJNeufeld
AJNeufeld

Reputation: 8695

You can compose views together. For instance, create JPanel with BorderLayout, add your panel with absolute layout to it with BorderLayer.CENTER and add your netbeans generated form as BorderLayout.LINE_END. Alternately, you could use a splitter view.

Upvotes: 2

Related Questions