Solved

adding names/text to a shape drawn in java

Posted on 2004-03-23
10
268 Views
Last Modified: 2010-03-31
hi,
here's what i need help with. i have one table in my database and what i want to happen is that the name of that database should appear on the shape( preferrable rectangle) that i have drawn.
i have the code for getting the name for the database shown below.

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
 conn=DriverManager.getConnection("jdbc:odbc:myDB","userid","password");
DatabaseMetaData dbmd = conn.getMetaData();
ResultSet rs = dbmd.getTables(null,null,null,new String[]{"TABLE"});
System.out.println("the tables in the database are:");

while(rs.next())
{
    String tables = rs.getString("TABLE_NAME");
    System.out.println(tables);
}                              
conn.close();


i know the code for drawing a rectangle goes something like this:

public void paint(Graphics g)
{
  g.drawLine(55,75,225,75);

  g.drawRect(30,50,50,50);
  g.setColor(Color.green);
  g.fillRect(31,51,49,49);
   g.setColor(Color.red);
  g.drawString("A", 55, 75);
              
}

so, instead of that "A" appearing, i want the name of the table to appear. any assistance would be greatly appreciated
0
Comment
Question by:msberth
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
10 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 10658861
You just need to drawString(tableName,......


instead
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10658891
Make 'tables' an instance variable:

while(rs.next())
{
      tables = rs.getString("TABLE_NAME");
         System.out.println(tables);
}                        



..........

g.drawString(tables, 55, 75);


0
 

Author Comment

by:msberth
ID: 10660483
ok. what if more than one table in the database exist. say four? and i want the rectangles to be displayed at different(random location) locations.  for example, if there are four tables, then there should be four rectangles. i dont want to be the one to specify the number of rectangles to be drawn.
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 86

Expert Comment

by:CEHJ
ID: 10660578
Then you'd need to iterate them.

List tableList = new ArrayList();

.......


while(rs.next())
{
     String table = rs.getString("TABLE_NAME");
     tableList.add(table);
}                        

.........

// (in paint or paintComponent)

final int WIDTH = getWidth();
final int HEIGHT = getHeight();
Iterator iter = tableList.iterator();
while (iter.hasNext()) {
     drawString(iter.next().toString(), (int)(Math.random() * WIDTH), (int)(Math.random() * HEIGHT));



0
 

Author Comment

by:msberth
ID: 10661955
thanx
one more thing before i close the question. how about adding actionlistener/mouselistener to a rectangle clicked. say for example i click on one of the displayed rectangles, then a message like "the rectangle named ,"name on rectangle", "has been clicked.
i hope you can help.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10661983
0
 

Author Comment

by:msberth
ID: 10674758
that doesnt help much. can you be a bit clearer
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 30 total points
ID: 10682273
>>
say for example i click on one of the displayed rectangles, then a message like "the rectangle named ,"name on rectangle", "has been clicked.
>>
// 'rects' is an array of Rectangle
for(int i = 0;i < rects.length;i++) {
      boolean wasHit = rects[i].contains(mouseEvent.getPoint());
      if (wasHit) {
            System.out.println("Rect " + i + " was clicked");
      }
 
}


0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This video teaches viewers about errors in exception handling.

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question