Getting stuck developing CAD program
I am developing a DSP CAD program in Java/AWT/Swing.
The idea is that small bits of DSP code are represented by a little block with signal input and output pins, and control inputs. Then there are
also other blocks that don't process signal, but they just do conversions on control signals. All fine so far.
The application so far is based on a JFrame and some JPanels and JToolbars inside. If there are some variables that more than one JPanel needs to access, then they live in the JFrame. All that's fine too.
All of my mouse interaction is in a custom class which extends JPanel. Depending on the drawing mode (which is set by JButtons in a JToolbar), the Mouse Events will do certain things.
For example, if we are in "Add Block" mode, then a mouse click will add a new block to the ArrayList, and draw it on the JPanel (actually it calls repaint(). The paintComponent() method actually iterates through the ArrayList of all blocks which were added and draws them at the location they were added). If we're in "Delete" mode, then a mouse click scans through the list and checks to see if we hit the visible "handle" of a block in the ArrayList and if so, it gets deleted. In "move" mode, we pick up the block and drag it around until we set it down with another click.
Each DSP functional block has a number of pins for connections, which are added dynamically during the constructor for a specific type of DSP function. The base class for these is called "SpinCADBlock" which is simply a generic block with no pins, but you can still add, delete and move them.
Now here's where I'm getting stuck. When I want to connect one block to another, I select "Connect" mode. Then, when I click with the mouse, that handler scans through all blocks and for each block scans through each pin. If it finds a pin at the same location as the mouse pointer, then you break out of that loop and variables "currentBlock" and "currentPin" are used - right now all I'm doing is changing the color of the pin (it's just a little circle). What I WANT to do is attach a line to the pin at one end, and to the mouse pointer at the other end, and have that line move and stretch, attached at one fixed end to the block/pin. Now when I am drawing or moving the blocks, that's not a problem, because I have added all of them to an ArrayList, and paintComponent() just goes through and redraws all of them. But the "line" is not really a "thing" in the design yet and I can't figure out where/how to draw it.
Ultimately the "lines" will represent connections between Pins, and the only important thing as far as the underlying data (DSP code) goes is for an "input" pin to know what "output" it is connected to. As an output could go several places, the output pins don't know and don't care where they are connected. Again, here I am sort of confused as to the best approach. I was thinking of having a separate "Connectors" class but that doesn't seem warranted.
Each DSP block has a location and an ArrayList of Pins. I can distinguish Input Pins from Output Pins. So in the case of an Input Pin, I guess I should add "sourceBlock" and "sourcePin" variables. There's also the possibility that an Input Pin is not connected to anything, in which case the DSP code needs to use a user-supplied fixed value instead.
Bottom line, I need some suggestions about:
1) Drawing the connection line when only one end is attached to a Pin (the other end is still on the mouse) and
2) Finding a way to connect all the connections visually once they are made. As I am iterating through the ArrayList of blocks, I would like to make sure they are ordered properly so that sourceBlocks and sourcePins have already been iterated by the time an inputPin which needs them is iterated.
I hope this makes sense.
Re: Getting stuck developing CAD program
Well it was not as hard as I thought. I just declared a Line2D in my extended panel class.
I guess I was confused or ignorant about the Model-View-Controller architecture. I am really working primarily on the "View" right now and having an extra Line2D hanging around waiting for me to use it is no big deal, even if there is no corresponding bit in the data model. I just make it "null" when I don't want it drawn. Drawing the line is part of the "paintComponent" method.