The Artima Developer Community
Sponsored Link

Java Answers Forum
Help me

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
sara

Posts: 2
Nickname: sarabirdie
Registered: Apr, 2004

Help me Posted: Apr 28, 2004 6:01 AM
Reply to this message Reply
Advertisement
Hello i tried to copy some device wich is (or gate, and gate) and the wire between them, when I pasted this devices I could not connect between the new devices;
each device has 3 ports( 2 input and 1 output),


public Vector CopyDevice = new Vector();
public Vector CopyConnectors = new Vector();

public void Copy()
{
CopyDevice.removeAllElements();
for(int i=0; i< canvas.devices.size();i++)

if(((Device)canvas.devices.elementAt(i)).getActiveSelected()==1)
CopyDevice.addElement((Device)canvas.devices.elementAt(i));

for(int j=0;j< canvas.connectors.size();j++)
{ if (((Connector)canvas.connectors.elementAt(j)) !=null )
{
if((((Connector)canvas.connectors.elementAt(j)).ports[0].device.getActiveSelect ed() ==1) &&
(((Connector)canvas.connectors.elementAt(j)).ports[1].device.getActiveSelected( )==1))

CopyConnectors.addElement(((Connector)canvas.connectors.elementAt(j)));
}
}

}//end copy function


public void Past(){



int index= canvas.nextId; // canvas.nextId get the index of the canvas.device.lastElement() + 1

for(int i=0; i< CopyDevice.size(); i++)
{
Point p=new Point((((Device)CopyDevice.elementAt(i)).getUpperLeft()));
// p.translate(pastNum*4,pastNum*4);


switch(((Device)CopyDevice.elementAt(i)).GetType())
{
case 0:
{
//Create new ANDGate
ORGate or = new ORGate(canvas, p);

canvas.devices.addElement((Device)or);
canvas.nextId++;

break;
}
case 1:
{

//Create new ANDGate
ANDGate and = new ANDGate(canvas, p);
// and.onePortSetAlready=((ANDGate)CopyDevice).onePortSetAlready;
canvas.devices.addElement((Device)and);
break;
}

}

((Device) canvas.devices.lastElement()).draw(p);



}


for(int y=0; y< CopyConnectors.size();y++)
{
Port port1,port2;
for(int f= index; f<canvas.devices.size(); f++)
{
if((port1=((Device) canvas.devices.elementAt(f)).canConnect(((Connector)CopyConnectors.elementAt(y) ).start)) != null)
{
Connector conn = new Connector(canvas,((Connector)CopyConnectors.elementAt(y)).start);
canvas.connectors.addElement(conn);
port1.setConnector(conn, true);
break;
}
}

for(int f= index; f<canvas.devices.size(); f++)
{
if((port2=((Device) canvas.devices.elementAt(f)).canConnect(((Connector)CopyConnectors.elementAt(y) ).end)) != null)
{
if (canvas.getCurrentConnector().canAddPort(port2))
{ port2.setConnector(conn, false);
canvas.getCurrentConnector().end=port2.upperLeft;
canvas.getCurrentConnector().draw();
}
break;
}
}


}


} // end paste function


//********************* Connector Class has this important things



Point start;
Point end;

protected Port[] ports;


public Connector(GateCanvas canvas, Point p){
start = p;
this.canvas = canvas;
ports = new Port[2];
}


/* Because at first the two ends of the connector are the same point, there is no need
* to draw them, they are just set instead
*/
public void draw(Point p) {
start = p;
end = p;
}

public void draw(){

g.drawLine(start.x,start.y, end.x, end.y);
repaint(start.x,start.y, end.x, end.y);

}

//Adds given port to its list of ports
public void addPort(Port p){
if (ports[0] == null){ports[0] = p;}
else if (ports[1] == null){ ports[1] = p;}
}



/* Checks if it is possible to add a port. only true if there are no ports added yet,
* or if there is only one port added and it is not of the same type of the given port.
* (So it is not possible to connect two output ports for example)
*/
public boolean canAddPort(Port p){
if (ports[0] != null && ports[1] != null){return false;}
else if (ports[0].getType() != p.getType()){return true;}
else return false;
}



}


//******************** the Port Class has this important things

//Type of port: either input or output.
protected String type;
protected Point upperLeft;
protected Connector connector;

//Indicates wether this is the first port set by connector or not
//Useful when moveing the connected with the port when it moves
boolean firstPortSet;

//Device the port is attached to.
protected Device device;



//Constructor
public Port(GateCanvas canvas, String t){
this.canvas = canvas;
type = t;
}

//A port accepts connections if is not attached to a connector and the mouse pointer resides in it.
public boolean canConnect(Point p){}


//Checks whether the given point is inside the port or not
public boolean inPort(Point p){}


//Sets the connector to be the one given and adds itself to it.
public void setConnector(Connector c, boolean firstPortSet){
connector = c;
this.firstPortSet = firstPortSet;
connector.addPort(this);
}


//Sets the device attached to it
public void setDevice(Device d){
device = d;
}

Topic: Formatting text in GUI Previous Topic   Next Topic Topic: Conquer the ocp

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use