从Java Graphics实例获取String

时间:2012-12-18 08:03:28

标签: java java-2d

我有一些代码片段如下所示

:

Graphics g;      // Java Graphics class

g.drawString("text", x, y);   // x, y for coordinate info, "text" is now an image object

:

我可以从Graphics实例中将“text”返回到String吗?

因为我想使用String数据作为从表中获取某些数据的键。

3 个答案:

答案 0 :(得分:1)

最好的方法可能是将文本存储在String变量中,然后从那里获取。

答案 1 :(得分:0)

不,Graphics实例只是将屏幕上的文字或图像绘制为像素。它不记得你画的是什么,所以它不能告诉你。

您必须在程序中以其他方式存储文本,以便以后可以将其用于任何您需要的目的(例如从表中获取数据)。

答案 2 :(得分:0)

我通过添加两个表来解决这个问题,每个表都记住在表示层和模型层生成的节点实例的X和Y坐标。

在运行时,节点图像会继续更改其位置,并且这些事件都会更新。

当我点击Panel实例上的节点图像时,它会给出节点实例的id。

需要两个表的原因是,只有当通过调用节点映像的X,Y位置返回的id引用相同的节点对象时,才有证据表明该事件是由单击的节点表示的节点对象图片。现在我意识到我可以使用多键表。

import java.util.ArrayList;
import java.util.List;

// * Represents node at presentation layer and its model

public class NodeObject 
{
// * node id
private int id;

// * coordinate on Panel
public double x;
public double y;

// * coordinate change
private  double dx;
private  double dy;

// * isFixed
public  boolean fixed;

// * node name
public String nodeName;

// * connected node object id list ( redundant when edge is used )
public List<Integer> connectedNodeIdList;

// * connected edge object id list
private List<Integer> connectedEdgeIdList;

// * constructor
public NodeObject()
{}

// * constructor
public NodeObject( int _id )
{
    // * initialization
    this.id = _id;

    // * default
    this.nodeName = "default";

    // * connected node id list
    this.connectedNodeIdList = new ArrayList<Integer>();
}

// * Constructor ( when node name is given )
public NodeObject( int _id, String _nodeName )
{
    // * node id
    this.id = _id;

    // * node name
    this.nodeName = _nodeName;

    // * connected node id list
    this.connectedNodeIdList = new ArrayList<Integer>();

    // * connected edge id list 
    this.connectedEdgeIdList = new ArrayList<Integer>();
}

// * set node name
public void setNodeName( String nName )
{
     this.nodeName = nName;
}

// * get node id
public int getNodeId()
{
    return this.id;
}

// * get node name
public String getNodeName()
{
    return this.nodeName;
}

// * get node name and id in String
public String getNodeNameWithID()
{
      String id = Integer.toString( this.getNodeId() );

      return ( this.nodeName + "-" +id );
}

// * store connected node
public boolean addSingleNode( int nid )
{
    if ( this.connectedNodeIdList.contains( nid ) == true )
    {
        return true;   // this node is already connected
    }
    else
    {
        // * register
        this.connectedNodeIdList.add( nid );

        // * this is the first time this node is connected
        return false;
    }
}

// * unregister a node
public boolean removeConnectedSingleNode( int nid )
{
      // * it was registered before
      if ( this.connectedNodeIdList.contains( nid ) == true )
      {
           this.connectedNodeIdList.remove( nid );

           return true; //  it had been registered and now unregistered ( only at model layer )
      }
      else
      {
           return false;   // it has not been registered before.
      }
}

// * add edge id
public boolean addSingleEdge( int eid )
{
    if ( this.connectedEdgeIdList.contains( eid ) == true )
    {
        return true;   // already registered edge
    }
    else
    {
        // * registered
        this.connectedEdgeIdList.add( eid );

        // * this is the first time this edge was connected
        return false;
    }
}

// * remove edge
public boolean removeConnectedSingleEdge( int eid )
{
      // * registered before
      if ( this.connectedEdgeIdList.contains( eid ) == true )
      {
           this.connectedEdgeIdList.remove( eid );

           return true; // now removed ( model level )
      }
      else
      {
           return false;   // it has not been registered before
      }
}

// * return edge id list
public List<Integer> getEdgeIdList()
{
    return this.connectedEdgeIdList;
}

//////// for visualization on Panel instance ///////

// * X coordinate setting
public void setNodePositionX( Double xPos )
{
     this.x = xPos;
}

// * Y coordinate setting
public void setNodePositionY( Double yPos )
{
     this.y = yPos;
}

// * change of node position
public void changeNodePosition( Double changeX, Double changeY )
{
     this.x = this.x + changeX;
     this.y = this.y + changeY;
}

// * position difference
public void changeNodePositionDifference( Double changeDX, Double changeDY )
{
     this.dx = this.dx + changeDX;
     this.dy = this.dy + changeDY;
}

// * update node position difference
public void updateNodePositionDifference( Double changeDX, Double changeDY )
{
     this.dx = changeDX;
     this.dy = changeDY;
}

// * get X pos
public double getNodePositionX()
{
     return this.x;
}

// * get Y pos
public double getNodePositionY()
{
     return this.y;
}

// * get dx
public double getNodePositionDX()
{
     return this.dx;
}

// * get dy
public double getNodePositionDY()
{
     return this.dy;
}
}

Graph类如下所示

public class Graph extends Panel implements Runnable, MouseListener, MouseMotionListener, ItemListener
{   

// * number of nodes
public int nnodes;

// * node generator
public NodeGenerator ndGenerator;

// * memorize the node coordinate
public Map<Integer, Integer> xCoordinateTbl;   // key : coordinate / value : node id
public Map<Integer, Integer> yCoordinateTbl;

// * node tbl
public Map<Integer, NodeObject> nodeTbl;

一旦发生鼠标点击事件,就会像下面那样处理

public void actionPerformed(ActionEvent event) 
{
    // * get node id of the node object being referred to at the presentation layer
    Integer nid = getNodeId( e.getPoint().x, e.getPoint().y );

    // * remove node on the presentation layer and of the model at the same time
     removeSingleNode( nid );  
}