Solved

adding names/text to a shape drawn in java

Posted on 2004-03-23
10
266 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
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

763 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